getline(cin, zips) skips fist input before a while loop in function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • m3zmeriz3d@aol.com
    New Member
    • Jul 2006
    • 1

    getline(cin, zips) skips fist input before a while loop in function

    i typed a function to enter data into a file to be recalled later, now i have done something here and as soon as this function begins it skips the first 'zips' getline and makes it a blank space and goes directly to 'city' - someone please tell me what the hell happenned here - g'day - mark


    void addData()
    {
    string zips = "";
    string city = "";

    ofstream outFile;

    outFile.open("c itiesandzips.tx t");

    if(outFile.is_o pen())
    {
    cout << "Enter ZIP Code (X to stop): " << endl; ///this is my problem - this bastard doesnt take data it automatically fills out as a blank space
    getline(cin,zip s);
    transform(zips. begin(), zips.end(), zips.begin(), toupper);


    while (zips != "X")
    {
    cout << "Enter City: " << endl;
    getline(cin,cit y);

    outFile << zips << '#' << city << endl;

    cout << "Enter ZIP Code (X to stop): ";
    getline(cin,zip s);

    transform(zips. begin(), zips.end(), zips.begin(), toupper);

    }
    outFile.close() ;
    }
    else
    cout << "The file could not be opened." << endl;
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Shouldn't this

    getline(cin,zip s);

    be this

    cin.getline(zip s, sizeof zips);

    Comment

    Working...