Thread: 'cin' and 'scanf()' issue

  1. #1
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472

    'cin' and 'scanf()' issue

    I just started learning C++ lately and im experiencing the same problems of 'scanf()' with 'cin' (well i consider them as problems).

    'cin' doesn't read characters it can't , and leaves them in the stream just like 'scanf()' with the newline character for example , but i can get rid of the '\n' with no problems so i don't care much about that.

    What i care about is , in the following program :
    Code:
    ...
    int num;
    while(1)
    {
       cout << "Enter a number : ";
       cin >> num;
    
       if(num == -1) break;
    }
    ...
    if i enter any character instead of a number , the program would loop infinitely at that point and "Enter a number : " would be all over the screen !!

    I'm guessing that when cin\scanf() can't read a character it leaves it in the input stream , then at the next attempt it would try to read it again but still can't and so on... am i guessing right? if not , then whats causing the problem?
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    cin's >> operator has the same problems as scanf, and your guesses are correct.
    My best code is written with the delete key.

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    sorry i forgot to ask why this wouldn't eat the characters left in the stream like it does with the newline character (ch is 'char'):
    Code:
    ...
    cin >> integer;
    ch = cin.get();  // this is suppoed to eat the first character left there
    ...
    but the program skips the second line as if cin.get() reads the '\n' first , even with multiple variables with cin.get() they would be all skipped.

    Why did that happen and how would i clear the stream from ANY character(s) left in there?


    thanks for the replying Prelude
    Last edited by Brain Cell; 11-07-2004 at 10:14 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >// this is suppoed to eat the first character left there
    It would if the stream weren't in an error state. Use cin.clear() before trying to read and discard unwanted characters.
    My best code is written with the delete key.

  5. #5
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    k thanks alot man
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf in c++
    By herWter in forum C++ Programming
    Replies: 5
    Last Post: 04-06-2009, 10:48 AM
  2. char pointer working in scanf but not in cin.
    By sawer in forum C++ Programming
    Replies: 14
    Last Post: 06-15-2006, 02:15 AM
  3. Cin & Cout VS. Scanf & Printf
    By MatriXxX in forum C Programming
    Replies: 19
    Last Post: 08-08-2003, 11:47 AM
  4. cin / scanf
    By evilmonkey in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2001, 03:13 PM