Gavin C.
Gavin C.

Reputation: 105

How do I make an if statement that repeats itself if the requirements are still not met?

Here's my code.

#include <iostream>

using namespace std;

int main()
{

    int i = 0;

    int players;
    int silenceAmount;
    int bleedAmount;
    int bleedDays;
    int playersAlive;
    int playersDead;
    int playersAllowed = 6;

    cout << "How many players are playing (can only be " << playersAllowed << ")? ";
    cin >> players;
    if (players > playersAllowed) (
            cout << "There an only be " << playersAllowed << " players. Please select another number. ");
            cin >> players;

    cout << "There are " << players << " players.\n\n";



    return 0;

}

this works for only one time and I want it to work until it gets a number less than or equal to 6.

Upvotes: 1

Views: 65

Answers (1)

ChrisMM
ChrisMM

Reputation: 10022

Repeating if statements are generally done as a while loop. In your code, when you have

if (players > playersAllowed)

You should simply change it do

while (players > playersAllowed)

Also, while I'm at it, the syntax you have for your if statement is not correct for what you are trying to do. You should replace ( and ) by { and } respectively. Also, the ending bracket is in the incorrect place.

In the end, your loop would be something like this:

while (players > playersAllowed) {
    cout << "There can only be " << playersAllowed << " players. Please select another number: ";
    cin >> players;
}

Note that this doesn't take into account someone entering jfksdjfs for a number.

Upvotes: 3

Related Questions