I'm currently taking a programming logic and design class and we're using C++ for our homework. I'm still a very fresh beginner - having trouble with assignment #9.
This is part of a larger assignment (using functions: enter six scores [0-100 range], drop the lowest score, calculate the average of the remaining five) - right now my "findLowest" function is returning "2" instead of whatever the real lowest score entered is.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
//list of current functions
void getScore(); //function for user to enter six scores
int findLowest(int); //function for finding lowest of six scores
int main ()
{
//declare
int lowest;
getScore();
findLowest(lowest);
cout << "\n" << endl; //making space in test runs, not important
cout << "Lowest score is: " << lowest << endl; //hoping to display the lowest score
system("PAUSE");
return 0;
}
//getScore function
void getScore()
{
int testScore = 0;
int counter = 0;
//writes to a file
ofstream scoreInput;
scoreInput.open("scores.txt");
cout <<"Please enter the six test scores: " <<endl;
//loop for entering and writing to file six score
do{
cin >> testScore;
counter = ++counter;
scoreInput << testScore <<endl;
}
while (counter < 6);
scoreInput.close();
}
//findLowest function, return the lowest score
int findLowest(int)
{
int testScore;
int lowest = 100;
//opening file created earlier, reading the scores
ifstream scoreInput;
scoreInput.open("scores.txt");
//trying to find the lowest score entered on file
while(scoreInput>>testScore)
{
cout <<testScore<<endl;
}
if (testScore < lowest)
{
lowest = testScore;
}
else
{
lowest = lowest;
}
return lowest;
}
Currently I'm working on entering the scores and finding the lowest score, once that is done there is still more to build. I'm just currently stuck on getting this part to work before I move on.
Any help on that subject would be awesome.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
//list of current functions
void getScore(); //function for user to enter six scores
int findLowest(int); //function for finding lowest of six scores
int main () {
int lowest = 0;
getScore();
lowest = findLowest(lowest);
cout << "\n" << endl; //making space in test runs, not important
cout << "Lowest score is: " << lowest << endl; //hoping to display the lowest score
cin.get();
return 0;
}
//getScore function
void getScore()
{
int testScore = 0;
int counter = 0;
//writes to a file
ofstream scoreInput;
scoreInput.open("scores.txt");
cout <<"Please enter the six test scores: " <<endl;
//loop for entering and writing to file six score
do{
cin >> testScore;
counter = ++counter;
scoreInput << testScore <<endl;
}
while (counter < 6);
scoreInput.close();
}
//findLowest function, return the lowest score
int findLowest(int lowest)
{
int testScore;
//opening file created earlier, reading the scores
ifstream scoreInput;
scoreInput.open("scores.txt");
scoreInput >> lowest;
//trying to find the lowest score entered on file
while(scoreInput>>testScore)
{
if (lowest > testScore) {
lowest = testScore;
}
}
return lowest;
}
You had several problems. You didn't assign a variable to your function findLowest() to collect the return value from the function. Also, Make sure you do you if statement checking inside the while loop so you can check each value from the file.