#include "../../std_lib_facilities.h"
void search(vector<string> names, vector<double> numbers);
int main()
{
vector<string> names; // holds names
vector<double> numbers; // holds numbers
string name; // input name
double number; // input number
bool double_name = false; // check variable for when a name is entered twice
cout << "Enter name followed by score: ";
while(cin >> name >> number)
{
for (int i = 0; i < names.size(); i++) { // checks if the name has already been entered
if (name == names[i]) {
cout << "That name has already been entered!\n";
double_name = true; // converts to true
break; } }
if(double_name == true) { // if for loop above finds double name, double_name is changed to true so that the while iteration can start again without counting the double name
double_name = false;
cout << "Enter name followed by score: ";
continue; }
names.push_back(name);
numbers.push_back(number);
cout << "Enter name followed by score: ";
}
for(int e = 0; e < names.size(); e++) {
cout << "names[" << e << ']' << " = " << names[e] << ' '
<< "numbers[" << e << ']' << " = " << numbers[e] << endl; } // prints name and number
search(names, numbers);
}
void search(vector<string> names, vector<double> numbers)
{
double scoresearch;
cout << "\nEnter a score:";
cin >> scoresearch; // THIS IS BEING SKIPPED
for(int x = 0; x < names.size(); x++) {
if (scoresearch == numbers[x])
cout << names[x] << " has score" << scoresearch; }
}
Considering that cin must be in an error state for the loop condition on line 14 to fail and that you never reset that state or remove the input from the stream that caused it to enter an error state, how do you expect it not to be in an error state when you try to use it again in search?
Don't require it to be in an error state to exit the loop that begins on line 14. Or, clear the error state and extract the offending input before attempting to get input again.
If you use the box up at the top of the page that says "Search" next to it you can probably find hundreds of threads concerning more or less the same subject.
I tried cin.clear(), but that didn't work. People seem to have a similar problem, but I don't know how to implement their solutions into my program. I understand what the problem is, I just can't figure out the code.
If you could show me the necessary code and where it goes, that would be great.
You'd be providing great help for a nooby programmer :)
I just finished this assignment in class. so I know what you are dealing with now my answer is not going to be neat or nice and it is definitly not production code at all BUT it does what it is supposed to do very nicely. also it seams like you are using to many variables to check for double input like the bool just iterate through the vector and than iterate through again checking if vec[i]==vec[i+1]. So this is my code that I turned in it does the same thing and I am pleased with the look.
#include<iostream>
#include<vector>
#include<string>
#include<Windows.h>
usingnamespace std;
int main()
{
vector <string> names;
vector <int> ages;
string name;
int age;
string age_check;
int name_check;
cout<<"Please enter a name and age seperated by a space."<<endl
<<"When you are done enter finished followed by a '0' to end the data input."<<endl<<endl;
while((cin>>name>>age)&&(name!="finished"))
{
names.push_back(name);
ages.push_back(age);
}
for(int i=0;i<names.size(),i<ages.size();++i)
{
cout<<names[i]<<" "<<ages[i]<<endl;
}
for(int i=0;i<names.size()-1;++i)
{
for(int j=i;names[j]==names[j+1];++j)
{
system("cls");
cout<<"Error: This name is not unique "<<names[j]<<endl;
Sleep(3000);
system("cls");
names.erase(names.begin(),names.end());
ages.erase(ages.begin(),ages.end());
return main();
}
}
cout<<"Type a name to see its corresponding age. \n"
<<"When you are done type Done. \n";
while(cin>>age_check&&age_check!="Done")
{
for(int i=0;i<names.size();++i)
{
if(age_check==names[i])
{
cout<<ages[i]<<endl;
}
}
}
cout<<"Type in an age to check all the names with that age. \n"
<<"When you are done type '0'. \n";
while(cin>>name_check&&name_check!=0)
{
for(int i=0;i<ages.size();++i)
{
if(name_check==ages[i])
{
cout<<name[i]<<endl;
}
}
}
system("pause");
return 0;
}
Also if you notice my while loop for input I use a key word to end the loop it is alot better without having to use another variable to dump your cin input into when it fails. good luck
while ((cin >> name >> number) && (name!= "finished"))
It ended up working.
I'm reading Bjarne Stroustrup's programming book and I'm only up to chapter 5, so I'm not very good at reading other peoples code. I'm learning though!
Thanks for all the pointers, I will continue to study this particular thread as I develop my skills.
I am glad my code helped i am also working through bjarne's book and chapter six has me stumped. anyways I hope you enjoyed the nested for loop to check for repeated input. Keep that in mind if you teacher ever asks you to check for mode.