Beginner problem

This is a discussion on Beginner problem within the C++ Programming forums, part of the General Programming Boards category; Hi I am a beginner, I am using 2 cin statements, one after the other. However, the program does not ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Beginner problem

    Hi I am a beginner,

    I am using 2 cin statements, one after the other. However, the program does not stop to wait for an input from the second cin statement.

    How do I overcome this, and,

    more importantly why is this happening?

    Thanks

    Code:
         
                    cout << "Please enter first name: "l;
                    cin >> name; 
    	cout << "Please enter your name: ";
    	cin.getline(name1,14);

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    You've gotta put a cin.ignore() before the getline() and after the first cin

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> You've gotta put a cin.ignore() before the getline() and after the first cin
    I prefer to phrase it the other way... put the cin.ignore() after the call to cin >>. It won't harm anything if you put it after every call to cin >>, but if you go the other way and put it before any call to getline then you might end up cutting off a letter if getline is called twice in a row.

    Also, strokebow, why are you using C-style strings? Were you aware that you were? C++ has a string class that makes handling text much easier. If your book/tutorial/instructor is teaching you to use character arrays for strings, you might want to get a more modern teacher.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    thanks for your replies.

    1. Why do I need to use this cin.ignore()?
    2. How would you deal with strings?

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Quote Originally Posted by Daved View Post
    >> You've gotta put a cin.ignore() before the getline() and after the first cin
    I prefer to phrase it the other way... put the cin.ignore() after the call to cin >>. It won't harm anything if you put it after every call to cin >>, but if you go the other way and put it before any call to getline then you might end up cutting off a letter if getline is called twice in a row.

    Also, strokebow, why are you using C-style strings? Were you aware that you were? C++ has a string class that makes handling text much easier. If your book/tutorial/instructor is teaching you to use character arrays for strings, you might want to get a more modern teacher.
    Code:
    cout << "Please enter your name: ";
    	cin >> name; 
    	cin.ignore();
    	cout << endl;
    	//fflush(stdin);
    	cout << "Please enter your name: ";
    	cin.ignore();
    	cin.getline(name1,14); //Allows acceptation of chartacters inc spaces
    	cin.ignore();
    Hey, Thks for your suggestions but it aint hapenin for me.

    I enter the name "Tom Store"

    Output

    'name1' = "tore"

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main( void ) {
      string name1, name2;
      // If not using namespace std use std::string here
    
      cout << "Please enter your name: ";
      cin >> name1; 
      cin.ignore();
    
      cout << endl;
      cout << "Please enter your name: ";
      // As Daved said only .ignore() after every cin<< 
      //cin.ignore();
    
      getline( cin, name2 ); 
      // I prefer this method
    
      cout<< "name1: " << name1 << "\nname2: " << name2;
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> 1. Why do I need to use this cin.ignore()?
    Because when you use operator >> to read from cin a newline character is left in the stream. The user types in their answer to your question and they hit <enter>. That places a newline character at the end of their input. cin >> reads in the input but not the newline character.

    Later, when getline tries to read a line it stops as soon as it finds a newline. The leftover newline from the previous read stops it immediately.

    To solve the problem, you can add a cin.ignore() after you call cin >>. This will ignore the trailing newline. Do not do it before the call to getline because you don't know if there is a newline in the stream at that point. Only do it after calls to cin >>.

    >> 2. How would you deal with strings?
    I would use C++ strings like in twomers example.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Quote Originally Posted by twomers View Post
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main( void ) {
      string name1, name2;
      // If not using namespace std use std::string here
    
      cout << "Please enter your name: ";
      cin >> name1; 
      cin.ignore();
    
      cout << endl;
      cout << "Please enter your name: ";
      // As Daved said only .ignore() after every cin<< 
      //cin.ignore();
    
      getline( cin, name2 ); 
      // I prefer this method
    
      cout<< "name1: " << name1 << "\nname2: " << name2;
      return 0;
    }
    thanks again for your replies.

    I copied your code and built and ran it.

    It did not work 'properly'. Unless, this was the outcome you wanted.

    This is the output I got:

    Please enter your name: si m

    Please enter your name: name1: si
    name2: mPress any key to continue . . .
    Another point. I am using VS 2005 express. The 'string' variable type doesnt show up as a keyword (ie, blue). Why is this?

    Thanks

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    cin>> looks at the input until it sees whitespace, which is the space between si and m. The ignore ignores the space and the m is getline-d. Try it the other way around, i.e. first a word, then <return> then a string of words.

    >> The 'string' variable type doesnt show up as a keyword (ie, blue). Why is this?
    Because it's not a base type by itself. It's a character class.

    Edit: My output:
    Code:
    Please enter your name: ThisIsOneWord
    Please enter your name: This Is Many Words
    
    name1: ThisIsOneWord
    name2: This Is Many Words
    Last edited by twomers; 09-12-2008 at 10:58 AM.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Excellent.

    Many thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. very basic beginner problem
    By sandingman1 in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 04:48 PM
  3. Beginner Problem
    By knoxmaddog in forum C++ Programming
    Replies: 13
    Last Post: 11-21-2005, 10:16 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  5. beginner problem
    By laasunde in forum C Programming
    Replies: 0
    Last Post: 11-21-2002, 07:24 AM

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