在C ++中读取文件,搜索和显示信息到结构数组时出现问题 [英] Problem reading file,searching, and displaying information to an array of structures in C++

查看:72
本文介绍了在C ++中读取文件,搜索和显示信息到结构数组时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这看起来很愚蠢,我是C ++的新手,我无法显示从输入文件读取到结构数组中的信息。

Sorry if this seems dumb, I am pretty new to C++ and I'm having trouble displaying information read into an array of structures from an input file.

我有3个功能。一个用于提示将我的文件读入数组,一个用于提示用户在数组中搜索特定的结构,最后一个用于显示数组的内容。

I have 3 functions. One for reading my file into the array, one for prompting the user to search for a specific struct in the array, and the last one for displaying the contents of the array.

我不确定我的 readFile() displayAllStudents()是否损坏。我可以从文件中输出第一行,但其余为0。接下来,我的 selectStudent()可能很糟糕;我很难找到要解决的问题的好的解决方案。

I don't know for certain if my readFile() or displayAllStudents() is broken. I am able to output the first line from the file, but the rest is 0. Next my selectStudent() is probably terrible; I'm having trouble finding a good solution for what I'm trying to accomplish.

我尝试了许多此处发布的解决方案,但是我的问题似乎很特殊,所以我m希望我能帮助您指出正确的方向。

I've tried many solutions posted here, but my issue seems unique so I'm hoping I can help pointed in the right direction.

Input file and desired output.


    ID   CLA   OLA   Quiz   Homework   Exam   Bonus   Total   FinalGrade   
c088801    10    15      4         15     56       5 
c088802     9    12      2         11     46       2 
c088803     8    10      3         12     50       1
c088804     5     5      3         10     53       3
c088805     3    11      1         10     45       0 
c088806     8    14      2         11     40      -1  
c088807     4    12      2         12     48      -2
c088808    10    10      3         11     36       0
c088809     8     8      3         11     39       0
c088810     6     9      4          9     47       3
c088811     8     7      3         13     41       3
c088812     4    11      3         11     37       1
c088813     9    15      2          8     50       2
c088814     8    12      2         10     48       4
c088815     6     8      1          7     45       1
c088816     7     7      2          6     51       2
c088817     8     9      2         12     38       2 



#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Student {
    char ID[7];
    int CLA;
    int OLA;
    int Quiz;
    int Homework;
    int Exam;
    int Bonus;
    int Total;
    int FinalGrade;
};
const int SIZE = 20;

//Function prototypes
void readFile(Student[]);
int selectStudent(Student[]);
void displayAllStudents(Student[]);

int main() {
    Student Roster[SIZE] = {};                  //Initalizes array

    readFile(Roster);
    //selectStudent(Roster);
    displayAllStudents(Roster);

    system("pause");
    return 0;
}

//This function will read the text file into our array of structures.
void readFile(Student Roster[]) {
    ifstream inFile("point.dat");                   //Reads input file
    string line;

    getline(inFile, line);                          //Skips first line of file

    for (int i = 0; i < SIZE; i++) {                
            inFile >> Roster[i].ID >> Roster[i].CLA >> Roster[i].OLA >>
                Roster[i].Quiz >> Roster[i].Homework >> Roster[i].Exam >>
                Roster[i].Bonus >> Roster[i].Total >> Roster[i].FinalGrade;
    }

}

//This function will prompt user to select an individual student ID
//then display that student's information.
int selectStudent(Student Roster[]) {
    char* cNumber;

    cout << "Please enter the student's c-number:" << endl;
    cin >> cNumber;

    for(int i; i < SIZE; i++){
        if(strcmp(Roster[i].ID, cNumber)==0){
            return i;
        }
    }
    return -1;
}

//This function will display all our student information
void displayAllStudents(Student Roster[]) {
    cout << "     ID   CLA   OLA   Quiz   Homework   Exam   Bonus   Total   FinalGrade" << endl;

    for (int i = 0; i < SIZE; i++) {
        cout << Roster[i].ID[0] << Roster[i].ID[1] << Roster[i].ID[2] << Roster[i].ID[3] << Roster[i].ID[4] << Roster[i].ID[5] << Roster[i].ID[6]
            << " " << Roster[i].CLA << " " << Roster[i].OLA << " " << Roster[i].Quiz << " " <<
            Roster[i].Homework << " " << Roster[i].Exam << " " << Roster[i].Bonus << " " <<
            Roster[i].Total << " " << Roster[i].FinalGrade << endl;
    }
}




My output.

     ID   CLA   OLA   Quiz   Homework   Exam   Bonus   Total   FinalGrade
c088801 10 15 4 15 56 5 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0
Press any key to continue . . .


推荐答案

readFile ,当您在此处读取文件的内容时,已假定每个Student结构的所有字段都包含数据。

In readFile, when you're reading the contents of your file here, you've assumed that all fields for every Student struct contain data.

        inFile >> Roster[i].ID >> Roster[i].CLA >> Roster[i].OLA >>
            Roster[i].Quiz >> Roster[i].Homework >> Roster[i].Exam >>
            Roster[i].Bonus >> Roster[i].Total >> Roster[i].FinalGrade;

由于您输入的内容在总计 FinalGrade 列,当您的程序显示>>名册[i]。总计第一次实际上是试图读取第二个学生的id, c088802 ,它不是 Rock [i]。总计

Since your input doesn't have content in the Total and FinalGrade columns, when your program hits >> Roster[i].Total for the first time it's actually trying to read the second student's id, c088802, which isn't the integer value expected by Roster[i].Total.

如果您知道输入数据将永远不会包含在 Total FinalGrade 列,则可以删除>>名册[i]。总数>>名单[i] .FinalGrade 从文件读取循环开始。

If you know your input data will never have content in the Total and FinalGrade columns, you can remove >> Roster[i].Total >> Roster[i].FinalGrade from your file read loop.

如果您知道输入数据可能不完整,但不知道行中将填充多少行,虽然可能有更好的方法,但类似的事情应该起作用。

If you know your input data might not be complete, but don't know how much of the row will be filled, something like this should work, though there's likely a better way.

for (int i = 0; i < SIZE; i++) {
    getline(infile, line);
    stringstream ss(line);

    ss >> Roster[i].ID;

    int value, count = 0;
    while(ss >> value){
        switch(count)
        {
           case 0: Roster[i].CLA = value;
           break;
           case 1: Roster[i].OLA = value;
           break;
           case 2: Roster[i].Quiz = value;
           break;
           ...
           case 7: Roster[i].FinalGrade = value;
           break;
        }
        ++count;
    }        
}

这篇关于在C ++中读取文件,搜索和显示信息到结构数组时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆