#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<string> names;
vector<double> prices;
vector<int> scores;
double best_price = 1;
int best_score = 0;
int best_index = -1;
bool more = true;
while (more)
{
string next_names;
cout<<"enter the name:"<<endl;
getline(cin,next_names);
names.push_back(next_names);
cout << next_names<<endl;
double next_prices;
cout << "enter the price:"<<endl;
cin >> next_prices;
prices.push_back(next_prices);
cout << next_prices<<endl;
int next_scores;
cout << "enter the score:"<<endl;
cin >> next_scores;
scores.push_back(next_scores);
cout << next_scores<<endl;
string remainder;
getline(cin,remainder);//为了去掉getlibe和cin互用引起的字符问题,
if (next_scores / next_prices > best_score / best_price)//性价比
{
best_index = names.size() - 1;
best_score = next_scores;
best_price = next_prices;
}
cout<<"More data? (y/n):";//继续添加新的数据
// string answer;
string answer1;
// cin>>answer;
// getline(cin,answer);
cin>>answer1;
if (answer1 != "y")
more = false;
cout<< "Error answer1 :"<< answer1<<11<<endl;
}
for(int i= 0;i<names.size();i++)
{
if (i == best_index)
{
cout<<"best value=>";
cout<<names[i]
<<"price:"<<prices[i]
<<"score:"<<scores[i]<<"\n";
}
}
return 0;
}
以上代码运行时有4处警告错误(本人以为可以忽略),但是在新的添加数据时,使用getline传入的值(answer)总是为空,改用cin之后发现answer值正确了,但是当继续输入数据时,它就自动输出上一个数据,这是什么原因,还有getline和cin之间在互用时到底还有什么地方要注意的.
虚心求教!