-
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?
-
>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?
Correct, if scanf fails then whatever is not read will remain in the stream.
>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?
Well, I'm not a moderator but I do spend a lot of time here. I'm usually around during the day when I'm at work and nothing is going on ( which is often ).
-Prelude
-
Prelude, is there much demand for C programmers right now? I'm kind of looking for my first programming job. I took a hardware course that included two C classes and then learned the C portion of Deitel.
-
>is there much demand for C programmers right now?
Right now the pickings are very slim and entry level positions are all but nonexistant. This should change soon, but it's impossible to guess when.
-Prelude
-
Prelude, do you find that the board is a good way to keep your skills sharp? Or is it more of a pastime?
-
>Prelude, do you find that the board is a good way to keep your
>skills sharp? Or is it more of a pastime?
For the most part it's a passtime, but every once and a while a question pops up that requires me to double check myself or do research to find the answer. And "find the error" questions are useful for keeping my debugging skills sharp since you see some strange errors around here. ;)
-Prelude
-
Prelude, just wanted to thank you for your insights, and will be talking to you again on some other threads!
-
scanf
Hi,
Below is a way around the scanf problem. Maybe not the best but it works in both of my 16bit compilers and the free Borland 5.5.
#include<stdio.h>
#include <conio.h>
int main()
{
char errorbuff[100]; //Really doesn't have to be this large.?
int numberin;
int correctflag; // Set when a int is entered
for(correctflag = 0; correctflag == 0; )
{
printf("\nEnter A Number ");
if(!scanf("%d",&numberin)) // the entry was not an int.
{
scanf("%s",errorbuff);
}
else
{
correctflag = 1;
}
}
printf("\nNumber entered %d",numberin);
getch();
return 0;
}
Just another way.
Hope it helps.
-
>if(!scanf("%d",&numberin)) // the entry was not an int.
>{
>scanf("%s",errorbuff);
>}
>else
>{
>correctflag = 1;
>}
Eight lines just do handle a bug with scanf?
-Prelude