我正在努力使这个程序适用于我的C ++课程。每次我输入人名时,程序都会告诉我这是无效的。任何帮助查看我的程序将不胜感激。该程序需要从名为mystudents.txt的txt文件中提取名称和gpas。
计划说明:
附带的源代码(Lab6.cpp)包含一个不完整的程序及其设计。在本实验中,您将完成set_gpa,get_gpa和read_students这三个函数。虽然您可以对main进行更改以测试其他功能,但不需要修改主要功能。
按照Lab6.cpp中的设计,编写代码以完成功能,以便三个功能按指定的方式工作。可以修改主函数以进行其他测试,但函数必须独立于主函数指定。
我目前正在制作的节目
#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的示例,其中文件需要从中提取gpas和名称
billy bob
4.0
johnny smith
3.1
Craig Bean
1.2
答案 0 :(得分:0)
在read_students
函数中添加此代码,以确保您实际成功打开文件:
infile.open(filename.c_str());
if (infile.fail()) {
throw std::invalid_argument("Failed to open file: " + filename);
}