What am I not understanding about getline+strings?
Asked Answered
C

1

11

This is my first time using stackoverflow. I've been unable to find out the information I need regarding getline. I'm in a simple programming class for engineering transfers so the code that we write is pretty simple. All i'm trying to do here is put a user-defined number of questions and answers into two different arrays. My while loop looks like this (I was using a for loop but switched to while just to see if it would stop breaking):

int main ()
{
    srand((unsigned)time(0));
    string quest1[100], answ1[100];
    int size1, x = 0, num, count1, visit[100], shuffle[100];
    fstream flashcard1; 

    cout << "flashcard.cpp by NAME\n" << endl;
    cout << "This program allows user to manipulate questions and answers for studying.\n" << endl;
    cout << "\nHow many flash cards will be entered(MAX 100)? ";
    cin >> size1;
    cout << endl;

    while(x < size1)
    {
        cout << "Enter Question: ";
        getline(cin , quest1[x]);
        cout << endl;
        x = x++;

        /*
        cout << "Enter Answer: " << endl;
        getline(cin,answ1[x]);
        cout << endl;
        flashcard1.open("flashcard1.dat", ios::app);
        flashcard1 << quest1[x] << " " << answ1[x] << endl;
        flashcard1.close();
        cout << "Data Stored." << endl;
        */
    }
}

I noted out the answer entering part as well as the saving data to the file just for debugging. When I run the program it skips the getline for the first question, displays the second loop of "Enter question" and the getline works for the rest of them. So if I have a size1 of 5, the program only fills array positions 1-4. Please help. This is a simple flash card program that will do the same thing as if you were to create flash cards to study and shuffle them.

Carnarvon answered 23/11, 2011 at 19:51 Comment(6)
x = x++; is Undefined Behaviour. It should just be x++ (or ++x, or x += 1, or x = x + 1, or x -= -1....)Pickard
That's because x++ and ++x both change x itself by in/decrementing its value by 1. You can't guarantee that the assignment happens before or after this though, so x could be assigned the value of x++ before the increment happens or after it happens (the standard just doesn't specify this).Referendum
+1 for a good First Question on SO.Expressway
Welcome to Stack Overflow. +1! This is off-topic to your question, but 1) Please don't use endl when you mean '\n'. std::cout << std::endl is precisely equivalent to std::cout << '\n' << std::flush. 2) Never say "using namespace std;", ever, even if (especially if) your book or professor tell you to. Importing the entire std namespace into your program creates hard-to-identify bugs.Firooc
@Rob I think that's a huge exaggeration about using namespace std;. As long as you know what the dangers are, you can do it in very controlled environments (like one-file short programs for example).Pickard
@all thanks for all the tips. I'm taking my first computer science class next semester. This simple C++ class for engineering was my first dive into programming and I really enjoy it. I'm going to pursue computer science as a major if I like the CS class next semester. As far as my code goes regarding what Rob said. I don't even know what std::cout does. I don't think that this teacher is very good with C++, or he just wants to make the class as simple as possible. For lessons he just gives us ppt files which teach us 2-5 new C++ codes per class. Anyway thanks for all the help guys!Carnarvon
P
13

The reason it's appearing to skip the first iteration is because when you do

cin >> size1;

You enter a number and hit the Enter key. cin reads the integer and leaves the newline character unread on the buffer, so that when you call getline, it's as if you immediately hit the enter key, and getline reads nothing (because it stops before reading the newline character), discards the newline, and puts the empty string in quest1[0]. And that's why the rest of the getlines work "correctly".

Add cin.ignore('\n') above your loop to get rid of the lingering '\n', and that should make it work, barring other errors in your code.

And don't forget to change x = x++ to just x++ to avoid UB.

Pickard answered 23/11, 2011 at 19:55 Comment(4)
thank you! I haven't experienced such a helpful community in years!Carnarvon
@Carnarvon if this answered your question, please make sure to click the checkmark beside it to mark this as the answer to your question.Pickard
I apologize. I was in class earlier and couldn't edit my program, but assumed this was the correct answer. When I added in cin.ignore('n) above my loop, the program just never brings up the first cout statement within the loop enless you just hit enter about half a dozen timesCarnarvon
@Carnarvon ok, then try cin.ignore(1). I tried that myself and it worked.Pickard

© 2022 - 2024 — McMap. All rights reserved.