2 vectors and 1 is a string

closed account (2yRiz8AR)
I have to right a program that takes in a name and a number ex:(John 17 Jane 12) and then print them on the screen but i don't know how to make the one that is a string to stop taking in names.

here is what i have so far:

{
cout<< "please enter the name of your players and their number of goals.\n";

vector<string> names;
string name;
while(cin>>name)
names.push_back(name);

vector<double> scores;
double score;
while(cin>>score)
scores.push_back(score);

cout<< " " names " " scores " \n";

system ("pause");

}

can anyone help me?

or tell me whats wrong with my code?
easiest way is to tell the user to enter something that you can test for such as "no more names" or "finished". Off the top of my head, I am not aware of any way to cause it to stop when entering strings because numbers can be turned into strings by the stream template. You could also ask for the number of players first, and then loop on that number.

Keep this link handy for future reference. You could probably make some other minor improvements.
http://www.parashift.com/c++-faq-lite/input-output.html

closed account (2yRiz8AR)
I Finally got it to work, does this look better?


#include "std_lib_facilities.h"

int main()
{
cout<< "please enter the names of 2 of your frat brothers and how many numbers they got at the last party\n";
try{

vector<string> names;
vector<double> scores;

string name;
double score;

while( cin>>name>>score && name!="done")
{
for(int i=0;i<names.size(); ++i)
if (name==names[i]) error(name, " already exists in this list.");
names.push_back(name);
scores.push_back(score);
}


cout<< "Here is your list:\n";
for(int i=0;i<names.size(); ++i)
cout<< " "<< names[i]<< " " <<scores[i]<< " \n";
system ("pause");
}
catch (runtime_error e){
cout<< e.what() << " \n";
system ("pause");
}
}
FYI, it was just reading until EOF. Ctrl-D would have terminated each loop.
Topic archived. No new replies allowed.