gpt4 book ai didi

C++ 程序每隔一行读取输入文件,而不是每一行

转载 作者:太空宇宙 更新时间:2023-11-04 12:01:52 30 4
gpt4 key购买 nike

我正在做一项家庭作业,该作业使用外部文件计算 GPA 和类(class)平均分。代码还没有完全充实,但我已经足够完善了,它至少应该能够告诉我它可以从外部文件(在 TextWrangler 中创建的纯文本文件)中提取数据,并计算数字.

但是,当我运行它时,它似乎只提取输出文件的每一行 other 。关于我可能哪里出错的任何想法?

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main()
{

int studentID, studentCounter=0;
double grade1, grade2, grade3, grade4, course1Sum=0, course2Sum=0, course3Sum=0, course4Sum=0, course1Average, course2Average, course3Average, course4Average, GPA;
ifstream inputFile;
string filename;

for (int x=1; x<=3; x++) {

// Get the filename from the user.
// /Users/etc/Desktop/HW5test.txt - valid set
// /Users/etc/Desktop/HW5testset.txt - test set


cout<<"Enter the filename: ";
cin>>filename;

// Open the file.
inputFile.open(filename.c_str());
// If the file successfully opened, process it.
if (inputFile) {
// Read the numbers from the file and display them.
while (inputFile>>studentID>>grade1>>grade2>>grade3>>grade4)
{
if (studentCounter==0) // Prints header if at the start of the program.
cout<<"Header placeholder"<<endl;

inputFile>>studentID>>grade1>>grade2>>grade3>>grade4;
GPA=(grade1+grade2+grade3+grade4)/4;
cout<<studentID<<" "<<GPA<<" "<<"Special message (if needed)."<<endl;

// Course Average and Student Accumulators
course1Sum+=grade1;
course2Sum+=grade2;
course3Sum+=grade3;
course4Sum+=grade4;
studentCounter++;

} // Ends while() loop.

if (studentCounter==0)
cout<<"File contains no data."<<endl;

else {

// Perform course averages here, after all data has been read.
course1Average=course1Sum/studentCounter;
cout<<"Course 1 average: "<<course1Average<<endl;
course2Average=course2Sum/studentCounter;
cout<<"Course 2 average: "<<course2Average<<endl;
course3Average=course3Sum/studentCounter;
cout<<"Course 3 average: "<<course3Average<<endl;
course4Average=course4Sum/studentCounter;
cout<<"Course 4 average: "<<course4Average<<endl<<endl;

} // Ends "else" of file empty error check.

// Close the input file.
inputFile.close();
} // Ends "if" of if/else().
else {
cout<<"Error opening the file.\n";
} // Ends "else" of if/else().


} // Ends for() loop.

return 0;
} // Ends main().

输入文件数据:

1001 3.0 2.7 2.0 2.8 
1002 3.7 3.5 4.0 4.0
1003 0.0 1.0 2.2 1.0
1004 4.0 3.0 3.5 3.5

我的输出看起来像这样(请记住,仍在处理部分,但请注意它只是提取所有其他条目:

Enter the filename: /Users/etc/Desktop/HW5testset.txt
Header placeholder
1002 3.8 Special message (if needed).
1004 3.5 Special message (if needed).
Course 1 average: 3.85
Course 2 average: 3.25
Course 3 average: 3.75
Course 4 average: 3.75

可能发生了什么?

最佳答案

inputFile>>studentID>>grade1>>grade2>>grade3>>grade4 出现在您的 while 语句和 while 语句中。删除不在while条件的实例(GPA=之前的实例。

您阅读 while 语句中的行,然后阅读下一行,因此永远不会看到两行中的第一行。仅仅因为代码在 while() 中并不意味着它没有副作用

编辑

也让你的

if (studentCounter==0) // Prints header if at the start of the program.
cout<<"Header placeholder"<<endl;

进入:

if (studentCounter==0) { // Prints header if at the start of the program.
studentCounter++;
cout<<"Header placeholder"<<endl;
continue;
}

关于C++ 程序每隔一行读取输入文件,而不是每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13791958/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com