I am attempting to take names and weights from strings and just simply output them. For some reason it can't get out of the while loop, it just sets there waiting for the users to input names and weights. Here's my code
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main (void)
{
// creating variables for input
string name;
int weight;
// creating vectors
vector<string> names;
vector<int> weights;
cout << "Enter names and weight" << endl;
while(cin >> name, weight)
{
names.push_back(name);
weights.push_back(weight);
}
// outputs names and weight
for( unsigned int n = 0; n < weights.size(); n++ )
{
cout << "[" << n << "] " << weights[n] << " " << names[n] << endl;
}
cout << "done." << endl;
return 0;
}
I am self teaching myself C++ with someone helping me as well, my issue is. I am currently learning from Accelerated C++ book. I am on chapter three.
The problem is it doesn't come out of the while loop. It doesn't know when the user will stop input. How do I do it this way, but make it so the computer knows when the while(cin >> ) stops from the user input.
I assume you want this in line 20:
while(cin >> name >> weight)
not this:
while(cin >> name, weight)
What would you like the user to enter when he/she is done? You need to tell them (and the computer) how to signify that input is finished. It's not like reading from a file when you can detect end of file.
cout << "Enter names and weight. Enter 'Done' when finished" << endl;
cin >> name;
while(name.compare ("Done") != 0)
{
cin >> weight;
names.push_back(name);
weights.push_back(weight);
cin >> name;
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
the compare() function is just like "==" for std::strings.
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
Is there a way to do without the compare? I am unfamiliar with this function and it wasn't explained. Lets just say the user types Done in the console.
You can change that line to:
while(name != "Done")
and it'll do the same thing.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Look at my loop again:
cout << "Enter names and weight. Enter 'Done' when finished" << endl;
cin >> name;
while(name != "Done")
{
cin >> weight;
names.push_back(name);
weights.push_back(weight);
cin >> name;
}
Look at your loop:
cout << "Enter names and weight, press 'Done' when finished." << endl;
while(name.compare ("Done") !=0)
{
cin >> name >> weight;
names.push_back(name);
weights.push_back(weight);
}
Note my line 2. I initialize name so it definitely has a value BEFORE the while loop. You don't do this.
I then read in the weight in line 5. I have ALREADY read in the name, so I don't ask for the name again. You ask for both in the same line.
I now have a name and a weight. I push them onto the vectors in lines 6 and 7.
In line 8, I ask for the name again.
Try my original code that I posted in post 2. See if it works for you. Then change the while line to what I posted in post 4 since you didn't want to use compare. That's fine. But whether you use compare is not the issue here. Give it a try and see if that solves your problem. if it doesn't, post the entire code as you did, so we can run the same program.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711