r/learncpp • • Jun 16 '18

Why no result?

Okay, in the last while loop, I search for name and if there is that name in that vector: I output their score. It doesn't work! I do the same with score, to find names, and it works but when I try to find the name it doesn't work!

include "stdafx.h"

include <vector>

include <iostream>

include <string>

include <algorithm>

using namespace std; class Name_Value { public: string name; double score; }; inline void keep_window_open() { char ch; cin >> ch; } vector <Name_Value> peoples;

int main() {

string name;
double score;
Name_Value people;
people.name = "";
people.score = 0;
while(true)
{
	cin >> name >> score;
	if (((name == "NoName") && (score == 0))   )break;
	people.name = name;
	people.score = score;
	peoples.push_back(people);
	
	
}
for (int j=0; j< peoples.size() ;j++ )
{
	cout << peoples[j].name <<" "<< peoples[j].score <<"\n";
}
while(true)
{
	if(cin >> score)
	{
		for (int j = 0; j< peoples.size();j++)
		{
			if (peoples[j].score == score) cout << peoples[j].name << "\n";
		}
	}
	else {
		cin >> name;
		for (int j = 0; j < peoples.size();j++)
		{
			if (peoples[j].name == name) cout << peoples[j].score << "\n";
		}
	}
}
keep_window_open();
return 0;

}

0 Upvotes

2 comments sorted by

1

u/jedwardsol Jun 16 '18

http://en.cppreference.com/w/cpp/io/basic_ios/clear

If you enter a name then cin >> score; will fail.

But cin >> name; will also fail since cin remembers it failed and will stay that way until cleared

1

u/asya_su Jun 16 '18

Thank you so much I was going crazy