首页 文章

需要文本文件2D阵列指导

提问于
浏览
1

这个程序需要读取一个文本文件:

艾米100 100 90 95 85 100 90 100 100 98 75

Bob 90 92 82 95 89 93 95 97 96 98 92

尼娜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

湿婆90 90 95 100 75 100 100 90 92 82 68

然后提示用户使用switch语句选择一个名称,并为输入的选项提供最高和最低等级 .

这就是我一直在努力的方面 .

我如何允许我的switch语句从数组中获取信息?另外“我得到格式的错误(文件>>名称[])

使用命名空间std;

void PrintLines(int numlines); //为边框“*”

int main()

{

cout.setf(IOS ::固定);

cout.setf(IOS :: showpoint);

cout.precision(2);

ifstream文件;

ofstream文件;

file.open( “gradename.txt”);

如果(file.fail())

{

cout <<(“cant open text”);

//系统( “暂停”);

返回1;

}

字符串名称[6]; //用于存储名称的数组

int scores [6] [11]; //数组来存储分数

int x,y;

for(int x = 0; x <6; x)

{

文件名>> [X]; //更改此错误?

for(int y = 0; y <11; y)

档案>>得分[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)

{

如果(数>最大)

最大=数目;

如果(数

MIN =数目;

}

sum =数字;

count = 1;

文件>>数;

}

ave = sum /(count-1);

////计算结束

int numlines = 0; //初始化要打印的新变量*

cout <<“\ n你要打印多少行*”

cin >> numlines;

PrintLines(numlines);

cout <<“\ n \ n你希望看到谁的成绩\ n为Amy输入1,为Bob输入2或为Nina输入3,

4为Eva,5为Matt或6为Shiva .... \ n“<< endl; //用户的选择

开关(选择)

{

情况1:

cout <<“Amy's \ n最高等级是:”<< max <<“\ n最低等级:”<< min <<“\ n平均等级:”<< ave <<“\ n”;

打破;

案例2:

cout <<“Bob's \ n最高等级是:”<< max <<“\ n最低等级:”<< min <<“\ n平均等级:”<< ave <<“\ n”;

打破;

案例3:

cout <<“Nina's \ n最高等级是:”<< max <<“\ n最低等级:”<< min <<“\ n平均等级:”<< ave <<“\ n”;

打破;

案例4:

cout <<“Eva's \ n最高等级是:”<< max <<“\ n最低等级:”<< min <<“\ n平均等级:”<< ave <<“\ n”;

打破;

案例5:

cout <<“Matt's \ n最高等级是:”<< max <<“\ n最低等级:”<< min <<“\ n平均等级:”<< ave <<“\ n”;

打破;

案例6:

cout <<“Shiva's \ n最高等级是:”<< max <<“\ n最低等级:”<< min <<“\ n平均等级:”<< ave <<“\ n”;

打破;

默认:

cout <<“\ n无效\ n”;

打破;

}

cout <<“你要打印多少行*”<< endl;

cin >> numlines;

PrintLines(numlines);

}

file.close(); //关闭文件

系统( “暂停”);

返回0;

}

void PrintLines(int numlines)//包含*的输出的函数

{

for(int count = 1; count <= numlines; count)

cout <<“ ************************************************************************ ”<< endl;

}

1 回答

  • 0

    试试这个代码 . 注意:我假设您的文件如下所示:

    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<<"\n\n Whose grades would you like to see\n 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 input\n";
                break;
            }
    
            cout << "Your choice is " << chName;
    
    
            cout << "\n" << chName << "'s\n 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;
    }
    

相关问题