0

I'm writing a program that tells a user when they enter a negative or positive and even or odd number then the program ask a question, " Would you like to enter another number? y(es) or n(o) I need to account for the user entering in something else besides 'y' and 'n' and I need to account for if the user does not enter an integer. Last if the user selects yes, the program will need to go through the loop process of determining if they enter an integer and if its (positive or negative and odd or even)

int value;
char choice;

cout << "Please enter a number" << endl;
cin >> value;
while (!(cin >> value)) {
    cin.clear();
    cin.ignore(999, '\n');
    cout << "Invalid data type! Please enter 'value' again" << endl;
}
if (value > 0 && value % 2 == 0) {

    cout << value << " is even" << endl;
    cout << value << " is positive" << endl;
}

else if (value < 0 && value % 2 != 0) {
    cout << value << " is odd" << endl;
    cout << value << " is negative" << endl;
}

else if (value > 0 && value % 2 != 0) {
    cout << value << " is odd" << endl;
    cout << value << " is postive" << endl;
}

else if (value < 0 && value % 2 == 0) {
    cout << value << " is even" << endl;
    cout << value << " is negative" << endl;
}
cout << "Would you like to try another number? Say y(es) or n(o)" << endl;
cin >> choice;

while (choice != 'y' &&choice != 'n') {
    cin.clear();
    cin.ignore(999, '\n');
    cout << "Invalid response! Please enter 'choice' again" << endl;
}

do {
    if (choice == 'y') {
        cout << "Please enter a number" << endl;
        cin >> value;
        if (!(cin >> value)) {
            cin.clear();
            cin.ignore(999, '\n');
            cout << "Invalid data type! Please enter 'value' again" << endl;

        if (value > 0 && value % 2 == 0) {

            cout << value << " is even" << endl;
            cout << value << " is positive" << endl;
        }

        else if (value < 0 && value % 2 != 0) {
            cout << value << " is odd" << endl;
            cout << value << " is negative" << endl;
        }

        else if (value > 0 && value % 2 != 0) {
            cout << value << " is odd" << endl;
            cout << value << " is postive" << endl;
        }

        else if (value < 0 && value % 2 == 0) {
            cout << value << " is even" << endl;
            cout << value << " is negative" << endl;
        }
        cout << "Would you like to try another number? Say y(es) or n(o)" << endl;
        cin >> choice;
        }
    }
} while (choice == 'n');
    cout << "Thank you for using my program. Goodbye!" << endl;
return 0;

}

1 Answers1

0

It would take nested do-while loops to check all your conditions. Here I am using cin.fail(). cin.fail() detects whether the value entered fits the value defined in the variable.

int value;
char choice;
        do{     
            cout << "Please enter a number" << endl;
            cin >> value;
            if(cin.fail()) // check if input is int
            {
                cout<<"Not an int";
                choice = 'y';
            }
            else
            {
                if (value > 0 && value % 2 == 0) 
                {

                    cout << value << " is even" << endl;
                    cout << value << " is positive" << endl;
                }

                else if (value < 0 && value % 2 != 0) 
                {
                    cout << value << " is odd" << endl;
                    cout << value << " is negative" << endl;
                }

                else if (value > 0 && value % 2 != 0) 
                {
                    cout << value << " is odd" << endl;
                    cout << value << " is postive" << endl;
                }

                else if (value < 0 && value % 2 == 0) 
                {
                    cout << value << " is even" << endl;
                    cout << value << " is negative" << endl;
                }
                do{
                    cout << "Would you like to try another number? Say y(es) or n(o)" << endl;
                    cin >> choice;
                }while(choice != 'y' || choice != 'n');
            }
        }while (choice == 'n');

Also you should read this: Checking input value is an integer

Aditi Rawat
  • 784
  • 1
  • 12
  • 15