Thread: getline problem

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    getline problem

    I dont know if the problem is the way I use getline.
    If I run this little program, it is supposed to read a string, a number...5 times, but it does not, after the second atepmt,
    the program goes crazy.
    I tried a fix I was given (STLFix), and still doesn't work,
    I found an article in microsoft to solve the getline bug, but
    I can't find where to fix it, how do I get to the header to
    change it ?
    http://support.microsoft.com/default...;EN-US;q240015

    anyway, this is the little program, it is getline or the program
    the one that doesn't work?
    Code:
    #include<iomanip>
    #include<iostream>
    #include<string>
    using namespace std;
    
    main()
    {
    	string str;
    	int num;
    	for(int i=0;i<5;i++){
    		cout<<endl<<"String ";
    		getline(cin, str, '\n');
    		cout<<"Number ";
    		cin>>num;
    	}
    	return 0;
    }
    thank you if you guys can be VERY explicit with your guidelines.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Well, first of all, main is defined to return an int. So, it's int main not just main.

    Try adding a cin.clear() and a cin.ignore(80, '\n'). This will clear the error state and ignore any left over characters in the input buffer. I'm assuming that the program is skipping the input. Is this correct?

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    yes

    yes, it pretty much does it, thank u, the cin.clear doesn't
    seem to affect the program though. Only one thing, the
    first time I still have to press enter twice.

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

    Re: getline problem

    Originally posted by jriano
    I dont know if the problem is the way I use getline.
    If I run this little program, it is supposed to read a string, a number...5 times, but it does not, after the second atepmt,
    the program goes crazy.
    I tried a fix I was given (STLFix), and still doesn't work,
    I found an article in microsoft to solve the getline bug, but
    I can't find where to fix it, how do I get to the header to
    change it ?
    http://support.microsoft.com/default...;EN-US;q240015

    anyway, this is the little program, it is getline or the program
    the one that doesn't work?
    Code:
    #include<iomanip>
    #include<iostream>
    #include<string>
    using namespace std;
    
    main()
    {
    	string str;
    	int num;
    	for(int i=0;i<5;i++){
    		cout<<endl<<"String ";
    		getline(cin, str, '\n');
    		cout<<"Number ";
    		cin>>num;
    	}
    	return 0;
    }
    thank you if you guys can be VERY explicit with your guidelines.
    It's on line 165 (god why do I remember this) of the string header file. Change snextc to sbumpc. You can easily open the file by clicking on the #include <string> directive and selecting "open document string" or something similar.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. getline() problem
    By mrafcho001 in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2005, 01:16 AM
  4. Need help with structures
    By yoursport in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2005, 11:59 AM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM