So this is my first post though I have been reading posts on here for the past 3 weeks. I am in a 4th week of c++ class. One of the homework programs involves creating two vectors, on to track a string(name) and one for an int(score).
I have answered the first part of the question, the second part however asks to modify the program so that when a name is entered, it outputs the corresponding score.
The header we use is limited so a lot of more advanced operators won't work.
#include "std_lib_facilities.h"
int main(){
vector<string> namesvector; //vector for name
vector<int> scoresvector; //vector for scores
string name; //string for name
int score; //int for score
cout<<"Enter in a set of name-and-value pairs such as 'Joe 17'\n";
cout<<"When you are finished, enter in 'No more'"<<endl; //user direction
while(cin>>name>>score){
namesvector.push_back(name);
scoresvector.push_back(score); //pushes name and score into vectors
}
for(int i=0; i<namesvector.size();i++)
cout<<namesvector[i]<<" "<<scoresvector[i]<<endl; //outputs all of the input
//part I am struggling with
cout<<"If you would like to find the score of a specific person, please enter the name"<<endl;
cin>>name;
cout<<
system("Pause");
return 0;
}
I understand that say I entered
joe 12
namesvector[0]=joe scoresvector[0]=12
Vectors are still kind of new-ish so not everything is clear to me.
I am just not sure how to relay that into a cin cout if statement, can anyone point me in the right direction?
Took me a few hours but I finally figured it out.
Here is the part of the code that achieves what I needed in case anyone else ever has the same question.
1 2 3 4 5 6 7 8 9 10 11
cout<<"If you would like to find the score of a specific person, please enter the name"<<endl;
string nametwo;
while(cin>>nametwo){
int findname=0;
for(int i=0; i<namesvector.size(); i++){
if (namesvector[i]==nametwo){
cout<<"Score of "<<nametwo<< " is "<< scoresvector[i]<<endl;
findname=1;
break;
}