Help with function returning wrong value

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace 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.

Thanks!
Last edited on
Also - not as important - how do you make the code in the box that scrolls instead of just all posted long format?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace 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.
Last edited on
Thank you! I've made the changes you suggested and it worked!
But for this part : "lowest = findLowest(lowest);" - I'm embarrassed I missed that!

The other points you made, I will keep that in mind going forward.


Thank you again!
Topic archived. No new replies allowed.