-
crazy output
Please look at my code:
#include <stdio.h>
#include <stdlib.h>
int get_menu_choice (void);
int main ()
{
int choice;
choice = get_menu_choice();
printf("you chose menu option %d\n", choice);
return 0;
}
int get_menu_choice (void)
{
unsigned int selection;
do
{
printf("\n");
printf("\n1 - add record");
printf("\n2 - change record");
printf("\n3 - Delete a record");
printf("\n4 - Quit");
printf("\n");
printf("\nEnter a selection: ");
scanf("%u", &selection);
}
while (selection < 1 || selection > 4);
return selection ;
}
when a char is entered as a menu choice i get crazy output. Is there a simple way that i can stop this from happening?
I've tried adding if statements, etc but nothing has seemed to work.
Can anyone please help me thanku.
-
You need to put parenthesis' around your compound conditionals, is my guess, since I think || supercedes <
-
Adding parenthesis' doesnt work.
Any other ideas?
How can i check the return value of scanf?
-
hmm... i'm guessing it's your usage of scanf then... give me an example of your input and output...
-
that will work i think
i think
%d in place of %u will work perfectly in the following statement.
scanf("%u", &selection);
-
Just a guess, but I noticed that you scan an unsigned int and then it gets coerced into an int. Try keeping it an int all the way through.
-
I've made everytinh int all the way through but it doesnt work...
input
1 - add record
2 - change record
3 - Delete a record
4 - Quit
Enter a selection: y
OUPUT:
Enter a selection:
1 - add record
2 - change record
3 - Delete a record
4 - Quit
Enter a selection:
1 - add record
2 - change record
3 - Delete a record
4 - Quit
Enter a selection:
1 - add record
2 - change record
3 - Delete a record
4 - Quit
Enter a selection:
1 - add record
2 - change record
3 - Delete a record
4 - Quit
.
.
.
.
This happens until i hit control X.
Thats the crazy output. I'm thinking i have to check the return status of scanf?
-
ah i see, i think you need to clear out your input buffer when doing this at each iteration so it waits for new input instead of running on the old. maybe there's a better way of doing this... perhaps using another method to handle the input...
-
-
In the do-while loop of the function, maybe print selection after you scan it, just to see what it is.
-
I added a print statement but it shows the same value as the final print statement in main.
I also added that check = scanf...
i made it so that if check != "%d" exit.
I dont know why this work but their is no crazy output but the do while loop doesnt go around again after this:
I get this output instead:
1 - add record
2 - change record
3 - Delete a record
4 - Quit
Enter a selection: u
3344you chose menu option 3344
No crazy out put. Is the "%d" allowed?
here is what i did...
#include <stdio.h>
#include <stdlib.h>
int get_menu_choice (void);
int main ()
{
int choice;
choice = get_menu_choice();
printf("you chose menu option %d\n", choice);
return 0;
}
int get_menu_choice (void)
{
int selection, check;
do
{
printf("\n");
printf("\n1 - add record");
printf("\n2 - change record");
printf("\n3 - Delete a record");
printf("\n4 - Quit");
printf("\n");
printf("\nEnter a selection: ");
check = scanf("%d", &selection);
printf("%d", selection);
return selection ;
}
while ((check != "%d")||(selection < 1) || (selection > 4));
}
The problem isnt yet fixed.It works on one compiler but not another
-
Try this:
Code:
#include <stdio.h>
int get_menu_choice (void);
int main ()
{
int choice;
choice = get_menu_choice();
printf("you chose menu option %d\n", choice);
return 0;
}
int get_menu_choice (void)
{
int selection;
do
{
printf("\n");
printf("\n1 - add record");
printf("\n2 - change record");
printf("\n3 - Delete a record");
printf("\n4 - Quit");
printf("\n");
printf("\nEnter a selection: ");
scanf("%d", &selection);
while ( getchar() != '\n' ); /* Clear the input buffer */
}
while (selection < 1 || 4 < selection );
return selection;
}
-Prelude
-
Prelude, you've added code to ignore the newline. I don't understand why the newline would cause an infinite loop?
-
>I don't understand why the newline would cause an infinite loop?
The program uses scanf to read an integer, if the user enters an integer then all is well. If the user enters a character then scanf fails, and because of the newline in the buffer, scanf can't read anything valid and will continually fail. Thus the infinite loop. I cleared the input buffer because if it is empty the program will wait for user input instead of attempting to read what is there and failing if it is invalid.
-Prelude
-
Prelude, I've always been pretty confused about buffers. From your reply, I gather that characters only leave the buffer when scanf is successful, otherwise they just stay there?
I've noticed that you are one of the main moderators here and you seem to really know your stuff. Do you do this while you're programming?