Jump to content
xisto Community
Sign in to follow this  
Jeune

Probable Bug Or am I just stupid?

Recommended Posts

Hi guys!

I need help with a simple C program I am doing.
I want the program to do something then ask the user if it wants to do it again.
I used a do-while loop for this but as it comes out it's either a bug or there's something I really need to know.

Here is my code btw:

#include <stdio.h>main() {     char a='y';     int b;    do     {            printf("\nEnter a number: ");            scanf("%d",&b);            printf("You entered %d\n\n",b);                         printf("try again? (y/n): ");            scanf("%c",&a);            printf("\n");    } while (a!='n');}

I have a problem with my code and it this is what happens:

Enter a number: 5
You entered 5

try again?(y/n):

Enter a number: _ <---------- cursor

It does not ask if the user tries again. Instead, it goes directly to asking the user too input a new number.

Has anyone encountered this problem before?
I really need help.

Thanks

Share this post


Link to post
Share on other sites

I wouldn't set a equal to y right off the bat. Try "char a;" rather than "char a='y';" and see if that helps.BTW, what is the purpose of your program? You enter a number, and then you get to "try again". Are you trying to make some sort of number guessing game?

Share this post


Link to post
Share on other sites

I wouldn't set a equal to y right off the bat.  Try "char a;" rather than "char a='y';" and see if that helps.

214180[/snapback]


tried that already. I still get the same result.

 

BTW, what is the purpose of your program?  You enter a number, and then you get to "try again".  Are you trying to make some sort of number guessing game?

 


I have a more complicated program that I already finished. The only thing I havent figured out is this part, asking if the user wants to try again.

 

I wrote a much simpler task before the try again part just to ilustrate that I want the program to do a task then ask if the user wants to try it again.

 

So any more ideas?

Share this post


Link to post
Share on other sites

I just compiled it myself and you're problem's occuring on my computer too.

 

Now this is interesting: If you take a look at the value of a when the program is running, it actually shows up as '10'. Try changing while (a != 'n'); to while (a == 'y');. The program terminates after the first loop.

What is happening in the background is that a is being set to the ASCII value 10 by the scanf() function.

 

I've just done a search through the MSDN documentation, and this article has some interesting information on your problem that you might find useful.

 

Take a look at this quote from the MSDN article:

If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read.

Here we go! Turns out that Ascii code 10 (the value that a is being set to) represents the '\n' character. What is happening is that when you press ENTER after being asked for input, the scanf() function reads the integer, hits the \n and then decides not to read it (in accordance with the quote). The second time scanf() is called, it reads '\n' and stores it in a.

 

Here's a Solution:

#include <stdio.h>main(){    char a;	char enter; //useless variable to store '\n'	a='y';    int b;   do   {           printf("\nEnter a number: ");           scanf("%d",&b);           printf("You entered %d\n\n",b);                      printf("try again? (y/n): ");     scanf("%c", &enter);     scanf("%c%c",&a,&enter);           printf("\n");   } while (a == 'y');}
that's just a slightly modified version of yours. PM me if you have any questions.

 

Cheers bro! :D

Share this post


Link to post
Share on other sites

Interesting discovery switch. Someone also told me that but I couldn't quite make out what he was saying. I'll work on it when I get back home. Thanks a million!

Share this post


Link to post
Share on other sites

i know the problem with your program ...and the solution is simple......

USE FLUSH BEFORE THE SCANF FUNCTION.....

Flush functions are used to clear the buffer memory...
we use flush(stdin) before the getch() command and scanf command so as to clear the memory space...
this is needed because in your program scanf() is taking 'enter' as a character ...to avoid this we use the flush functions.

here is the code......I have just added a statement...FLUSHALL...



#include <stdio.h>main(){	char a='y';	int b;   do   {		   printf("\nEnter a number: ");		   scanf("%d",&b);		   printf("You entered %d\n\n",b);		   		   printf("try again? (y/n): ");		   flushall();		   scanf("%c",&a);		   printf("\n");   } while (a!='n');}



so flushall is the solution to your problem...
you can also use flush(stdin)....
Edited by klusk (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.