GetLine

This is a discussion on GetLine within the C++ Programming forums, part of the General Programming Boards category; I'm using MSVC++ and am getting this problem: I'm asking the user to enter a string that may or may ...

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    GetLine

    I'm using MSVC++ and am getting this problem: I'm asking the user to enter a string that may or may not contain a space using the following code:
    Code:
    	string city, team;
    	cout<<"What do you want the city name to be? --> ";
    	getline(cin, city);
    	cout<<"What do you want the team name to be? --> ";
    	getline(cin, team);
    The problem is that the user has to press the enter key twice and I'm not sure how to avoid that. I've tried using cin.sync() and cin.ignore(100, '\n') before and after the getline, but they don't work. The program works fine, whatever the user enters before the first enter goes into the string, and nothing is recorded if anything is entered BETWEEN the first and second enter. Anyone know what I can do to fix this?

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    use char.

    Code:
    #include<iostream>
    #include <conio.h> //for my compiler
    using namespace std;
    int main()
    {
    char city[100]; //max of 100 character for city
    char team[100]; // max of 100 team characters
    
    cout<<"What do you want the city name to be? --> ";
    cin.getline(city,sizeof(city));
    cout<<"What do you want the team name to be? --> ";
    cin.getline(team,sizeof(team));
    
    getch(); // for my compiler
    return 0;
    }
    try it , i did and it works
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Thanks, but I'm trying to do it without char arrays and instead using strings. Number of reasons for this, most notably because its for a big project spanning 15 files that is 99% written and I don't want to go back and change everything!

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Ahhhh, I had this probelm once too.

    Check in this
    STL question: getline

    and se what Prelude says.

    Haha a bug!!! Make a search on google and you will find the answer. If I remember it right you have to modify a header-file .
    Last edited by ripper079; 11-11-2002 at 02:49 PM.

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Thanks, I'll do that! Geez, between this and the friend bug, I'm getting tired of all these MSVC++ errors!

  6. #6
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Thanks for the help! Bug has been fixed!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 05:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 12:12 PM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM

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