Sana J
Sana J

Reputation: 25

for loop with invalid error and looping questions

I am fairly new to C++, I am trying to write a code that allows input number of reviewers then allows number of reviewers to enter movie rating and display asterisks based on the number input. I am having difficulty incorporating an if statement that display "Movie ratings must be from 1 to 5." when the user input any number that's outside of 1 to 5. Another thing when it does work, it still continues the for loop of cout << "\nReviwer " << r << " rating: " ; instead of stopping and restarting. Any assistance is appreciated. complied code

int reviewers;
int rating;

//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;

for (int r = 1; r <= reviewers; r++) {
    cout << "\nReviwer " << r << " rating: ";
    cin >> rating;

    if (rating < 1 || rating > 5)
        cout << "Movie ratings must be from 1 to 5." << endl;

    else {
        for (int j = 1; j <= rating; j++) {
            cout << "* ";
        } //end for
        cout << endl;
    } //end if
} //end for

Output example should be like this

example of what it should look like

Upvotes: 2

Views: 142

Answers (5)

anastaciu
anastaciu

Reputation: 23832

You'd want to keep asking the number of stars if the user inputs a value outside the range, something like:

for (int r = 1; r <= reviewers; r++)
{
    cout << "\nReviwer " << r << " rating: ";
    do
    {
        cin >> rating;

        if (rating < 1 || rating > 5) // print error message
            cout << "Movie ratings must be from 1 to 5." << endl;

    } while (rating < 1 || rating > 5); // repeat the loop if out of range

    for (int j = 1; j <= rating; j++)
    {
        cout << "* ";
    } //end for
    cout << endl;
}

Note that you should be doing input validation also, e.g., if the input is an alphabetic character, your code will trigger an infinite loop, here an example of a possible solution:

#include <limits>

//...
 
do
{
    if (!(cin >> rating))
    {
        std::cout << "Bad input, try again";
        cin.clear();                                                   //clear error flags
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // clear buffer
        rating = 0;
    }
    if (rating < 1 || rating > 5)
        cout << "Movie ratings must be from 1 to 5." << endl;
} while (rating < 1 || rating > 5);

Upvotes: 1

ThomasNotTom
ThomasNotTom

Reputation: 51

Here is some code I quickly put together. I hope the comments are useful and if you have any questions just ask!

The main part is the recursive function get_ratings which will loop forever until it returns 1

// All this function does is returns the correct amount of stars
// E.G. make_stars(4) returns " * * * *"
string make_stars(int star_Count) {
    string stars;
    for (int i = 0 ; i < star_Count ; i++) {
        stars += " *";
    }
    return stars;
}
// We get the ratings and returns 1 or 0 depending of it succeeded or failed
int get_ratings(int reviewer_count) {
    // We initialise the ratings integer
    int rating;
    // We loop through all reviewers
    for (int i = 0 ; i < reviewer_count ; i++) {
        
        // We do i + 1 so it is more readable (usually in English we start at 1 not 0 like computers)
        cout << "What is reviewer " << (i + 1) << "'s rating?" << endl;
        
        //We get the user input
        cin >> rating;
        
        // We check to see if rating is withing the range. We could also do is NOT in the range and flip line 27 and 29
        if (1 <= rating && rating <= 5) {
            
            // If it is within range we will print the correct amount of stars
            cout << make_stars(rating) << endl;
        } else {
            
            // We return 0 so we can determine the function "failed"
            return 0;
        }
    }
    
    // We return 1 so we can determine the function "succeeded"
    return 1;
}

// This is a recursive function (it can run itself)
int get_ratings_rec(int reviewers) {
    cout << "All ratings must be given between 1 and 5 (inclusive)" << endl;
    
    // get_ratings_status is equal to 1/0 depending on if get_ratings() succeeded or failed
    int get_ratings_status = get_ratings(reviewers);
    
    if (get_ratings_status == 1) {
        
        // If it was a success we print "Success!"
        cout << "Success!" << endl;
    } else {
        
        // If it was a failure we tell the user and run get_ratings_loop() again until it succeeds
        cout << "Failed, please try again\n" << endl;
        get_ratings_loop(reviewers);
    }
}
// Our main entry point to the program
int main() {
    // We initialise the reviewers integer 
    int reviewers;
    
    cout << "How many reviewers?\n>>> " << endl;
    cin >> reviewers;
    
    // We run get_ratings_loop() with the integer given
    get_ratings_loop(reviewers);
}

Upvotes: 1

et11enne
et11enne

Reputation: 240

You should have


#include <iostream>

#include <string>

using namespace std;
int main(){




int reviewers;
int rating;

//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;

for (int r = 1; r <= reviewers; r++) {
    cout << "\nReviwer " << r << " rating: ";
    cin >> rating;

    while (rating < 1 || rating > 5){
        cout << "Movie ratings must be from 1 to 5." << endl;
        cout << "\nReviwer " << r << " rating: ";
        cin >> rating;
    }
    if (rating >= 1 && rating <=5){
        for (int j = 1; j <= rating; j++) {
            cout << "* ";
        } //end for
        cout << endl;
    } //end if
} //end for

}

};

It's not optimised but do the trick

Upvotes: 1

nielsen
nielsen

Reputation: 7774

I suppose you aim at something like this:

int reviewers;
int rating;

//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;

for (int r = 1; r <= reviewers; r++) {
    while(true) {
        cout << "\nReviewer " << r << " rating: ";
        cin >> rating;
        if (rating < 1 || rating > 5) {
            cout << "Movie ratings must be from 1 to 5." << endl;
        } else {
            break;
        }
    }
    for (int j = 1; j <= rating; j++) {
        cout << "* ";
    } //end for
    cout << endl;
} //end for

Upvotes: 1

Nguyen Ngoc
Nguyen Ngoc

Reputation: 76

if your mean is the reviewer must input the number between 1 and 5, you can use a do while loop like this:

do
{
    //enter rating;
}while (rating < 1||rating >5);
    //print out rating;

Upvotes: 1

Related Questions