Thread: Do - While Problem

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    10

    Do - While Problem

    Hello maybe this is easy question but i couldn't why it happened.
    Here is my code.
    [CODE]#include <stdio.h>
    #include <conio.h>
    int main(){
    char x;
    do{
    printf("?: ");
    scanf("%c",&x);
    if(x=='s')
    printf("open\n");
    else
    printf("closed\n");
    }
    while(x!='s');
    getch();
    return 0;

    }CODE]
    I started exe and it gave this.

    Code:
    ?: a
    closed
    ?: closed/* makes this one for 2 times. it's not scaning in this part.*/
    ?: a
    closed
    ?: closed
    ?: s
    open
    but i was excepting that it will give like this

    Code:
    ?: a
    closed
    ?:a 
    closed
    ?: a
    closed
    ?:a 
    closed
    ?: s
    open
    it's very simple code. but i couldn't find why this happens. i'm waiting your reply a.s.a.p thx

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    scanf is leaving your linefeed in the buffer, so the next scanf reads in the linefeed as a character.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    10
    so how can i make this . can you write true code?

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    10
    is there someone that can help me pleasE?-

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Instead of begging for someone to write your code and bumping your thread, why don't you take some initiative and do some searching based on what cwr told you.

    You need to find a way to remove the newline character from the buffer before the next scanf().
    Sent from my iPad®

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Ideally, read the FAQ and learn to use fgets() to read a whole line, then use sscanf() (or whatever else you like) to parse the line.

    Using scanf() properly takes way too much effort.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    10
    SlyMaelstrom
    i'm not begging i wanted to show my post only. if you don't know how to do it don't write something to topic and don't bump it. i only asked whether someone knows it . if i'm begger all the people in the forum who wants help are begger isn't it true?
    so i only wanted to learn it .

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I posted 9 minutes after you, jackass, that's not bumping it.
    ...and to say that I'm not helping you cause I'm not gonna write the code is rediculously ignorant. Cwr told you what you had to do. HE TOLD YOU. ...but that's not enough, you want someone to write the exact code for you so you can copy and paste it to your program and tell all your friends that you're some 1337 programmer. I would have even given you a few examples of how to do it if you didn't bump it in the first place. Your name is officially noted as a person I never plan on helping. You redefine unappreciative.
    Sent from my iPad®

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM