0x1000001
0x1000001

Reputation: 137

C++: Loop behaves differently after control statement

After the user enters a response to whether or not to continue, the program prints the remaining prompts after an affirmative user response. Before I incorporated this control statement, the loop waited for each user response. Afterwards, the remaining set of prompts are outputted to the console without regard to any user input.

How do I incorporate a control statement while maintaining the original functionality?

using namespace std;

int main (){
    bool cont1, cont2;
    string items [10];
    float budget;
    float prices [10];
    int i;
    string  y, n;

    for ( i = 0; i < 10; i++ ){
        cout << "Enter item name: "<<endl;
        cin >> items[i];
        cout << "Enter item price: "<<endl;
        cin >> prices [i];
        cout << "Enter another item? (y/n): "<<endl;
        cin >> cont2;

        if ( (tolower(cont2)) == 'y' ){
            break;
        }

        else if ( (tolower(cont2)) != 'n' && (tolower(cont2)) != 'y'){              
            cout << "Please enter y(es) or n(o)." << endl;
        }
    }
}

Upvotes: 1

Views: 74

Answers (1)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

 cin >> cont2;

Expects an input that can be mapped to bool, like 0,1,false or true.

If you want to check for 'y' or 'n' as input use

   char cont1, cont2;
// ^^^^

as data type.


Whatever cont1 is supposed to be used

Upvotes: 3

Related Questions