Join 118,588 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 831 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
I've built a function called recycle which looks like this:
cpp
void recycle(int select) { int ans;
scanf_s("%d", &ans);
if (ans == 1) { search(select); } else if (ans == 0) { system("cls"); menu(); } else { printf("Invalid Choice. Press enter to try again"); system("pause"); recycle(select); } }
It's purpose in my program is that when the user is finished looking up one student, it will give said user a chance to look up another student if they would like to. However the problem I run into is that it will work once, and the next time it won't give the user the option, it will tell you to hit any key and return to the main menu. If there is a way in C to make this work no matter what and it will keep on asking that question until you enter 0.
Also a question a little off of this topic, but I really started getting interested in the system(*); commands. I know of system("cls"; (to clear the screen) and system("pause"); (to wait for user input before continuing). What I'd like to know is what other system(*); commands are there, what do they do, and more specifically are there any that will communicate with the computer's installed printer say to print a hard copy of something.
You might need to clear the input buffer before asking for input, though I don't quite remember. It's been a long time since I've used scanf. It works the first time, though I'm not sure if the carriage return (when you press <ENTER>) is actually removed from the stream with the scanf. Calling a scanf with something other than a number in the stream is likely to yield a zero, which with your code, calls the menu.
void recycle(int select) { int ans; printf("\nEnter your selection (1 or 0):"); scanf_s("%d", &ans);
if (ans == 1) { printf("You typed in a one!\n"); //search(select); } else if (ans == 0) { printf("You typed in a zero!\n"); //menu(); } else { printf("Invalid Choice. Press enter to try again"); recycle(select); } }
int main() { int choice = 3; recycle(choice); return 0; }
Ok try it in this program and see if you can reproduce the error:
CODE
/*Welcome to the Grade Book program. Students grades will be read from file, and we will manipulate this info to get grades and scores for students. Enjoy! Written By: Brandon Matykunas Date: 7/18/2008*/
void menu(); //Prints a nice looking menu Thanks to James Stam void menuoption (int* select, int* selectIn); //Pick a choice any choice. int search (int select); //Hey lookee what I find int compare (int *x, int *y) { return (* x -*y); } int printscr (int select); //Print the scores about a given student int printavg (int select); //Print the average of a given student int printgrd (int select); //Print the letter grade of a given student int printinfo (int select); //Print scores, average, and letter grade of a given student int extprog (void); //Just made a special way to exit the program void recycle (int select); //Want to look up another student, here's how void scores (int studentnumber, int sdata[][6]); //This is how we print the scores for the student we find void average (int studentnumber, int sdata[][6]); //This is how we print the average for the student we find void grade (int studentnumber, int sdata[][6]); //This is how we print the letter grade for the student we find void allinfo(int studentnumber, int sdata[][6]); //This is how we print all the info we find for a student
//Call main int main (void) { int select, selectIn;
menuoption (&select, &selectIn); //Surprisingly this is all main has to do. What a slacker
return 0;
}//End main
//Once again, thanks James for the good looking menu. I made a few changes though, but I kept the output formatting void menu (void) { printf("\t\t\tWelcome to Grade Book V1.0.\n\t\tPlease tell me what we're going to do today?\n\n"); printf("\t\t---------------------------------------------\n"); printf("\t\t| 1:Print A Students Scores |\n"); printf("\t\t| 2:Print A Students Average |\n"); printf("\t\t| 3:Print A Students Letter Grade |\n"); printf("\t\t| 4:Look up a Students info |\n"); printf("\t\t| 5:Quit |\n"); printf("\t\t---------------------------------------------\n");
return; }//End menu //Once again James gets credit for building menu options, however I've made some improvements I'll point out void menuoption (int* select, int* selectIn) { do { menu(); printf("Please choose option 1-5:\n"); scanf_s("%d", select);
switch(*select) { case 1: *selectIn = printscr(*select); break; case 2: *selectIn = printavg(*select); break; case 3: *selectIn = printgrd(*select); break; case 4: *selectIn = printinfo(*select); break; case 5: *selectIn = extprog(); //Yes I actually built a function on how to kill my program break; default: FLUSH; system("cls"); //You're going to notice a lot of this command to clear the screen printf("\aNot recognized. Please try again.\n"); *selectIn = 1; //Now I've wiped the screen and let you start over, there only seems to be a glitch when it comes to //letters though } } while(*selectIn); }//End choose option
//Now comes the fun part, searching an array int search (int select) { //I decided to just hardcode my values into the array, because I could never seem to get the sprintf command to work right int sdata[15][6] = { {1234, 52, 7, 100, 78, 34}, {1947, 45, 40, 88, 78, 55}, {2134, 90, 36, 90, 77, 30}, {2877, 55, 50, 99, 78, 80}, {3124, 100, 45, 20, 90, 70}, {3189, 22, 70, 100, 78, 77}, {4532, 11, 17, 81, 32, 77}, {4602, 89, 50, 91, 78, 60}, {5405, 11, 11, 0, 78, 10}, {5678, 20, 12, 45, 78, 34}, {6134, 34, 80, 55, 78, 45}, {6999, 0, 98, 89, 78, 20}, {7874, 60, 100, 56, 78, 78}, {8026, 70, 10, 66, 78, 56}, {9893, 34, 9, 77, 78, 20} }; size_t elems = 90; //15*6 we have 90 values to search in this array int studentnumber; int *find;
system("cls"); //Again I've cleared the screen so you can see what you're actually doing printf("Please enter a Student Number for more info: "); scanf_s("%d", &studentnumber);
//You will notice this is not your average search, this is a lateral search, maybe not as fast but luckily this is a small array find = (int *)_lfind (& studentnumber, &sdata, &elems, sizeof (int), (int(*) (const void *, const void *)) compare); if (find) //If the item is found we'll do one of these for things depending on the menuoption you selected before { printf ("\nStudent %d found Please verify the results.\n\n", studentnumber); if (select == 1) //this comes from selecting Print a students Scores scores(studentnumber, sdata); else if (select == 2) // this comes from selecting Print a Students Average average(studentnumber, sdata); else if (select == 3) // this comes from selecting Print a Students Letter Grade grade(studentnumber, sdata); else if (select == 4) // this comes from selecting Look up a Students info allinfo(studentnumber, sdata); } else //If we don't find the student you're looking for we'll ask again. Just be careful because you can look up a grade { printf ("Could not find student %d. Press enter to try again.\n", studentnumber); system("pause"); search(select); } return 1; }//Done searching
//This will only print the students scores and is a result of selecting option 1 int printscr (int select) { search(select); //Called the search function printf("\n\n"); printf("Would you like to look up another student?\n1 to look up another student, 0 to return to the main menu.\n"); recycle(select); //This will let you look up another student system("pause"); system("cls"); return 1; } int printavg (int select) { search(select); printf("\n\n"); printf("Would you like to look up another student?\n1 to look up another student, 0 to return to the main menu.\n"); recycle(select); system("pause"); system("cls"); return 1; } int printgrd (int select) { search(select); printf("\n\n"); printf("Would you like to look up another student?\n1 to look up another student, 0 to return to the main menu.\n"); recycle(select); system("pause"); system("cls"); return 1; } int printinfo (int select) { search(select); printf("\n\n"); printf("Would you like to look up another student?\n1 to look up another student, 0 to return to the main menu.\n"); recycle(select); system("pause"); system("cls"); return 1; } int extprog (void) { int ans;
system("cls");
printf("Are you sure you want to quit? 0 to quit, 1 to return to main menu.\n"); scanf_s("%d", &ans);
if (ans == 0) { return 0; } else if (ans == 1) { system("cls"); return 1; } else if (ans != 1 || 0) { printf("Sorry Invalid choice. Press Enter to try again.\n"); system("pause"); system("cls"); extprog(); } return 1; } void recycle(int select) { int ans;
scanf_s("%d", &ans);
if (ans == 1) { search(select); } else if (ans == 0) { system("cls"); menu(); } else { printf("Invalid Choice. Press enter to try again"); system("pause"); recycle(select); } } void scores (int studentnumber, int sdata[][6]) { int studentID = studentnumber;
for (int i = 0; i < 15; i++) { if (studentID == sdata[i][0]) { printf("\tQuiz 1 Quiz 2 Quiz 3 Quiz 4 Quiz 5"); printf("\n"); printf("\t"); for (int j = 1; j < 6; j++) { printf("%d\t", sdata[i][j]); } } } } void average (int studentnumber, int sdata[][6]) { int studentID = studentnumber; int sum = 0, avg = 0;
for (int i = 0; i < 15; i++) { if (studentID == sdata[i][0]) { for (int j = 1; j < 6; j++) { sum += sdata[i][j]; } avg = sum/5; printf("\n\nAverage: %d\n\n", avg); } } } void grade (int studentnumber, int sdata[][6]) { int studentID = studentnumber; int sum = 0, avg = 0; char Grade;
for (int i = 0; i < 15; i++) { if (studentID == sdata[i][0]) { for (int j = 1; j < 6; j++) { sum += sdata[i][j]; } avg = sum/5;
if (avg >= 90) Grade = 'A'; else if (avg >= 80) Grade = 'B'; else if (avg >= 70) Grade = 'C'; else if (avg >= 60) Grade = 'D'; else Grade = 'F'; printf("Your Grade in this class is: %c\n", Grade); } } } void allinfo(int studentnumber, int sdata[][6]) { int studentID = studentnumber; int sum = 0, avg = 0; char Grade;
for (int i = 0; i < 15; i++) { if (studentID == sdata[i][0]) { printf("\tQuiz 1 Quiz 2 Quiz 3 Quiz 4 Quiz 5"); printf("\n"); printf("\t"); for (int j = 1; j < 6; j++) { sum += sdata[i][j]; printf("%d\t", sdata[i][j]); } avg = sum/5; printf("\n\nAverage: %d\n\n", avg);
if (avg >= 90) Grade = 'A'; else if (avg >= 80) Grade = 'B'; else if (avg >= 70) Grade = 'C'; else if (avg >= 60) Grade = 'D'; else Grade = 'F'; printf("Your Grade in this class is: %c\n", Grade); } } }
I can recreate the error. It happens any time you try to do it more then once on any option. I've tried flushall(), but that isn't working. I'm thinking it has something to do with the way you defined FLUSH.
flushall() should take care of anything left inside the "buffer"
I took out the system pauses and it immediately returns to the menu after you search for a second student. I would try looking at that flow. I would continue searching, but I need to sleep
If there is a way in C to make this work no matter what and it will keep on asking that question until you enter 0.
Yes, there is. Adding a while loop (and a couple other minor changes) does the trick...but there's still at least one thing that needs to be worked out.
Maybe you can figure out how to get it to quit asking after we get back to the main menu : "Would you like to look up another student? 1 to look up another student, 0 to return to the main menu."
cpp
/*Welcome to the Grade Book program. Students grades will be read from file, and we will manipulate this info to get grades and scores for students. Enjoy! Written By: Brandon Matykunas Date: 7/18/2008*/
void menu(); //Prints a nice looking menu Thanks to James Stam void menuoption (int select, int* selectIn); //Pick a choice any choice. int search (int select); //Hey lookee what I find int compare (int *x, int *y) { return (* x -*y); } int printscr (int select); //Print the scores about a given student int printavg (int select); //Print the average of a given student int printgrd (int select); //Print the letter grade of a given student int printinfo (int select); //Print scores, average, and letter grade of a given student int extprog (void); //Just made a special way to exit the program void recycle (int select); //Want to look up another student, here's how void scores (int studentnumber, int sdata[][6]); //This is how we print the scores for the student we find void average (int studentnumber, int sdata[][6]); //This is how we print the average for the student we find void grade (int studentnumber, int sdata[][6]); //This is how we print the letter grade for the student we find void allinfo(int studentnumber, int sdata[][6]); //This is how we print all the info we find for a student
//Call main int main (void) { int select, selectIn;
menuoption (select, &selectIn); //Surprisingly this is all main has to do. What a slacker
return 0;
}//End main
//Once again, thanks James for the good looking menu. I made a few changes though, but I kept the output formatting void menu (void) { printf("\t\t\tWelcome to Grade Book V1.0.\n\t\tPlease tell me what we're going to do today?\n\n"); printf("\t\t---------------------------------------------\n"); printf("\t\t| 1:Print A Students Scores |\n"); printf("\t\t| 2:Print A Students Average |\n"); printf("\t\t| 3:Print A Students Letter Grade |\n"); printf("\t\t| 4:Look up a Students info |\n"); printf("\t\t| 5:Quit |\n"); printf("\t\t---------------------------------------------\n");
return; }//End menu //Once again James gets credit for building menu options, however I've made some improvements I'll point out void menuoption (int select, int* selectIn) { do { menu(); printf("Please choose option 1-5:\n"); cin>>select;
switch(select) { case 1: *selectIn = printscr(select); break; case 2: *selectIn = printavg(select); break; case 3: *selectIn = printgrd(select); break; case 4: *selectIn = printinfo(select); break; case 5: *selectIn = extprog(); //Yes I actually built a function on how to kill my program break; default: FLUSH; system("cls"); //You're going to notice a lot of this command to clear the screen printf("\aNot recognized. Please try again.\n"); *selectIn = 1; //Now I've wiped the screen and let you start over, there only seems to be a glitch when it comes to //letters though } } while(*selectIn); }//End choose option
//Now comes the fun part, searching an array int search (int select) { //I decided to just hardcode my values into the array, because I could never seem to get the sprintf command to work right int sdata[15][6] = { {1234, 52, 7, 100, 78, 34}, {1947, 45, 40, 88, 78, 55}, {2134, 90, 36, 90, 77, 30}, {2877, 55, 50, 99, 78, 80}, {3124, 100, 45, 20, 90, 70}, {3189, 22, 70, 100, 78, 77}, {4532, 11, 17, 81, 32, 77}, {4602, 89, 50, 91, 78, 60}, {5405, 11, 11, 0, 78, 10}, {5678, 20, 12, 45, 78, 34}, {6134, 34, 80, 55, 78, 45}, {6999, 0, 98, 89, 78, 20}, {7874, 60, 100, 56, 78, 78}, {8026, 70, 10, 66, 78, 56}, {9893, 34, 9, 77, 78, 20} }; size_t elems = 90; //15*6 we have 90 values to search in this array int studentnumber; int *find;
system("cls"); //Again I've cleared the screen so you can see what you're actually doing printf("Please enter a Student Number for more info: "); cin>> studentnumber;
//You will notice this is not your average search, this is a lateral search, maybe not as fast but luckily this is a small array find = (int *)_lfind (& studentnumber, &sdata, &elems, sizeof (int), (int(*) (const void *, const void *)) compare); if (find) //If the item is found we'll do one of these for things depending on the menuoption you selected before { printf ("\nStudent %d found Please verify the results.\n\n", studentnumber); if (select == 1) //this comes from selecting Print a students Scores scores(studentnumber, sdata); else if (select == 2) // this comes from selecting Print a Students Average average(studentnumber, sdata); else if (select == 3) // this comes from selecting Print a Students Letter Grade grade(studentnumber, sdata); else if (select == 4) // this comes from selecting Look up a Students info allinfo(studentnumber, sdata); } else //If we don't find the student you're looking for we'll ask again. Just be careful because you can look up a grade { printf ("Could not find student %d. Press enter to try again.\n", studentnumber); system("pause"); search(select); } return 1; }//Done searching
//This will only print the students scores and is a result of selecting option 1 int printscr (int select) { search(select); //Called the search function
while(select !=0)//let's see if adding a while loop will help { recycle(select); } //This will let you look up another student system("pause"); system("cls"); return 1; } int printavg (int select) { search(select);