Thread: Please help a noobie out! Loops :(

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    Exclamation Please help a noobie out! Loops :(

    Hey people that are way smarter then me!

    well i just started teaching myself c and I am going through the book "let us c" by Yashavant P. Kanetkar.

    I'm on chapter three which is an introduction to loops and i'm running into a problem which is driving me crazy!

    ok in this program, im trying to use the do-while loop to do a very simple thing.

    Code:
    #include <stdio.h>
    int main()
    {
    
    
        char another;
        do
        {
        printf("hi!\n");
        printf("want me to say hi again? (y/n)\n");
        scanf("%c", &another);
        } while(another=='y');
    
    
        printf("Done.\n");
    
    
        return 0;
    }
    According to the book i should be able to keep entering "y" and it would keep looping indefinitely! But instead it just does it once...and that's it.


    If anyone can please help me it would be greaatttllllly appreciated! haha I know I'm a beginner but even then, I would like to be able to add a basic y/n feature to my programs

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    scanf("%c", &another);
    Try this instead; the space before the "%" say tells scanf to skip space. In this case, it will skip the new line created by pressing enter.
    Code:
    scanf(" %c", &another);
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    Dude! thank you so much, i seriously would have never figured that out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Very noobie question!
    By ewandougie in forum C++ Programming
    Replies: 5
    Last Post: 01-10-2010, 12:45 PM
  2. Some help for a Noobie!
    By barryr in forum C Programming
    Replies: 22
    Last Post: 11-29-2009, 08:59 AM
  3. Noobie Little Help Please
    By Mike1982 in forum C Programming
    Replies: 2
    Last Post: 09-18-2007, 10:34 PM
  4. noobie question!
    By tetra in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2003, 02:58 PM
  5. help me im a C++ noobie!!!
    By fatso in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2002, 10:14 AM

Tags for this Thread