- #1
exitwound
- 292
- 1
We're only second semester programmers who don't know too much.
Using lists and iterators, write a program which accepts string inputs from the console and outputs them in alphabetical order. If the whole program has to be rewritten, fine. I've spent way too much stupid time on it already.
Here's a snippet of the code. I'm having problems writing out how to get the string to go BEFORE the first myList entry.
-Extremely frustrated.
Homework Statement
Using lists and iterators, write a program which accepts string inputs from the console and outputs them in alphabetical order. If the whole program has to be rewritten, fine. I've spent way too much stupid time on it already.
The Attempt at a Solution
Here's a snippet of the code. I'm having problems writing out how to get the string to go BEFORE the first myList entry.
Code:
list<string> myList;
string myString;
int i=1; //counter
do
{
//ask for String
cout << "Insert a string: ";
//put string into myString
cin >> myString;
if (i==1)
myList.push_back(myString);
for (list<string>::iterator pos = myList.begin(); pos != myList.end() && i != 1; ++pos)
{
if (myString < *pos)
{
myList.insert(pos, myString);
break;
}
}
i++;
} while (i<=STRINGS_TO_USE);
-Extremely frustrated.