这个程序需要读取一个文本文件:
Amy 100 100 90 95 85 100 100 100 98 75
Bob 90 92 82 95 89 93 95 97 96 98 92
Nina 100 90 95 85 100 65 75 95 100 90 60
Eva 98 90 95 85 100 90 95 100 90 82
马特100 90 95 85 75 85 90 95 100 78
湿婆90 90 95 100 75 100 90 92 82 68
并随后提示用户使用switch语句选择名称,并为所输入的选择呈现最高、最低和平均等级。
这就是我一直在做的事情。
如何允许switch语句从数组中获取信息?此外,"我收到格式错误(文件>>名称[])
using namespace std;
void PrintLines (int numlines); //for the border "*"
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
ifstream file;
ofstream file;
file.open("gradename.txt");
if(file.fail())
{
cout<< ("cant open text");
//system("pause");
return 1;
}
string name[6]; //array to store the names
int scores[6][11]; //array to store the scores
int x, y;
for(int x = 0; x < 6; x++)
{
file>>name[x]; //change this error?
for(int y = 0; y < 11; y++)
file >> scores[x][y];
}
{
int number = 0;
double ave = 0.0 ;
double sum = 0;
int count = 0;
int max = 0;
int min = 100;
int selection=0;
while (!file.eof())
{
if(count != 0)
{
if(number>max)
max=number;
if(number<min)
min=number;
}
sum+=number;
count+=1;
file>>number;
}
ave = sum / (count-1);
////end of calculation
int numlines=0; //initializing new variable to print *
cout<< "nHow many lines of * do you want to print"<<endl; //asking user for input for
border *
cin>> numlines;
PrintLines(numlines);
cout<<"nn Whose grades would you like to seen
Enter 1 for Amy, 2 for Bob or 3 for Nina,
4 for Eva, 5 for Matt or 6 for Shiva.... n "<< endl; // User's choice
switch(selection)
{
case 1:
cout << "Amy'sn Highest grade is:" << max << "nLowest grade:"<< min << "nAverage grade:"<< ave << "n";
break;
case 2:
cout << "Bob'sn Highest grade is:" << max << "nLowest grade:" << min << "nAverage grade:" << ave << "n";
break;
case 3:
cout << "Nina'sn Highest grade is:" << max << "nLowest grade:" << min << "nAverage grade:" << ave << "n";
break;
case 4:
cout << "Eva'sn Highest grade is:" << max << "nLowest grade:" << min << "nAverage grade:" << ave << "n";
break;
case 5:
cout << "Matt'sn Highest grade is:" << max << "nLowest grade:" << min << "nAverage grade:" << ave << "n";
break;
case 6:
cout << "Shiva'sn Highest grade is:" << max << "nLowest grade:" << min << "nAverage grade:" << ave << "n";
break;
default:
cout << "n Invalidn";
break;
}
cout << "How many lines of * do you want to print" << endl;
cin >> numlines;
PrintLines(numlines);
}
file.close(); //closing file
system("pause");
return 0;
}
void PrintLines(int numlines) //function that contains the output for *
{
for (int count =1; count <=numlines; count++)
cout << "************************************************************************" << endl;
}
试试这个代码。注意:我假设你的文件是这样的:
Amy 100 100 90 95 85 100 90 100 100 98 75
Bob 90 92 82 95 89 93 95 97 96 98 92
Nina 100 90 95 85 100 65 75 95 100 90 60
Eva 98 90 95 85 100 90 90 95 100 90 82
Matt 100 100 90 95 85 75 85 90 95 100 78
Shiva 90 90 95 100 75 100 100 90 92 82 68
现在的代码:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <map>
#include <numeric>
using namespace std;
void PrintLines (int numlines); //for the border "*"
int main( )
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
ifstream file;
//ofstream file;
file.open("Path to your file\gradename.txt");
if(file.fail()) {
cout<< ("cant open text");
return 1;
}
map<string, vector<int> > scores; //[6][11]; //array to store the scores
for (int x = 0; x < 6; ++x) {
string name;
file >> name; //change this error?
vector<int> scoresOfName;
for(int y = 0; y < 11; y++) {
int score;
file >> score;
scoresOfName.push_back(score);
}
scores[name] = scoresOfName;
}
{
int numlines;
cout<< "nHow many lines of * do you want to print"<
cin >> numlines;
PrintLines(numlines);
cout<<"nn Whose grades would you like to seen Enter 1 for Amy, 2 for Bob or 3 for Nina,4 for Eva, 5 for Matt or 6 for Shiva.... n "<< endl; // User's choice
int selection;
cin >> selection;
string chName = "";
switch(selection)
{
case 1:
chName = "Amy";
break;
case 2:
chName = "Bob";
break;
case 3:
chName = "Nina";
break;
case 4:
chName = "Eva";
break;
case 5:
chName = "Matt";
break;
case 6:
chName = "Shiva";
break;
default:
cout << "n Invalid inputn";
break;
}
cout << "Your choice is " << chName;
cout << "n" << chName << "'sn Highest grade is:"
<< *(max_element(scores[chName].begin(), scores[chName].end()))
<< "nLowest grade:"
<< *(min_element(scores[chName].begin(), scores[chName].end()))
<< "nAverage grade:"
<< accumulate(scores[chName].begin(), scores[chName].end(), 0) / 10
<< "n";
cout << "How many lines of * do you want to print" << endl;
cin >> numlines;
PrintLines(numlines);
}
file.close(); //closing file
cin.get();
return 0;
}
void PrintLines(int numlines) //function that contains the output for *
{
for (int count =1; count <=numlines; count++)
cout << "********************************************************************" << endl;
}