D:\\votes.txt 比如文件为:colin 5000
make 3000
kelin 4000
--------
想输出票数最高的名字:
可达不到效果
麻烦看一下!
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int main()
{
const MAX=5;
char name;
int votes;
int highvotes;
ifstream inflie("D:\\votes.txt");
if(!inflie)
{
cerr<<"The inputfile does not exist."
<<"program terminates!"<<endl;
return 1;
}
inflie>>name>>votes;
highvotes=votes;
vector<char> vect;
vector<char>::iterator iter=vect.begin();
while(inflie)
{
if(votes>highvotes)
{
if(iter!=vect.end()-1)
vect.pop_back();
vect.push_back(name);
highvotes=votes;
iter++;
}
else if(votes==highvotes)
{
if(iter!=vect.end()-1)
vect.push_back(name);
iter++;
}
else
{
cerr<<"Stack overflow program terminates"<<endl;
return 1;
}
inflie>>name>>votes;
}
cout<<"Highest votes:"<<highvotes<<endl;
cout<<"the winner:";
while(!vect.empty())
{
name=vect.back();
vect.pop_back();
cout<<name<<endl;
}
cout<<endl;
return 0;
}