Home > Net >  I can't figure this out there is an error
I can't figure this out there is an error

Time:10-05

My teacher says to create a stack overflow account so this is not cheating. If my rating is 1-5 the code is good. But if not, there is an error. I also have a warning using uninitialized memory c and b.

Task:

write a C program that allows a user to enter their rating of the three movies in the dark knight trilogy: batman begins the dark knight the dark knight rises

rating must be between 1 and 5. If a rating is less than 1 or more than 5, display an appropriate error message, and ask for a new rating.

After all the ratings have been entered, display each rating entered the highest rating, the lowest rating, and the average of the ratings.

requirements:

your program must include comments explaining your methodology

The output should look like this

this is my code:

#include <iostream>

using namespace std;

int SmallestNum(int a, int b, int c) {

    if ((a <= c) && (a <= b)) {
       int min;
       min = a;
       return min;
    }
    else if ((b <= c) && (b <= a)) {
        int min;
        min = b;
        return min;
    }
    else if ((c <= b) && (c <= a)) {
        int min;
        min = c;
        return min;
    }
}

int BiggestNum(int a = 0, int b = 0, int c = 0) {

    if ((a >= c) && (a >= b)) {
        int max;
        max = a;
        return max;
    }
    else if ((b >= c) && (b >= a)) {
        int max;
        max = b;
        return max;
    }
    else if ((c >= b) && (c >= a)) {
        int max;
        max = c;
        return max;
    }
}

int main() {

    int a, b, c, d, e;

    cout << "Enter your rating for the following movies:  " << endl;
    cout << "Batman Begins - Rating (1-5): ";
    cin >> a;
    if ((a >= 0) && (a <= 5)) {
        cout << "The Dark Knight - Rating (1-5): ";
        cin >> b;
    }
    else {
        cout << "Rating must be between 1 and 5. Please re-enter: ";
        cin >> a;
    }
    if ((b >= 0) && (b <= 5)) {
        cout << "The Dark Knight Rises - Rating (1-5): ";
        cin >> c;
    }
    else {
        cout << "Rating must be between 1 and 5. Please re-enter: ";
        cin >> b;
    }
    if ((c >= 0) && (c <= 5)) {

        d = BiggestNum(a, b, c);
        e = SmallestNum(a, b, c);

        cout << " You entered the following ratings:" << endl;
        cout << " Batman Begins: " << a << endl;
        cout << " The Dark Knight: " << b << endl;
        cout << " The Dark Knight Rises: " << c << endl;
        cout << endl;
        cout << " The higest rating was: " << d << endl;
        cout << " The lowest rating was: " << e << endl;
        cout << " The average rating was: " << (a   b   c) / 3 << endl;
        cout << endl;
    }
    else {
        cout << "Rating must be between 1 and 5. Please re-enter: ";
        cin >> c;
    }
    return 0;
}

CodePudding user response:

The comments have already raised a number of good and important points. Since this is a homework question, I am merely going to refactor the 'query for rating' part.

If there is anything you cannot understand about it, feel free to ask.

const char *movieNames[] = 
{
    "Batman begins",
    "The dark knight",
    "The dark knight rises"
};
int ratings[3] = { 0, 0, 0 };

cout << "Enter your rating for the following movies:  " << endl;
for (int i = 0; i < 3;   i)
{
    bool ratingIsValid = false;
    while (!ratingIsValid)        //Repeats until a valid rating has been entered.
    {
        cout << movieNames[i] << "- Rating (1-5): ";
        cin >> ratings[i];
        ratingIsValid = ratings[i] > 0 && ratings[i] <= 5;
        if (!ratingIsValid)
            cout << "Rating must be between 1 and 5. Please re-enter: ";
    }
}

Once this loop is through, you can determine your ranking. Please note, that I did not testrun this.

CodePudding user response:

You can get the job done with a simple while loop.

#include <iostream>
using namespace std;
    
    int SmallestNum(int a, int b, int c) {
    
        if ((a <= c) && (a <= b)) {return a;}
        else if ((b <= c) && (b <= a)) {return b;}
        else if ((c <= b) && (c <= a)) { return c;}
    }
    
    int BiggestNum(int a = 0, int b = 0, int c = 0) {
    
        if ((a >= c) && (a >= b)) {return a;}
        else if ((b >= c) && (b >= a)) {  return b;}
        else if ((c >= b) && (c >= a)) { return c; }
    }
    
    int main() {
    
        int a=0, b=0, c=0;
    
        cout << "Enter your rating for the following movies:  " << endl;
        cout << "Batman Begins - Rating (1-5): ";
        cin >> a;
        while (std::cin.fail() || (a <= 0 || a > 5 )) {
            cout << "Rating must be between 1 and 5. Please re-enter for Batman Begins - Rating (1-5):";
            cin >> a;
        }
            cout << "The Dark Knight - Rating (1-5): ";
            cin >> b;
        
            while (std::cin.fail() || (b <= 0 || b > 5)) {
                cout << "Rating must be between 1 and 5. Please re-enter for The Dark Knight - Rating (1-5):";
                cin >> b;
            }
            cout << "The Dark Knight Rises - Rating (1-5): ";
            cin >> c;
            while (std::cin.fail() || (c <= 0 || c > 5)) {
                cout << "Rating must be between 1 and 5. Please re-enter for The Dark Knight Rises - Rating (1-5):";
                cin >> c;
            }
        
            cout << " You entered the following ratings:" << endl;
            cout << " Batman Begins: " << a << endl;
            cout << " The Dark Knight: " << b << endl;
            cout << " The Dark Knight Rises: " << c << endl;
            cout << endl;
            cout << " The highest rating was: " << BiggestNum(a, b, c) << endl;
            cout << " The lowest rating was: " << SmallestNum(a, b, c) << endl;
            cout << " The average rating was: " << (a   b   c) / 3 << endl;   
        return 0;
    }

Notes: You don't need maxand min variables inside functions. Just return a; return b; return c; You can write as.If you say a>=0, you will make 0 a valid value and this does not match your scenario.You don't need the d and e variables either.

  •  Tags:  
  • c
  • Related