dimanche 3 juillet 2016

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;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire