May 16, 2008

Probable Bug - Or am I just stupid?

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

Probable Bug - Or am I just stupid?

Jeune
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:

CODE

#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




 

 

 


Reply

Captain_Thunder
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?

Reply

Jeune
QUOTE(Captain_Thunder @ Dec 19 2005, 09:19 PM)
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.
*



tried that already. I still get the same result.

QUOTE
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?

 

 

 


Reply

Captain_Thunder
Hm, I'll try writing a program with the effect you want to achieve, and then compare it to your code. Sorry I don't have time right now, but I'll put it on my to-do list smile.gif

Reply

switch
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:
QUOTE
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:
CODE
#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! biggrin.gif

Reply

Jeune
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!

Reply

klusk
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...



CODE
#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)....

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics
Looking for probable, bug, stupid

Searching Video's for probable, bug, stupid
advertisement



Probable Bug - Or am I just stupid?



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE