Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Probable Bug, Or am I just stupid?
Jeune
post Dec 19 2005, 10:41 AM
Post #1


Member [Level 2]
*****

Group: Members
Posts: 82
Joined: 20-October 05
Member No.: 13,144



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




Go to the top of the page
 
+Quote Post
Captain_Thunder
post Dec 19 2005, 01:19 PM
Post #2


Newbie [Level 1]
*

Group: Members
Posts: 15
Joined: 17-December 05
Member No.: 15,864



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?
Go to the top of the page
 
+Quote Post
Jeune
post Dec 20 2005, 08:56 AM
Post #3


Member [Level 2]
*****

Group: Members
Posts: 82
Joined: 20-October 05
Member No.: 13,144



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?
Go to the top of the page
 
+Quote Post
Captain_Thunder
post Dec 20 2005, 12:28 PM
Post #4


Newbie [Level 1]
*

Group: Members
Posts: 15
Joined: 17-December 05
Member No.: 15,864



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
Go to the top of the page
 
+Quote Post
switch
post Dec 26 2005, 01:52 AM
Post #5


Premium Member
********

Group: Members
Posts: 178
Joined: 13-October 04
From: NSW, Australia
Member No.: 1,713



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
Go to the top of the page
 
+Quote Post
Jeune
post Dec 27 2005, 10:09 AM
Post #6


Member [Level 2]
*****

Group: Members
Posts: 82
Joined: 20-October 05
Member No.: 13,144



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!
Go to the top of the page
 
+Quote Post
klusk
post Feb 27 2008, 07:47 PM
Post #7


Member [Level 1]
****

Group: Members
Posts: 55
Joined: 16-February 08
From: bombay
Member No.: 57,972



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

This post has been edited by klusk: Feb 27 2008, 07:47 PM
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Stupid Credit System(9)
  2. Stupid Teenagers!(48)
  3. Stupid Computer Crash Due To Power Surge(17)
  4. What Do You Do When Two Friends Betray You?(17)
  5. Stupid Science Teachers(10)
  6. Love Sucks; Love Is Stupid(24)
  7. Stupid Hosts Out There(3)
  8. Stupid Statements(13)
  9. What Are Some Stupid Things You've Read?(22)
  10. Maze Game... (stupid, Short, Fun...)(10)
  11. Can The Internet Make You Stupid ?(54)
  12. My Stupid Friend And Her ...mess Up...of A Boyfriend!(5)
  13. Stupid Girls(21)
  14. Stupid Questions With Just As Stupid Answers.(9)
  15. The New Portuguese Identity Card!(4)
  1. My Two Stupid Friends... Immitating Rick Larkin The Dress Code Nazi.(0)
  2. Stupid Ikea Instructions(16)
  3. I Hate Byethost!(24)
  4. This Is Just Too Funny! (or Stupid)(7)
  5. Stupid And Pathetic People(8)
  6. My Bird(3)
  7. Why Tables For Layout Is Stupid(10)
  8. Possibly A Stupid Question, But Im Stumped(2)
  9. I'm Not Dead! (yet)(3)
  10. Stupid People Who Can't Shut Up.(15)
  11. A (mostly) Stupid Question.(3)
  12. Stupid Schools(3)
  13. Stupid Things You've Done While Making Or Fixing A Pc(4)
  14. More Stupid Pc Troubles(12)
  15. Stupid Sore Throat.(8)