string input problem

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

string input problem

Postby lap » Sun Oct 12, 2003 11:18 pm

Code: Select all
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string address;

    cout << "Enter you address" << endl;

    getline(cin,address,'\n');
        //with a delimiter specified
        //getline(cin,address,'\n');

    cout << "You are residing at " << address << endl;

    return 0;
}


this is a piece of code i got from http://cplus.about.com/library/weekly/aa030702a.htm.

When I tried out this code, I entered:

555 Elm St.

then hit enter.
Nothing happens after i hit the first enter.
I hit enter again for a 2nd time...
the output finally comes out: " You are residing at 555 Elm St."


How do i fix it so that I only have to press enter once? I tried specifying the delimeter to be \n but same results occurred.
lap
 

Postby jgbauman » Mon Oct 13, 2003 3:49 am

The code work perfectly with g++ 3.3.1. What compiler (and standard library) do you use? VC++ had a bug in getline in an older version.
User avatar
jgbauman
 
Posts: 358
Joined: Sat Sep 27, 2003 9:00 am

yep..

Postby lap » Mon Oct 13, 2003 4:33 am

just found out that visual c++ 6.0 has a bug with getline...how annoying :evil:
lap
 

Postby Tracer » Mon Oct 13, 2003 4:58 am

wtf? How homosexual is that? This must be rectified immediately... :shock:

P.S. Is there anyway to "fix" this, or like, some kind of workaround?
(C++ TotalNewB)

if(!die(self));
self++;
User avatar
Tracer
 
Posts: 124
Joined: Wed Oct 08, 2003 4:07 am

Fix for getline

Postby OgreCoder » Tue Oct 14, 2003 3:13 pm

I remembered doing this fix several years ago as a student so I found it again. Here is what you have to do:

getline()

The getline function that is supplied in the string library requires the user to hit enter twice (not fun). This can be fixed by editing line 165 of the file: c:\Program Files\Microsoft Visual Studio\VC98\Include\STRING

Line 165 should be:

_I.rdbuf()->sbumpc();

instead of:
_I.rdbuf()->snextc();
OgreCoder
 


Return to For Beginners

Who is online

Users browsing this forum: No registered users and 2 guests