[英]Reading from a text file and then using the input in the program
我想让一个程序读取带有5个两位数数字的文本文件,这些数字在单独的行上。 然后,我想将那些数字放置在程序中,在其中将删除最低的数字,然后对剩余的4个数字求平均,然后在屏幕上输出。
该文本文件只是具有以下内容的记事本.txt文件:
83
71
94
62
75
我已经成功构建了该程序,可以在其中手动输入数字,并且可以按我的意愿进行操作,但是我在代码中使用文件的知识有限。 我已经读过关于向量的知识,但是我想先让它简单一点,然后再尝试学习其他知识之前,先去掌握它。 我试图建立一些类似于我认为应该看起来像的代码。 我的问题是:为什么在调试时出现““错误C2082:重定义形式参数””?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//function prototypes
void getScore(int s1, int s2, int s3, int s4, int s5);
void calcAverage(int s1, int s2, int s3, int s4, int s5);
int findLowest(int s1, int s2, int s3, int s4, int s5);
int main()
{
getScore(0, 0, 0, 0, 0);
return 0;
}
//function to collect the 5 test scores
void getScore(int s1, int s2, int s3, int s4, int s5)
{
string line1[30];
string line2[30];
string line3[30];
string line4[30];
string line5[30];
ifstream myfile("grades.txt");
int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
int s5 = 0;
if(!myfile)
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
while(!myfile.eof())
getline(myfile,line1[s1],'\n');
cin >> s1;
getline(myfile,line2[s2],'\n');
cin >> s2;
getline(myfile,line3[s3],'\n');
cin >> s3;
getline(myfile,line4[s4],'\n');
cin >> s4;
getline(myfile,line5[s5],'\n');
cin >> s5;
calcAverage(s1, s2, s3, s4, s5);
}
//function to calculate the average of the 4 highest test scores
void calcAverage(int s1, int s2, int s3, int s4, int s5)
{
int average;
int lowest;
lowest = findLowest(s1, s2, s3, s4, s5);
average = ((s1 + s2 + s3 + s4 + s5) - lowest)/4;
cout << endl;
cout << "The average of the four highest test scores is: ";
cout << average << endl;
}
//function to find the lowest test score
int findLowest(int s1, int s2, int s3, int s4, int s5)
{
int lowest = s1;
if (s2<lowest)
lowest = s2;
if (s3<lowest)
lowest = s3;
if (s4<lowest)
lowest = s4;
if (s5<lowest)
lowest = s5;
return lowest;
return 0;
}
生成结果:
1>------ Build started: Project: droplowest, Configuration: Debug Win32 ------
1> lowest drop.cpp
1>c:\users\ldw\documents\visual studio 2010\projects\droplowest\droplowest\lowest drop.cpp(33): error C2082: redefinition of formal parameter 's1'
1>c:\users\ldw\documents\visual studio 2010\projects\droplowest\droplowest\lowest drop.cpp(34): error C2082: redefinition of formal parameter 's2'
1>c:\users\ldw\documents\visual studio 2010\projects\droplowest\droplowest\lowest drop.cpp(35): error C2082: redefinition of formal parameter 's3'
1>c:\users\ldw\documents\visual studio 2010\projects\droplowest\droplowest\lowest drop.cpp(36): error C2082: redefinition of formal parameter 's4'
1>c:\users\ldw\documents\visual studio 2010\projects\droplowest\droplowest\lowest drop.cpp(37): error C2082: redefinition of formal parameter 's5'
1>c:\users\ldw\documents\visual studio 2010\projects\droplowest\droplowest\lowest drop.cpp(42): error C2562: 'getScore' : 'void' function returning a value
1> c:\users\ldw\documents\visual studio 2010\projects\droplowest\droplowest\lowest drop.cpp(14) : see declaration of 'getScore'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
在这里看看。
myfile.open();
if(!myfile.is_open())
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
没有遍历整个代码。 但是正如错误所暗示的那样,您已经重新声明了int s1,s2,s3,s4,s5。
尝试更改以下内容
ifstream myfile("grades.txt");
int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
int s5 = 0;
至
ifstream myfile("grades.txt");
s1 = 0;
s2 = 0;
s3 = 0;
s4 = 0;
s5 = 0;
由于这些变量已经在功能参数列表中声明。
void getScore(int s1, int s2, int s3, int s4, int s5)
{
...
您不能声明它们。 它在C ++中是不允许的。
您会收到有关重新定义形式参数的错误,因为在getScore
您正在重新定义s1
到s5
。 这五个分数作为getScore
参数getScore
,因此您不必再次定义它们。 您可以只使用它们。
要使程序从文件中读取而不是您手动输入分数,只需更改每个cin >> s#;
到myfile >> s#;
(将#
替换为实际数字1-5)。 cin
和myfile
都是流,因此您可以以相同的方式使用它们。 他们只是从不同的地方阅读。
我将把getScore
函数更改为如下形式:
//function to collect the 5 test scores
void getScore(int s1, int s2, int s3, int s4, int s5)
{
ifstream myfile("grades.txt");
if (!myfile)
{
cout << "Error opening output file" << endl;
system("pause>nul");
return;
}
myfile >> s1;
myfile >> s2;
myfile >> s3;
myfile >> s4;
myfile >> s5;
calcAverage(s1, s2, s3, s4, s5);
}
请注意,我更改了您的return -1;
刚return;
。 这是因为getScore
被定义为不返回任何内容( void
)。 因此,您无需返回任何东西。 另外,您不需要所有的getline
。
还要注意,在findLowest
函数中,最后有多个返回。 仅将使用第一个,因为返回表示函数结束。 我将这样删除第二个:
//function to find the lowest test score
int findLowest(int s1, int s2, int s3, int s4, int s5)
{
int lowest = s1;
if (s2 < lowest)
lowest = s2;
if (s3 < lowest)
lowest = s3;
if (s4 < lowest)
lowest = s4;
if (s5 < lowest)
lowest = s5;
return lowest;
}
为了使程序更易于修改(如果以后要读取/处理更多数字该怎么办?),您需要使用固定大小的数组(而不是向量),如下所示:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int NUM_SCORES = 5;
//function prototypes
void getScores(string filename);
void calcAverage(int* scores);
int findLowest(int* scores);
int main()
{
getScore("grades.txt");
return 0;
}
//function to collect the test scores
void getScores(string filename)
{
int scores[NUM_SCORES] = { 0 }; // initialize all of the scores to 0.
ifstream myfile(filename);
if(!myfile)
{
cout << "Error opening output file." << endl;
system("pause>nul");
return;
}
int i;
for (i = 0; i < NUM_SCORES && !myfile.eof(); i++)
myfile >> scores[i];
if (i != NUM_SCORES)
{
// this means that there weren't enough numbers in the file.
cout << "Not enough numbers in the file." << endl;
system("pause>nul");
return;
}
calcAverage(scores);
}
//function to calculate the average of the every test score but the lowest
void calcAverage(int* scores)
{
int lowest = findLowest(scores);
int sum = 0;
for (int i = 0; i < NUM_SCORES; i++)
sum += scores[i];
sum -= lowest;
int average = sum / (NUM_SCORES - 1);
cout << endl << "The average of the four highest test scores is: " << average << endl;
}
//function to find the lowest test score
int findLowest(int* scores)
{
int lowest = scores[0];
for (int i = 1; i < NUM_SCORES; i++)
if (scores[i] < lowest)
lowest = scores[i];
return lowest;
}
还要注意,在calcAverage
,您不需要在函数开始时定义所有变量。 在C ++中,您可以在块中的任何位置定义它们。
在函数原型中,您仅具有形式参数的数据类型。
void getScore(int,int,int,int,int)
并且已经将它们重新定义为int s1=0;
。 只要做s1=0;
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.