I am new to C++ and programming for that matter, but I am realizing that I love it more and more everyday. At this point I am learning about <list> and <string>. I am trying to get a program up and running that simply takes 20 string in from the user, stores them in a list, & then outputs them back to the user. Here is what I have come up with so far. From what I can tell it is takeing the strings in, but I am not sure if it is storing them in my list. Also I thought my second loop would output the strings back but I was wrong. Everything is compiling without errors, but it is not doing what I want it to do.
Am I correct in assuming that my first for loop is taking a string in from the user and putting it in the first memory location of my list?Code:#include <iostream> #include <string> #include <list> using namespace std; int main(int argc, char **pArgv) { string useInput; list<string> sampleList(20); cout << "Enter twenty strings" << endl; list<string>::iterator iter; for(iter = sampleList.begin(); iter != sampleList.end(); iter++) { cin >> useInput; sampleList.push_back(useInput); } for(iter = sampleList.begin(); iter != sampleList.end(); iter++) { cout << (*iter) << endl; } }
What is weird and I can't figure out is when I do compile and run my program it prompts me to enter my strings, but does not stop me after I enter 20.