c++ - 从文本文件中读取,然后在程序中使用输入

标签 c++ arrays visual-studio-2010

我想让一个程序读取一个包含 5 个双位数的文本文件,它们在不同的行上。然后我想将那些数字放在程序中,其中最小的数字将被删除,其余 4 个数字将被平均,然后在屏幕上输出。

文本文件只是一个记事本 .txt 文件,包含:

83
71
94
62
75

我已经成功构建了可以手动输入数字的程序,它会按照我的意愿运行,但我对在代码中使用文件的知识有限。我已经读过有关 vector 的内容,但我想先保持简单并在尝试学习其他东西之前掌握它。我试图设置一些类似于我认为它应该看起来像的代码。我的问题是:为什么我在调试时收到“错误 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 ==========

最佳答案

看看here .

myfile.open();
if(!myfile.is_open()) 
{
  cout<<"Error opening output file"<<endl;
  system("pause");
  return -1;
}

关于c++ - 从文本文件中读取,然后在程序中使用输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17453592/

相关文章:

c++ - 数据越大,内存分配错误

c++ - 二进制到格雷码,反之亦然

c++ - jpeg_decompress_struct 结构大小不匹配

计算字符串中某个数字出现的次数

.net - 在 Visual Studio 中使用 Com Interop 选项注册可以,但直接调用 Regasm 则不行

c# - 连一个简单的方法都无法内联(?)

c++ - 在一行中将 int 写入 std::ofstream::write()

php - 数组合并问题

javascript - 我无法让我的 javascript 数组工作了

visual-studio-2010 - 在另一个源代码管理项目中引用源代码管理项目(使用TFS)