[英]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.