Thread: stuck with getline() function

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    55

    stuck with getline() function

    below are a part of my program ... the problem here is ... when i run the 1st option (1. Open an account) ... it display my cout ... but skipped my cin ...

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    class acc
    {
    	public:
    		void initial();
    		void deposit();
    		void withdraw();
    		void display();
    
    	private:
    		string name;
    		int accno;
    		string type;
    		float balance;
    };
    
    void acc::initial()
    {
    	cout << "Name: ";
    	getline(cin, name);
    	cout << "Account No: ";
    	cin >> accno;
    	cout << "Account type: ";
    	cin >> type;
    	cout << "Initial amount: ";
    	cin >> balance;
    }
    
    void acc::deposit()
    {
    	float dep;
    	cout << "Deposit: ";
    	cin >> dep;
    	balance += dep;
    }
    
    void acc::withdraw()
    {
    	float draw;
    	cout << "Withdraw: ";
    	cin >> draw;
    	if(balance < draw)
    		cout << "Insufficient amount in your balance" << endl;
    	else
    		balance -= draw;
    }
    
    void acc::display()
    {
    	cout << "Name: " << name << endl;
    	cout << "Account No.: " << accno << endl;
    	cout << "Account Type: " << type << endl;
    	cout.setf(ios::fixed);
    	cout << "Balance: " << setprecision(2) << balance << endl;
    }
    
    int main()
    {
    	system("color 1F");
    
    	acc x;
    	int option;
    
    	while(true)
    	{
    		cout << "\t\t\t1. Open an account" << endl;
    		cout << "\t\t\t2. Deposit money" << endl;
    		cout << "\t\t\t3. Withdraw money" << endl;
    		cout << "\t\t\t4. Show info" << endl;
    		cout << "\t\t\t5. Exit\n" << endl;
    		cout << "\t\t\tChoose an option: ";
    		cin >> option;
    
    		switch(option)
    		{
    			case 1:
    				cout << "\n";
    				x.initial();
    				system("pause");
    				system("cls");
    				break;
    
    			case 2:
    				cout << "\n";
    				x.deposit();
    				system("pause");
    				system("cls");
    				break;
    
    			case 3:
    				cout << "\n";
    				x.withdraw();
    				system("pause");
    				system("cls");
    				break;
    
    			case 4:
    				cout << "\n";
    				x.display();
    				system("pause");
    				system("cls");
    				break;
    
    			case 5:
    				exit(1);
    		}
    	}
    
    	return 0;
    }
    anyone mind to explain to me ?

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    try this:
    Code:
    void acc::initial()
    {
    	cout << "Name: ";
            cin.ignore(1);     //skips what's left in the input stream
    	getline(cin, name);
    	cout << "Account No: ";
    	cin >> accno;
    	cout << "Account type: ";
    	cin >> type;
    	cout << "Initial amount: ";
    	cin >> balance;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    why add ignore function ? mind to explain ? i am still new to C++

    Code:
    cin.ignore(1);
    beside that, is there any other way to replace/modfy the getline function so

    Code:
    getline(cin, name);
    i don't have to press enter TWICE after i entered the value for the variable name. Instead, i just have to press enter ONCE like i enter values for other variables.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    isnt thier suppose to be a period in getline?

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    The press enter twice thing might be a bug in the STL implementation used by Visual C++ 6.0. Check out this site and scroll down to the part about <string>

    http://www.dinkumware.com/vc_fixes.html

    or look at this one

    http://support.microsoft.com/default...;en-us;Q240015

    Once you apply their fix, you can use getline like you wanted to.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    sure, don't use >> in the same program as getline(). Otherwise you can't avoid the problem. The fix isn't all that hard. Once you have enough experience and learn how >> and getline() actually work under the hood you'll be better able to appreciate the reason this is as it is. For now, just remember that if you use getline() you should (probably) call ignore() just before the call to getline(), especiallly when using >> and getine() in the same program. As an aside, I might use a different set of parameters for ignore() than was suggested, but that's neither here nor there right now. The point is use the combination of ignore() and getline().

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    instead of using getline() function, is there any other way to input a line of characters into string ?

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    Code:
    	else if (_Tr::eq((_E)_C, _D))
                    {_Chg = true;
                  //  _I.rdbuf()->snextc(); /* Remove this line and add the  line below.*/ 
    		  _I.rdbuf()->sbumpc();
                    break; }
    i opened string.h in the include folder but i cant find the code as mentioned above ....

    mind telling me how to fix the bug ?

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Originally posted by stalker
    i opened string.h in the include folder but i cant find the code as mentioned above ....

    mind telling me how to fix the bug ?
    The file is string, not string.h. If you still can't find it then maybe you don't have that bug (which would be strange if you are using VC++ 6.0). Search for getline in the file, and then search for snextc in that function.

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    thanks jlou ....... i found it ~

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    thanks elad for your help ~

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by stalker
    instead of using getline() function, is there any other way to input a line of characters into string ?
    yeah, but it stops at whitespaces...
    Code:
    cin>>fname>>lname;
    cout<<fname<<' '<<lname;
    //input: My Name
    //output: My Name
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    25
    Getline under the hood

    Problems with getline(cin, name);

    Since getline does not ignore leading whitespace characters, you should take special care when using it in conjunction with cin >>.

    The problem: cin>> leaves the newline character (\n) in the iostream. If getline is used after cin>>, the getline sees this newline character as leading whitespace, thinks it is finished and stops reading any further.

    Program fragment with problem:

    string name;
    int age;

    cout<<"Enter your age";
    cin>>age;
    cout<<"Enter your full name";
    getline(cin, name);

    cout<<name<<", you are "
    <<age<<endl;
    OUTPUT:
    Enter your age 5
    Enter your full name Ben Bunny
    , you are 5
    The name did not print because the getline saw the newline character left from the cin>> as whitespace and stopped reading any further.

    So , bottom line you have to learn when things are left in the iostream.........& when they're not.
    In addition to cin.ignore ...you could also use flush or endl..........both of these flush the \n out of the iostream........
    Hope that helps to understand why...........

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    flush and endl clear the ostream buffer, but not the istream buffer.

  15. #15
    Registered User
    Join Date
    Aug 2003
    Posts
    25

    Arrow

    elad
    My Bad ..Thanks for straighting that out..........I learn something everyday around here........ ....
    Is cin.ignore the only way to deal with using cin in conjunction with getline then?
    Last edited by teeyester; 10-24-2003 at 07:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM