getline statement

This is a discussion on getline statement within the C++ Programming forums, part of the General Programming Boards category; In the function at the bottom, I need a getline statement to accomodate the cin statement so that we can ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    16

    getline statement

    In the function at the bottom, I need a getline statement to accomodate the cin statement so that we can enter a string of text with spaces in it for "hole_type". I ran the program and after entering any string for an answer, the program skips the rest of the inputs and prints out the rest of the program.

    What can you do for me?


    Code:
    #include<iostream.h>
    #include<cstring.h>
    #include<conio.h>
    using namespace std;
    
    
    struct GolfHole
    {
    	char hole_type[20];
    	int yardage,
    	    par,
    	    hole_number;
    
    	GolfHole(char hole_type[20] = "dog-leg right")
    	{
                    hole_type = "dog-leg right";
    	        yardage = 254;
    	        par = 4;
    	        hole_number = 1;
    	}
    };
    
    GolfHole get_golf_hole_info();
    
    void main()
    {
    	GolfHole HoleA;
    
    	HoleA = get_golf_hole_info();
    
    	cout<<"\n\nThe hole number is:"<<HoleA.hole_number<<".\n";
    	cout<<"\n\nThe hole-type is:"<<HoleA.hole_type<<".\n";
    	cout<<"\n\nThe hole is "<<HoleA.yardage<<" yards long.\n";
    	cout<<"\n\nThe par for this hole is:"<<HoleA.par<<".\n";
    
    
    	GolfHole HoleB;
    
    	HoleB = get_golf_hole_info();
    
    	cout<<"\n\nThe hole number is:"<<HoleB.hole_number<<".\n";
    	cout<<"\n\nThe hole-type is:"<<HoleB.hole_type<<".\n";
    	cout<<"\n\nThe hole is "<<HoleB.yardage<<" yards long.\n";
    	cout<<"\n\nThe par for this hole is:"<<HoleB.par<<".\n";
    
            getch();
    
    }
    
    
    GolfHole get_golf_hole_info()
    {
    	GolfHole info;
    
    	cout<<"\nWhat is the number of this hole?: ";
    	cin>> info.hole_number;
    
    	cout<<"\nDescribe the type of hole you'd like: ";
    	cin.getline(info.hole_type, 20);                            //<-right here.
    
    	cout<<"\nHow long is the hole in yards?: ";
    	cin>> info.yardage;
    
    	cout<<"\nWhat is the par for this hole?: ";
    	cin>>info.par;
    
    	return info;
    }
    Last edited by Troll; 10-14-2005 at 08:56 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    This is a common problem. When you use cin >>, it leaves the newline in the stream from when the user presses <enter>. Then you call getline, which reads until the first newline, so it stops immediately at the leftover newline from the previous cin>> call.

    Add cin.ignore() after using cin >> and before calling getline() or get(). You could also just always call cin.ignore() after cin >>, it shouldn't cause any other problems.

    BTW, <iostream.h> is an outdated and non-standard header that doesn't work on some modern compilers. You should switch to <iostream>. Also, <cstring.h> is wrong, it should be <cstring> (but you aren't using it yet anyway). Finally, you are assigning C style strings, which is not correct. You should switch to the C++ string in <string>. If you have to use C style strings, use strcpy (in <cstring>) to copy data into it (e.g. in your constructor).
    Last edited by Daved; 10-14-2005 at 09:22 PM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    I don't quite know how to correct my code with your suggestion.

    "Add cin.ignore() after using cin >> and before calling getline() or get(). You could also just always call cin.ignore() after cin >>, it shouldn't cause any other problems."

    could you quickly write a few statements to make everything clear?

    Code:
    GolfHole get_golf_hole_info()
    {
    	GolfHole info;
    
    	cout << "\nWhat is the number of this hole?: ";
    	cin >> info.hole_number;
    
    	cout << "\nDescribe the type of hole you'd like: ";
    	cin >> info.hole_type;
            cin.ignore();                     //This gives me the same result
    
    	cout << "\nHow long is the hole in yards?: ";
    	cin >> info.yardage;
    
    	cout << "\nWhat is the par for this hole?: ";
    	cin >> info.par;
    
    	return info;
    }

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Ok, I fiddled with it a bit and this seems to work. (which follows your advice)

    Thank you very much!

    Code:
    GolfHole get_golf_hole_info()
    {
    	GolfHole info;
    
    	cout << "\nWhat is the number of this hole?: ";
    	cin >> info.hole_number;
            cin.ignore();
    
    	cout << "\nDescribe the type of hole you'd like: ";
    	cin.getline(info.hole_type, 20);
    
    	cout << "How long is the hole in yards?: ";
    	cin >> info.yardage;
    
    	cout << "\nWhat is the par for this hole?: ";
    	cin >> info.par;
    
    	return info;
    }
    Last edited by Troll; 10-14-2005 at 10:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 06:19 AM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 12:12 PM
  4. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

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