简体   繁体   中英

Reading from a text file and then using the input in the program

I want to have a program read a text file with 5 double digit numbers, that are on separate lines. I want to then have the those numbers placed in the program where the lowest number will be dropped and the remaining 4 numbers will be averaged and then output on the screen.

The text file is just a notepad .txt file with:

83
71
94
62
75

I have successfully constructed the program where I can manually enter the numbers and it will do as I wanted, but my knowledge of using files within the code is limited. I have read about vectors but I would like to keep it simple first and get the hang of it before I try to learn something else. I have tried to set up a some code that resembles what I think it should probably look like. My question is, : Why am I getting a ""error C2082: redefinition of formal parameter"" with my debugging?

#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;
}

Build results:

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 ==========

Have a look here .

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

Didn't go through the entire code. But as the error suggests u have re-declared the int s1 , s2 , s3 , s4, s5.

Try changing the following

ifstream myfile("grades.txt");
int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
int s5 = 0;

To

ifstream myfile("grades.txt");
s1 = 0;
s2 = 0;
s3 = 0;
s4 = 0;
s5 = 0;

As these variables are already declared in the function parameter list.

void getScore(int s1, int s2, int s3, int s4, int s5)
{
  ...

You cannot declare them. Its not allowed in C++.

You are getting the error about redefinition of formal parameters because in getScore you are redefining s1 through s5 . The five scores are given as arguments to getScore , so you don't have to define them again. You can just use them.

To have your program read from the file instead of you manually entering the scores, simply change each cin >> s#; to myfile >> s#; (where you replace # with the actual numbers 1-5). cin and myfile are both streams, so you can use them in the same way. They just read from different places.

I would change your getScore function to look like this:

//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);
}

Note that I changed your return -1; to just return; . This is because getScore is defined as returning nothing ( void ). So you have to return nothing. Also, you don't need to have all of the getline s.

Note also that in your findLowest function, you have multiple returns at the end. Only the first one will be used, since a return means that that is the end of the function. I would remove the second one like this:

//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;
}

In order to make your program easier to modify (what if you want to read/process more numbers later?), you would want to use fixed-size arrays (not vectors) like this:

#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;
}

Also note that in calcAverage , you don't need to define all of your variables at the beginning of the function. In C++, you can define them anywhere in a block.

In the function prototype you just have the datatype of the formal parameters.

void getScore(int,int,int,int,int)

And you have redefined them into int s1=0; . Just do s1=0; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM