在 C++ 中从 .csv 读取文件
Reading file from .csv in c++
我正在编写一个从 .csv 文件获取输入的程序。程序 运行 但问题是它只 returns .csv 文件的最后一行并跳过所有其他 9 行。
它也不显示平均值和等级。请帮我弄清楚我做错了什么。
教授非常具体地介绍了我们目前所学的知识,即循环和函数。我只能使用循环和函数,所以我不允许使用数组。
这是 scores.csv 文件
#include <iostream>
#include <fstream>
using namespace std;
int testScore1, testScore2, testScore3, testScore4, testScore5;
double avg;
char grade;
double getAvg(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5){
avg = (testScore1 + testScore2 + testScore3 + testScore4 + testScore5)/5;
return avg;
}
char getGrade(double avg){
if (avg >= 90){
grade = 'A';
cout << 'A' << endl;
}else if (avg >= 80){
grade = 'B';
cout << 'B' << endl;
}else if (avg >= 70){
grade = 'C';
cout << 'C' << endl;
}else if (avg >= 60){
grade = 'D';
cout << 'D' << endl;
}else{
grade = 'F';
cout << 'F' << endl;
}
return grade;
}
int main(){
ifstream myFile;
myFile.open("scores.csv");
if(!myFile.is_open()){
cout << "Error" << endl;
}
string testScore1, testScore2, testScore3, testScore4, testScore5;
while (myFile.good()) {
getline(myFile, testScore1, ',');
getline(myFile, testScore2, ',');
getline(myFile, testScore3, ',');
getline(myFile, testScore4, ',');
getline(myFile, testScore5, '\n');
}
cout<< setw(15) <<" Test scores"<<endl;
cout<<"--------------------"<<endl;
cout<< " 1 2 3 4 5 Avg Grade"<<endl;
cout<<"=== === === === === ===== ====="<<endl;
cout<< " "<< testScore1<< " "<< testScore2 << " "<< testScore3 << " "<< testScore4 << " "<< testScore5<< " " << getAvg << " " << getGrade <<endl;
return 0;
}
您在 while 循环之外打印值,这意味着您永远不会保存以前的值。您的程序本质上是在说:
“将 t1 设置为测试分数第 0 行,第 0 列,将 t2 设置为第 1 行,第 0 列,等等...”
循环重新开始,现在开始:
“将 t1 设置为测试分数第 0 行第 1 列,将 t2 设置为第 1 行第 1 列,等等...”
你看到问题了吗?每次循环运行时都覆盖变量,而不保存它们。然后程序只打印最后一行,因为这是循环结束时的变量。
您可能想使用二维数组来解决这个问题。然后,您可以使用嵌套循环将每个点设置为相应的值。这些信息是否足以找出其余部分?如果不告诉我。
总体思路:
new int[行数][列数]数组;
for(int i = 0; i = 行数; i++)
for(int j=0; j=列数; j++)
array[i][j] = getline 代码在这里
闭环。
已经给出答案并被接受。
为了完整起见,我将展示您的代码的重构版本和工作版本。
我没有使用数组。只是循环和函数。
#include <iostream>
#include <fstream>
#include <iomanip>
double getAvg(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5){
double avg = (testScore1 + testScore2 + testScore3 + testScore4 + testScore5)/5.0;
return avg;
}
char getGrade(double avg){
char grade = 'F';
if (avg >= 90){
grade = 'A';
}else if (avg >= 80){
grade = 'B';
}else if (avg >= 70){
grade = 'C';
}else if (avg >= 60){
grade = 'D';
}
return grade;
}
int main(){
std::ifstream myFile("scores.csv");
if(!myFile){
std::cerr << "\nError: Could not open input file\n";
}
else {
std::cout<< std::setw(15) <<" Test scores\n--------------------\n 1 2 3 4 5 Avg Grade\n=== === === === === ===== =====\n";
int testScore1, testScore2, testScore3, testScore4, testScore5;
char comma1, comma2, comma3, comma4;
// Read all values in one line, then next line, next line and so on
while (myFile >> testScore1 >> comma1 >> testScore2 >> comma2 >> testScore3 >> comma3 >> testScore4 >> comma4 >> testScore5) {
// Calculate
double average = getAvg(testScore1, testScore2, testScore3, testScore4, testScore5);
char grade = getGrade(average);
std::cout<< " "<< testScore1<< " "<< testScore2 << " "<< testScore3 << " "<< testScore4 << " "<< testScore5<< " " << average << " " << grade << '\n';
}
}
return 0;
}
我正在编写一个从 .csv 文件获取输入的程序。程序 运行 但问题是它只 returns .csv 文件的最后一行并跳过所有其他 9 行。 它也不显示平均值和等级。请帮我弄清楚我做错了什么。
教授非常具体地介绍了我们目前所学的知识,即循环和函数。我只能使用循环和函数,所以我不允许使用数组。
这是 scores.csv 文件
#include <iostream>
#include <fstream>
using namespace std;
int testScore1, testScore2, testScore3, testScore4, testScore5;
double avg;
char grade;
double getAvg(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5){
avg = (testScore1 + testScore2 + testScore3 + testScore4 + testScore5)/5;
return avg;
}
char getGrade(double avg){
if (avg >= 90){
grade = 'A';
cout << 'A' << endl;
}else if (avg >= 80){
grade = 'B';
cout << 'B' << endl;
}else if (avg >= 70){
grade = 'C';
cout << 'C' << endl;
}else if (avg >= 60){
grade = 'D';
cout << 'D' << endl;
}else{
grade = 'F';
cout << 'F' << endl;
}
return grade;
}
int main(){
ifstream myFile;
myFile.open("scores.csv");
if(!myFile.is_open()){
cout << "Error" << endl;
}
string testScore1, testScore2, testScore3, testScore4, testScore5;
while (myFile.good()) {
getline(myFile, testScore1, ',');
getline(myFile, testScore2, ',');
getline(myFile, testScore3, ',');
getline(myFile, testScore4, ',');
getline(myFile, testScore5, '\n');
}
cout<< setw(15) <<" Test scores"<<endl;
cout<<"--------------------"<<endl;
cout<< " 1 2 3 4 5 Avg Grade"<<endl;
cout<<"=== === === === === ===== ====="<<endl;
cout<< " "<< testScore1<< " "<< testScore2 << " "<< testScore3 << " "<< testScore4 << " "<< testScore5<< " " << getAvg << " " << getGrade <<endl;
return 0;
}
您在 while 循环之外打印值,这意味着您永远不会保存以前的值。您的程序本质上是在说:
“将 t1 设置为测试分数第 0 行,第 0 列,将 t2 设置为第 1 行,第 0 列,等等...”
循环重新开始,现在开始:
“将 t1 设置为测试分数第 0 行第 1 列,将 t2 设置为第 1 行第 1 列,等等...”
你看到问题了吗?每次循环运行时都覆盖变量,而不保存它们。然后程序只打印最后一行,因为这是循环结束时的变量。
您可能想使用二维数组来解决这个问题。然后,您可以使用嵌套循环将每个点设置为相应的值。这些信息是否足以找出其余部分?如果不告诉我。
总体思路:
new int[行数][列数]数组;
for(int i = 0; i = 行数; i++)
for(int j=0; j=列数; j++)
array[i][j] = getline 代码在这里
闭环。
已经给出答案并被接受。
为了完整起见,我将展示您的代码的重构版本和工作版本。
我没有使用数组。只是循环和函数。
#include <iostream>
#include <fstream>
#include <iomanip>
double getAvg(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5){
double avg = (testScore1 + testScore2 + testScore3 + testScore4 + testScore5)/5.0;
return avg;
}
char getGrade(double avg){
char grade = 'F';
if (avg >= 90){
grade = 'A';
}else if (avg >= 80){
grade = 'B';
}else if (avg >= 70){
grade = 'C';
}else if (avg >= 60){
grade = 'D';
}
return grade;
}
int main(){
std::ifstream myFile("scores.csv");
if(!myFile){
std::cerr << "\nError: Could not open input file\n";
}
else {
std::cout<< std::setw(15) <<" Test scores\n--------------------\n 1 2 3 4 5 Avg Grade\n=== === === === === ===== =====\n";
int testScore1, testScore2, testScore3, testScore4, testScore5;
char comma1, comma2, comma3, comma4;
// Read all values in one line, then next line, next line and so on
while (myFile >> testScore1 >> comma1 >> testScore2 >> comma2 >> testScore3 >> comma3 >> testScore4 >> comma4 >> testScore5) {
// Calculate
double average = getAvg(testScore1, testScore2, testScore3, testScore4, testScore5);
char grade = getGrade(average);
std::cout<< " "<< testScore1<< " "<< testScore2 << " "<< testScore3 << " "<< testScore4 << " "<< testScore5<< " " << average << " " << grade << '\n';
}
}
return 0;
}