Jul 20, 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

Keywords : probable, bug, stupid

  1. Stupid Hosts Out There
    (3)
  2. Maze Game... (stupid, Short, Fun...)
    (10)
    Here is a little flash game I made... five levels..... some of them are difficult... some of them
    are not... enjoy.... The basic premise is you have to move your mouse through the mazes without
    touching the green walls... then you must click on the red button.... There are a few things I need
    to fix... but I'll do that later... also, I plan on adding many many more levels later, when I
    have more time.... Again, enjoy! (Oh yeah, on level 5... you have to find the correct button)
    http://www.2kart.trap17.com/maze%20game/maze%20game.html ....
  3. My Two Stupid Friends... Immitating Rick Larkin The Dress Code Nazi.
    (0)
    Well, before you watch this video, I figured I might give you a little background info. A couple of
    years ago at the C-Town Highschool, there was a principal named Rick Larkin. He was a complete
    dress code nazi... and flip flops... oh you didn't want to be caaught wearing those around
    him... http://youtube.com/watch?v=e87xLw73jdI ....
  4. Stupid Ikea Instructions
    (16)
    ok today me and my family went down to ikea and bought a cabinet with a roller door on the frount of
    which my mum had seen on a ealier acassion and she now had a place to put it. so we bought it and
    went home. on the instruction manual it said "construction time = 30 mins" well first of that was a
    lie so we got started putting it together. first of was to but the actual cabinet together. no
    proplem there. time = 5 mins ok you know how ikea instructions are picture of what you should do.
    well this one was very confusing because it was time to but the roller door on. i....
  5. Stupid Questions With Just As Stupid Answers.
    Post 'em here guys, I've started us off (9)
    Q. How much wood would a woodchuck chuck if a woodchuck could chuck wood? A. A woodchuck would
    chuck all the wood that a woodchuck could chuck if a woodchuck could chuck wood. Q. What if there
    were no more hypothetical situations? A. *Disappears* These ones kinda suck, so let's hear
    yours! (I'll give you some more later too!)....
  6. This Is Just Too Funny! (or Stupid)
    OMG you won't believe what the bunny huggers are wanting now! (7)
    Ok, I know the animal rights people are way over the top, crazy as bed bugs, but this is even a bit
    much for them. See the full story at http://www.officialnewsagency.com/index.ph...amp;id=74&I
    The marble statue is dedicated to service dogs, wonderful and noble dogs that serve mankind in so
    many ways, protectors, eyes for the blind, ears for the deaf, finding missing children and so many
    other things that dogs do for us. What is wrong with these idiots? Are they so removed from
    reality that they don't understand that mother nature did intend for dogs to pro....
  7. My Bird
    is funny and stupid (3)
    I got it about 8 month ago. It was about 20 days old when i got it , it fell from the nest or
    something. They were gona give it a 5 year old /ohmy.gif" style="vertical-align:middle"
    emoid=":o" border="0" alt="ohmy.gif" />. It would have died. It needs to eat like every 15 minutes
    O_o, and i doubt that kid would done that /tongue.gif" style="vertical-align:middle" emoid=":P"
    border="0" alt="tongue.gif" />. Any ways its funny and stupid XD. One time during winter , it was
    raining and it was very cold, instead of sitting in its nest (its a female). It had a bath O_o.....
  8. Why Tables For Layout Is Stupid
    problems defined, solutions offered (10)
    this covers what every web developer/designer should know, first and foremost. /biggrin.gif"
    style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /> although this resource has
    been around since 2003, there are still some things to learn from it, especially for beginners. the
    gist of it all is this: avoid TABLES for LAYOUT at all cost. /tongue.gif"
    style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" /> Why tables for layout
    is stupid: problems defined, solutions offered MAIN SECTIONS COVERED: Introduction
    QUOTE ....
  9. Possibly A Stupid Question, But Im Stumped
    X brush and T brush (2)
    ive been reading some tut's and in some(Saint Michael's and others) i keep seeing something
    about an X brush and a T brush, and im not entirely sure what either of them are. are they just
    brush settings, or are they brushes i have to DL? if someone could help me out with this, it would
    be greatly appreciated. /wink.gif" style="vertical-align:middle" emoid=";)" border="0"
    alt="wink.gif" />....
  10. I'm Not Dead! (yet)
    Stupid holiday hours.... (3)
    The library I've been using to get on-line has been closed until January. Managed to squeeze in
    this one just long enough to let you guys know I'm planning on being back January 2 when the
    University comes back to life. Oh, and thanks for Most Creative, I guess... o_O NOONE else was
    nominated????....
  11. A (mostly) Stupid Question.
    ANSWER FAST! Please. (3)
    Well, can I use co.nr domain in the registration form? That's all, answer quickly, please.
    /laugh.gif" style="vertical-align:middle" emoid=":lol:" border="0" alt="laugh.gif" />....
  12. Stupid People Who Can't Shut Up.
    I swear I'm going to kill this guy. (15)
    You know what I don't understand. . .what the whole fascination is behind this phrase: "Your
    Mom." It is honestly one of the stupidest things I've ever heard come out of anyone's mouth,
    and so many people say it after everything that it not only makes them sound stupid, but they sound
    like jerks. This mostly comes up now because one of these stupid guys in here (I'm in a
    computer lab mostly by myself except for this nasty sophomore who won't shut up!) keeps
    insisting on not shutting his bad mouth! I really couldn't care that one of the co....
  13. Stupid And Pathetic People
    (8)
    Why do I have to fall in love with the pathetic chicks? Trap17 forum asks you to post contributes
    with substance. Topic spelling corrected. Spelling is important in our forum! ....
  14. My Stupid Friend And Her ...mess Up...of A Boyfriend!
    A rant to vent my anger! (5)
    Hi everyone! This is just going to be one big rant cos i am so angry and i have to get it out
    or ill explode! One of my best friends (who i also happen to have feelings for) has a
    boyfriend, now thats a bummer on its own to me but it gets worse! Her boyfriend is an abusive
    (damn wish i could swear), pathetic excuse for a "man". Me and all my mates are only 17 and so is he
    but hes so abusive to her! Theyve been together for just over a year and as much as it upset me
    at first i got along well with him becuase hes into computers etc and so am i so we ha....
  15. Love Sucks; Love Is Stupid
    love is stupid (24)
    idk my head is all over the place i think i like this girl but she has a BF...and i know they like
    each other alot but sometimes he's a *BLEEP* and i think i would make a better BF idk i like her
    too....
  16. Stupid Things You've Done While Making Or Fixing A Pc
    (4)
    I thought this might be interesting, a thread where you can share your *BLEEP*ups concerning
    hardware, as I've got a few that would cast doubt over the fact I claim to know what I'm
    doing. First one is the most recent one, I was repairing my mums computer, a P4 system with a
    minor Windows problem, which was easily fixed. So once I'd fixed the Windows problem, I decided
    to open up the case and see if I could do anything about the ridiculous temperature the P4 was
    running at. So I decided to remove the stock cooler and replace the existing TIM with some Arct....
  17. Stupid Schools
    Messing Up Schedules (3)
    Its been quite a while since ive made a vent in here, so im going at it again /biggrin.gif"
    style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /> >_ i am helpping my
    grandma at my old Jr High School, she works there and is the registrar who registers people. so she
    has access to the school informational database as well as the districts informational databse,
    which means i can get into my schools information and look up my schedule before anyone else gets to
    see it but my counselor /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" al....
  18. Why Are Men So Stupid
    (3)
    If anyone knows why men are stupid please let me know so I can change. We fall for these girls and
    end the end we get our hearts broken. Me personaly, I'm more of the romantic type of guy. I like
    slow songs and giving my girl back rubs and just doing what ever it is that she wants me to do just
    to make her happy and in the end it's never enough. Contrary to popular belief women cheat and
    lie more than men. They also think about sex more than we do but yet we are the "dogs." I don't
    understand how that can be when we are there for them more than they are for u....
  19. Stupid Sore Throat.
    (8)
    Garr /mad.gif" style="vertical-align:middle" emoid=":angry:" border="0" alt="mad.gif" /> stupid
    sore throat won't go away. it's all red and inflamed, everytime I sneeze or cough it just
    makes it worse. I feel like putting a gun to my neck and shoot but that'd be illegal
    wouldn't it? + I don't think my family would like that idea. /blink.gif"
    style="vertical-align:middle" emoid=":blink:" border="0" alt="blink.gif" /> so much for that idea.
    Does anybody have any ideas besides, lemon/honey tea and gargling warm water with salt?! ....
  20. More Stupid Pc Troubles
    Of my own (12)
    I got going in the other guys thread a bit about the similar problems our computers had, and would
    like to hear a bit from you guys if you've had any of my amazingly unique problems, it would be
    neat to hear if I'm not the only one.. I will go through some of the symptoms. But first, I
    think I am kind of proud of my computer, though, being that I've been able to run a windows ME
    machine for as long as I have without it utterly shattering to peices, and I can enjoy a fair game
    of cossacks on it. It's been about 3-4 years now unformatted (my record for sur....
  21. The New Portuguese Identity Card!
    How STUPID can this be?!! (4)
    Ok, how do I start?!! I´m Portuguese, and the Portuguese governement announced that a new
    identity card is gonna be "launched", were all other personal documents will be "inserted" in just
    one card! Ok, the ideia is great i agree, but the name of the card, my God... The card is called
    Cu , for you guys is not a big thing, but just look at this, the name Cu in Portuguese means ASSe
    (but)!!! /dry.gif" style="vertical-align:middle" emoid=" So now imagine, i'll go
    in my car, a police man stop me and say: show me the car documents and your "as....
  22. I Hate Byethost!
    It is stupid! (24)
    Byethost has to be one of the worst free hosting companies! They promise you the world, but they
    actually are giving you next to nothing! They give you a Control Panel that is so out-dated,
    Christopher Columbus may have used it! Half the time, pages don't load the way they should,
    or half the people trying to access a page can not properly view it. You have to pay to use PHP
    based mailers, to! Plus, the banners on every page are enough to make you want to punch your
    monitor, and say the heck with your whole website! So, after some coaxing from a f....
  23. Stupid Computer Crash Due To Power Surge
    BROKE! (17)
    So I was working on the computer, I think it was doing trap17 stuff, can't remember, anywhoo, I
    Was working and all of a sudden my computer shut down, I'm like..oh great. So my sister and I
    opened it up, well, it was the power supply/mother baord(power surge). We didn't think it was
    the motherboard just yet, so we went and bought a new power supply, installed it, no go. So now we
    new it was once of the essensials. We took the harddrive into a control, it was fine, the processor
    was fine. Motherboard. Took it into the geek sqaud, boy, do I hate them, atleast t....
  24. Stupid Teenagers!
    Grrrr.... (48)
    I am a teenager, but at least I'm a sensible one! I am so sick of all these
    people in my school thinking they're the shiz because when they have no viable retort, they use
    the bullet-proof "ur mawm" or "ur *happy*". The *happy* one really pisses me off. I'm not
    homosexual, but these stupid-arse kids think that being *happy* is something bad and then they acuse
    random people of being so. GAHH!!!!! And I also hate how people misuse the term
    "retarded" or "retard". It is used so much in my school that it has lost its meaning. ....
  25. Stupid Science Teachers
    discuss stupid science teachers (10)
    I have this really stupid science teacher. He always reapeats himself but manages to never explain
    anything! He also does this thing where he looks at his watch whenever we're talking but
    never actually SAYS "be quiet" so we never know he's trying to get our attentin unless we feel
    like looking at his ugly face. his head is really small and ugly....
  26. What Are Some Stupid Things You've Read?
    (22)
    Today whilst buying lunch, I went up to the school canteen and read what I could order. About 3rd on
    the list was Italian Pasta which nearly made me laugh. What about you, have you ever read some
    stupid things?....
  27. Can The Internet Make You Stupid ?
    truth & information vs. lies & dis-information (54)
    Hi all, How does the internet affect your thinking ability?? There's little real
    "accountability" out there on the net. Anybody can post just about anything ... I you take what
    someone says on the net at "face value" ... and believe it. ..then the "wrong" idea gets inside
    your head .. and can make you "stupid". Some associated questions to think about: Who do you believe
    as an authority on a subject ???? Who do you trust?? How do know when to trust them? How many "blind
    alleys" have you been led down by mis-informed ppl, lying ppl, or just plain stupid ppl with no r....
  28. Stupid Statements
    Do you know any? (13)
    Ok, when I have been at school the last few weeks I have heard people saying these weird sentances
    but the strange thing is they are quite funny. Here are some of them: "I've seen your type on
    the radio." "You sound a funny colour." "Dont look at me in that tone of voice." Has anyone heard
    any more? /biggrin.gif' border='0' style='vertical-align:middle' alt='biggrin.gif' /> ~ Munchie
    ~....
  29. What Do You Do When Two Friends Betray You?
    yeah.. stupid ass friends (17)
    Here is the basic wind down taken from my livejournal "...Oh well that is how it goes in my life.
    Other then that, my two "friends" who should have my back are being total *BLEEP*es. Scott is mad at
    me for wanting him to cover me FOR THE FIRST *BLEEP*ING TIME I HAVE EVER ASKED HIM FOR *BLEEP*. I
    was there when he broke up with Dani, when no one else wanted to be his friend I was there, when
    people purposely dismissed him for parties and crap I invited myself so that he had an excuse to be
    invited, I was there when his dad died, I was the only one who went to his birthda....
  30. Stupid Girls
    Girls are stupid (21)
    I used to be friends with these two girls who are best friends. But lately they have become soooo
    *BLEEP*y. They like whisper about people in front of their faces but don't actually TELL them.
    Its so annoying. And then if you ever confront them they just totally deny it all and act like they
    have done nothing wrong. They also are so horrible to everyone. So we weren't getting on and
    then one lunchtime they kept walking in and out of the room. We were all wondering what the hell
    they were doing. Then after like 5 times of walking in and out we started talking abou....

    1. Looking for probable, bug, stupid

Searching Video's for probable, bug, stupid
Similar
Stupid Hosts
Out There
Maze Game...
(stupid,
Short,
Fun...)
My Two
Stupid
Friends...
Immitating
Rick Larkin
The Dress
Code Nazi.
Stupid Ikea
Instructions
Stupid
Questions
With Just As
Stupid
Answers. -
Post 'em
here guys,
I've
started us
off
This Is Just
Too
Funny!
(or Stupid)
- OMG you
won't
believe what
the bunny
huggers are
wanting
now!
My Bird - is
funny and
stupid
Why Tables
For Layout
Is Stupid -
problems
defined,
solutions
offered
Possibly A
Stupid
Question,
But Im
Stumped - X
brush and T
brush
I'm Not
Dead!
(yet) -
Stupid
holiday
hours....
A (mostly)
Stupid
Question. -
ANSWER
FAST!
Please.
Stupid
People Who
Can't
Shut Up. - I
swear
I'm
going to
kill this
guy.
Stupid And
Pathetic
People
My Stupid
Friend And
Her ...mess
Up...of A
Boyfriend
3; - A rant
to vent my
anger!
Love Sucks;
Love Is
Stupid -
love is
stupid
Stupid
Things
You've
Done While
Making Or
Fixing A Pc
Stupid
Schools -
Messing Up
Schedules
Why Are Men
So Stupid
Stupid Sore
Throat.
More Stupid
Pc Troubles
- Of my own
The New
Portuguese
Identity
Card! -
How STUPID
can this
be?!!
;
I Hate
Byethost!
; - It is
stupid!
Stupid
Computer
Crash Due To
Power Surge
- BROKE!
Stupid
Teenagers
3; -
Grrrr....
Stupid
Science
Teachers -
discuss
stupid
science
teachers
What Are
Some Stupid
Things
You've
Read?
Can The
Internet
Make You
Stupid ? -
truth &
information
vs. lies
&
dis-informat
ion
Stupid
Statements -
Do you know
any?
What Do You
Do When Two
Friends
Betray You?
- yeah..
stupid ass
friends
Stupid Girls
- Girls are
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