Problems inputting char and strings

This is a discussion on Problems inputting char and strings within the C++ Programming forums, part of the General Programming Boards category; Hi everybody, I am trying to input a character and right away an array of characters... However once i input ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    25

    Problems inputting char and strings

    Hi everybody,
    I am trying to input a character and right away an array of characters...
    However once i input the character, it doesnt let me input the array character and the program ends...

    Consider this:

    Code:
      char grade[4] ={NULL};
    	cout << "Enter grade: ";
    	grade[0] = cin.get();  // I am inputting character here but...
    	 
    	char a[100] = " ";
    	cout << endl << "Enter name: ";   // ...here it just displays  message...
    	cin.getline(a, 80, '\n');   // program ends without letting me input
    Any suggestions?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Yeah, read what get actually does. It doesn't handle the newline character for you, which will make all line-buffered methods terminate quickly.

    istream::get - C++ Reference

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    At the cin.get() line, you enter a character and then press enter, the program will not progress until you press the enter key. So... that is at least two characters in the input buffer. The get command processes and stores the first character into grade[0] but anything else remains in the input buffer to be processed at a later time. When your code reaches the getline command there is still that leftover data in the buffer which at the very least will include a newline character. The getline command will then happily process whatever is left in the input buffer which has the side-effect of your code seeming to skip allowing the user to input the name.

    The solution to this is to make sure the input buffer is cleared of extraneous input prior to the getline call. There are many ways of doing this, one of the most common would be to call the input stream's ignore() member function after the get() call. This will throw away data in the input buffer so that it is cleared before you reach the getline command which should then behave as expected by waiting for user input.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Btw, NULL is a deprecated use reserved for declaring a null pointer. It is not supposed to be used for char arrays.
    (And just for reference, it the future, a null pointer will be declared by assigning nullptr.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-28-2011, 08:14 PM
  2. Problems coping a char to a char using strcpy
    By iKlys++ in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2011, 06:39 PM
  3. Inputting strings from a file
    By Mystic_Skies in forum C Programming
    Replies: 7
    Last Post: 11-17-2004, 04:08 PM
  4. Inputting a string and a char
    By Drew in forum C++ Programming
    Replies: 2
    Last Post: 09-10-2003, 07:40 PM
  5. Inputting Strings from a file
    By Drealoth in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2003, 11:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21