Thread: Please help - how to quit a program

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    6

    Please help - how to quit a program

    hello,
    i am making this program. it reads in a number from the user then asks them if they want to enter another number, i want this repeat until the user says no, i used a do while loop but i can't get it working. please help.. the code is as follows:


    thanks for any help
    Code:
    #include <stdio.h>
    
    main()
    {
    
    int temp = 0;
    int temptotal = 0;
    char quit = 'y';
    
    do
    {
    
    printf("Please enter the temperture:\n");
    scanf("%d", &temp);
    
    temptotal = temptotal + temp;
    
    printf("Would you like to enter another temperture (y for yes / n for no)?\n");
    scanf("%c", &quit);
    
    } while (quit != 'n');
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    scanf() tends to leave the '\n' (end of line character) in the input buffer so the 2nd time you use it (to get the 'y' or 'n') it reads the previous '\n'. Take a look at the FAQs (http://faq.cprogramming.com/cgi-bin/...id=1043284385, http://faq.cprogramming.com/cgi-bin/...&id=1043284385) to see how you should do this.
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    6
    thanks for the reply,

    but you kind of lost me, i checked out the faq but it had stuff on reading numbers??

    could you possibly please tell me what i have to do to the code to get in working??

    many thanks

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    6
    if the program prints the message to read in a character n then doesn't wait for u to enter character then ,try this.
    1>
    before using scanf() to read 'quit' ,insert this line
    fflush(stdin);

    OR

    2>
    instead of
    scanf("%c",&quit);
    try
    scanf(" %c",&quit);/*Note the space before %c*/

    This problem arises because after typing the temperature,you have to press enter n that enter is stored in the buffer n is availbale for the next scanf() function.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    6
    if the program prints the message to read in a character n then doesn't wait for u to enter character then ,try this.
    1>
    before using scanf() to read 'quit' ,insert this line
    fflush(stdin);
    2>
    instead of
    scanf("%c",&quit);
    try
    scanf(" %c",&quit);/*Note the space before %c*/

    This problem arises because after typing the temperature,you have to press enter n that enter is stored in the buffer n is availbale for the next scanf() function.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    6
    THANK YOU SO MUCH I GOT IT WORKING

    i used the first option
    Ashkan

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Mahesh Herle B
    if the program prints the message to read in a character n then doesn't wait for u to enter character then ,try this.
    1>
    before using scanf() to read 'quit' ,insert this line
    fflush(stdin);
    NO NO NO NO!!! fflush() is not defined for input devices in Standard C -- on many compilers your code will either not work or cause errors. Read the FAQ Salem points to.

    OR

    2>
    instead of
    scanf("%c",&quit);
    try
    scanf(" %c",&quit);/*Note the space before %c*/

    This problem arises because after typing the temperature,you have to press enter n that enter is stored in the buffer n is availbale for the next scanf() function.
    I haven't tried this (it's somewhat kludgy), but a sure solution is to use
    fgets(quit, 1, stdin);

    and change the first scanf to
    Code:
    include <stdlib.h>
    char buf[10];
    ...
    fgets(buf, 10, stdin);
    temp = atoi(buf)


    This will always work the way you want it.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    6
    Thanx for the guidance, to WaltP and Salem.

    I think, at least, my second option is correct. As that will cause all
    the white spaces ( blank space, carriage return, tab, etc ) to be ignored. I use Turbo C++ compliler v 3.0 ( DOS environmment ),
    as i did not get any other compliler for windows environment .( i dont know much about programming in windows...).

    I studied the above, from the above said compiler's help pages...

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Originally posted by Mahesh Herle B
    I think, at least, my second option is correct. As that will cause all the white spaces ( blank space, carriage return, tab, etc ) to be ignored.
    While it may work, it might be considered a bad habit to learn. For scanf, I like to read Chris Torek's explanations (like this one -- particularly noting the last paragraph). Often, he goes into detail on why it is simply better to avoid scanf. YMMV.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Press "Enter" key to quit the program
    By Vitamin_C in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2005, 08:25 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. Error when trying to quit program...
    By marCplusplus in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2002, 07:03 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM