| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 524 人关注过本帖
标题:有关数据流的输入输出问题
只看楼主 加入收藏
slblake
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-7-24
收藏
 问题点数:0 回复次数:0 
有关数据流的输入输出问题

#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之间在互用时到底还有什么地方要注意的.
虚心求教!

搜索更多相关主题的帖子: 输入 输出 数据 
2007-08-24 21:28
快速回复:有关数据流的输入输出问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014731 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved