Hi ~ As you can see from the code below I am in school for programming. I have come across this homework assignment a few times on the site already however the requirements for mine are different. I cannot use arrays.

Three functions - I only have two coded. The void calcAverage prototype is not causing an error and the program compiles and runs asking the using for the five test scores with no problem.

I cannot find the error in passing the 'lowest' score value. Upon the user (me) entering the five scores, the lowest score value is always 0.

I have been working on this darn thing for a week and a half, think I have it figured out but this is stumping me. Why the heck isn't my lowest score of five entered showing on output? I have not added the third function because I wanted to test this first.

Any help will be very appreciated!!

Thanks!

#include <iostream>
using namespace std;

void getScore (int &);
int findLowest (int);
void calcAverage(int);

int main ()
{

    static int refscore, lowest;

    for(int count = 1; count <= 5; count++)
    {
        getScore(refscore);
        int findLowest(lowest);

    }

    cout << "The lowest score is: " << lowest << endl;

    system("pause");
    return 0;
}

void getScore(int &refscore)
{
    if(refscore >= 0 && refscore <= 100)
    {
        cout << "Please enter a test score: ";
        cin >> refscore;
    }
    if(refscore < 0 || refscore > 100)
    {
        cout << "Invalid Entry. Please enter a test score ";
        cout << "between 0 and 100. ";
        cin >> refscore;
    }
}


int findLowest(int lowest)
{
    static int refscore;

    getScore(refscore);

    lowest = refscore;

    if(refscore > lowest)
        lowest = lowest;

    return lowest;
}

Recommended Answers

All 8 Replies

When you return the value from findLowest() you aren't assigning that value to anything. You should have in main:

lowest = findLowest(lowest);

Setting the variable "lowest" to anything in the function findLowest() will not affect your global variable "lowest" because you did not pass it in by reference. You are only editing a local varible in the scope of the findLowest() function.

When you return the value from findLowest() you aren't assigning that value to anything. You should have in main:

lowest = findLowest(lowest);

Setting the variable "lowest" to anything in the function findLowest() will not affect your global variable "lowest" because you did not pass it in by reference. You are only editing a local varible in the scope of the findLowest() function.

Thank you for replying. Unfortunately we are not allowed to use global variables hence the static vars. I guess I messed that up too because I have two in main. Darn it!

Main is supposed to call getScore five times. getScore should use a reference variable. This part seems to be working. I have until Tuesday...*sigh*.

Also, I did try lowest = findLowest(lowest) in another version of the program and got an error basically telling me that I was using the function incorrectly.

Can you pass a static variable in a function parameter? I did try this only to get an error as well.

Thanks.

There is no need to for any static variables, use local variables instead by removing the static keyword. Also, maybe you should use a do-while loop when checking the input in getScore? What happens if the user inputs an incorrect value twice? There is no point in checking the argument to getScore before the user has been able to input any value. I do not think you ment to call getScore inside the findLowest function?? By doing this you are asking the user for 10 values..

One other thing to note is that the first value the user inputs will, at the time, also be the lowest value.

first :
cout << "The lowest score is: " << lowest << endl;
lowest is not inialized so it will be automatically inialized 0 so whatever values you write will be zero so you should call function

cout << "The lowest score is: " << findLowest(lowest) << endl;

second :
the function findLowest has a mistake in that it compares the last two values only and sets the lowest you can correct this by making the compiler compare every two values in list by making a variable that will take the lowest and compare it with next value
please mark the thread as solved if you've got the help you need :)

One other thing to note is that the first value the user inputs will, at the time, also be the lowest value.

Right...also I did get that - about the ten values - I have been all over the place with this. One way I did not go was with the do loop. I actually looked at that early this am but thought since I know the number of iterations that the for loop would be preferable since our book has said you generally use a for loop for situations where you know the number of iterations.

I think I'm a bit fried...As I said I have been working on this for 5 days straight from 8 am until I go to bed. A couple of breaks a day for food and FB (lol) but that's it. Think I need to walk away for a minute. When I get back to it I am going to take your suggestion and try the Do Loop. Global vars would be so much easier...(I think!).

Thanks for replying! Will post my updated code asap!

The for loop is correctly used, but when you ask the user for input in getScore you do not know how many times the user will input an incorrect value. Therefore it could be a good idea to do something like this:

int input = 0;
do {
  cout << "Input value: ";
  cin >> input;
} while (input < 0 || input > 100);

if the user inputs an incorrect number. A normal while loop would also work.

Thank you to everyone who helped me with this part. I re-read the text on static, reference and parameter vars, passing values - ok..the whole chapter and have it actually working now. *whew!!*

Now on to the final module - calculating the average of the five and displaying. Wish me luck!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.