Thread: Trouble with receiving input

  1. #1
    Registered User BellosX's Avatar
    Join Date
    Sep 2001
    Posts
    19

    Angry Trouble with receiving input

    I am having trouble with receiving input from the user in my programs. I have a situation where the user has to input whether or not they are ready to continue, and when it gets to there, it immediately follows on as if they said yes. When i went to debug it, i got my program to print out the variable which contained the input, and i found it was 10. If i'm not mistaken, 10 is the numeric value of the newline character (\n). I was wondering if there is anyway i can bypass my program spitting 10 when there is a scanf() or a getc() / getchar() function.

    Thanks

  2. #2
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    What to do depends on the functions and variables you're using to collect your input. Do you think you could post that part of the code?

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Yes post the code!!!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > If i'm not mistaken, 10 is the numeric value of the newline character (\n).
    This is correct.
    Welcome to the strange world of scanf - a wonderful function for converting user input, but has way too many traps for the unwary.

    The thing you've got to remember about scanf is that it only uses the minimum number of characters possible to satisfy the current conversion requirement (even when it fails).

    If this is your scanf call
    &nbsp; int res = scanf( "%d", &my_int );

    An input buffer of "4\n" would have res=1 (the number of sucessful conversions and assignments), my_int = 4 (the number you typed in), and "\n" left on the input stream.

    An input buffer of "Four\n" would result in res=0, my_int unmodified, and "Four\n" still in the input buffer.

    Code:
    #include <stdio.h>
    
    void flush_input ( FILE *fp ) {
        int ch;
        while ( (ch=fgetc(fp)) != EOF && ch != '\n' );
    }
    
    int main ( ) {
        int res, my_int;
        printf( "Type in a number > " );
        fflush( stdout );   /* the only correct use of fflush is on output streams */
        res = scanf( "%d", &my_int );
        if ( res == 1 ) {
            printf( "You typed in %d\n", my_int );
        } else {
            printf( "That wasn't a number\n" );
        }
        printf( "Press return to finish >" );
        fflush( stdout );
        flush_input( stdin );
        getchar();
        return 0;
    }
    The better way of coping with this is to use fgets and sscanf.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User BellosX's Avatar
    Join Date
    Sep 2001
    Posts
    19
    I've worked it out now, thanks and sorry i didn't respond to your requests i have been very busy lately
    Thanks Salem for your advice!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. still problems with ceasar shift
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 09-14-2008, 01:49 AM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  4. Trouble storing file input in array
    By difficult.name in forum C Programming
    Replies: 1
    Last Post: 10-10-2004, 11:54 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM