【问题标题】:I need help figuring out why current program does not work properly我需要帮助找出当前程序无法正常运行的原因
【发布时间】:2017-02-24 20:29:17
【问题描述】:

我正在努力使这个程序适用于我的 C++ 课程。每次我去输入一个人的名字时,程序都会告诉我这是无效的。任何查看我的程序的帮助将不胜感激。该程序需要从名为 mystudents.txt 的 txt 文件中提取名称和 gpas。

程序说明:

随附的源代码 (Lab6.cpp) 包含一个不完整的程序及其设计。在本实验中,您将完成三个函数 set_gpa、get_gpa 和 read_students。 Main 函数不需要修改,但您可以更改 main 以测试您的其他函数。

按照 Lab6.cpp 中的设计,编写代码完成功能,使三个功能按规定工作。可以修改您的 main 函数以进行额外测试,但这些函数必须按照指定的方式独立于 main 函数工作。

我目前正在从事的项目:

#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
#include <fstream>

using namespace std;


void set_gpa(string names[], double gpas[], int arraysize, double newgrade);
double get_gpa(string names[], double gpas[], int arraysize);
void read_students(string names[], double gpas[], int arraysize, stringfilename);



//
const int NUMSTU = 20;

int main(){
    srand(time(NULL));
    string studentnames[NUMSTU];
    double gpas[NUMSTU];
    string studentfile = "mystudents.txt";
    double studentgpa;

    read_students(studentnames, gpas, NUMSTU, studentfile);
    studentgpa = get_gpa(studentnames, gpas, NUMSTU);
    cout << "Got GPA of:  " << studentgpa << endl;
    set_gpa(studentnames, gpas, NUMSTU, 3.9);
    studentgpa = get_gpa(studentnames, gpas, NUMSTU);
    cout << "Got GPA of:  " << studentgpa << endl;

    return 0;
}


//set_gpa
//Purpose:  Get input of a student's name and, after validation, set that student's GPA to a given value.
//Preconditions:  Arrays of student names and gpas in parallel, the size of the array, and the grade.
//Postconditions:  Change the gpa of the validated student to the given value
void set_gpa(string names[], double gpas[], int arraysize, double newgrade){
   int counter = 0;
   bool studentnotfound = true;
   string nextname;
   double theirgpa;
    //1.  Get input of a student's name.
        cout << "Please input a students name" << endl;
        getline(cin, nextname);
    //2.  Search for the student's name in the array
        for(int i = 0; i < arraysize; i++){
            //2.1 Are you waldo?
            if(nextname == names[i]){
                studentnotfound = false;
                theirgpa = gpas[i];
            }
        }
        //3.  Until student's name has been located in the array:
        while(studentnotfound){
    //3.1     Inform user that the name was invalid.
            cout << "invalid name." << endl;
    //3.2     Get input of a student's name.
            cout << "Please input a  students name" << endl;
            getline(cin, nextname);
    //3.3     Search for the student's name in the array
            for(int i = 0; i < arraysize; i++){
    //4.  Store the student's new GPA in the array
            if(nextname == names[i]){
            gpas[i] = newgrade;
            }
        }
    }
        return;
}

//get_gpa
//Purpose:  Get input of a student's name and, after validation, return that student's GPA
//Preconditions:  Arrays of student names and gpas in parallel, the size of the array, and the grade.
//Postconditions:  Return the gpa of the validated student
double get_gpa(string names[], double gpas[], int arraysize){
   int counter = 0;
   bool studentnotfound = true;
   string nextname;
   double theirgpa;
    //1.  Get input of a student's name.
        cout << "Please input a students name" << endl;
        getline(cin, nextname);
    //2.  Search for the student's name in the array
        for(int i = 0; i < arraysize; i++){
            //2.1 Are you waldo?
            if(nextname == names[i]){
                studentnotfound = false;
                theirgpa = gpas[i];
            }
        }

        while(studentnotfound){
    //3.  Until student's name has been located in the array:
    //3.1     Inform user that the name was invalid.
            cout << "invalid name." << endl;
    //3.2     Get input of a student's name.
            cout << "Please input a students name" << endl;
            getline(cin, nextname);
    //3.3     Search for the student's name in the array
            for(int i = 0; i < arraysize; i++){
            //2.1 Are you waldo?
            if(nextname == names[i]){
                studentnotfound = false;
            }
            }
        }
    //4.  Return the student's GPA
    return theirgpa;
}

//read_students
//Purpose:  Store student names and gpas in two parallel arrays from an input file
//Preconditions:  empty arrays to store student names and gpas, the size of the arrays, and a file name
//Postconditions:  store the names and gpas of students into the array arguments up to the maximum size of the arrays
void read_students(string names[], double gpas[], int arraysize, string filename){
    string nextname;
    double nextgpa;
    ifstream infile;
    char nextchar;

    int counter = 0;
    //1.  Open file with the given filename
            infile.open(filename.c_str());
    //2.  Try to read a student's name on the next line
            getline(infile, nextname);
    //3.  Try to read a student's GPA on the next line
            infile >> nextgpa;
    //4.  Until we fail to read a student or run out of array space:
                while(infile && counter < arraysize){
    //4.1     Store the student's name in the array
                    names[counter] = nextname;
    //4.2     Store the student's gpa in the array
                    gpas[counter] = nextgpa;
    //4.3     Try to read a student's name on the next line
                    infile.get(nextchar);
                    getline(infile, nextname);
    //4.4     Try to read a student's GPA on the next line
                    infile >> nextgpa;

    //4.5     Add one to the line counter
                    counter ++;
                }
                infile.close();

}

mystudents.txt 的示例,该文件需要从中提取 gpa 和名称

billy bob
4.0    
johnny smith
3.1
Craig Bean
1.2

【问题讨论】:

  • 调试器是解决此类问题的正确工具。 询问 Stack Overflow 之前,您应该逐行逐行检查您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题,以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
  • 您只是忘记将 studentnotfound 设置为 false 一旦发现名称有效。
  • 删除前向声明,将main 移动到文件底部,然后惊叹于一半的错误神奇地消失了。虽然我认为stringfilename 只是一个错字,但不必要的前向声明只是更多代码,可能会在零附加值的情况下出错。
  • 通过编译器运行您的代码。它可能告诉你一些关于theirgpa 的重要信息。

标签: c++ arrays getline


【解决方案1】:

read_students 函数中添加此代码,以确保您确实成功打开了文件:

infile.open(filename.c_str());
if (infile.fail()) {
    throw std::invalid_argument("Failed to open file: " + filename);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    相关资源
    最近更新 更多