Not sure what to change to let a char break statement

时间:2016-02-12 21:24:25

标签: c++ loops while-loop

I'm setting up my while loop and it's supposed to break off if you enter 'q' or 'Q', only way i could get it to work is if I set "number" to a Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=? instead of an char. If I change it to char, everything goes full on whacky. Here's what it looks like. Currently if i press 'q' to break, it goes into an infinite rambling

int

I need it to be something along the lines of;

    int maximum = 0, sum = 0, odd = 0, even = 0, minimum = 0, count = 0;
    int number;
    char q, Q;


    while (1)
    {
        cout << "Enter a series of number, then type Q to process: "; cin >> number;

        if (number == 'q')
            break;

Also, to make a variable undefined, it was giving me errors. I need if (number == 'q' | | number == 'Q') cout << "No Numbers were pressed"; break; and maximum to be undefined so if the user doesn't go above 0 for either it wont say their maxinmum number pressed was 0. Any ideas?

3 个答案:

答案 0 :(得分:1)

std::cout will not read a character into an int variable. In other words, this code will never work. You can either change the input to a string and parse it into an int (stoi is useful here) or have two separate reads, one into a char and one into an int.

答案 1 :(得分:1)

You need to understand how stream operators work. --trust-model pgp|classic|direct|always|auto Set what trust model GnuPG should follow. The models are: always Skip key validation and assume that used keys are always fully trusted. You generally won't use this unless you are using some external validation scheme. This option also suppresses the "[uncertain]" tag printed with signature checks when there is no evidence that the user ID is bound to the key. Note that this trust model still does not allow the use of expired, revoked, or disabled keys. will want your input to be numeric (and decimal for that matter). If you give it cin >> number, it doesn't accept it, and will not modify Q.

If you want your input to be both numeric and string, you will have to first read it is an number, check the string for being equal std::string (NB - as a string, in quotes, not as a character!), and if it is not, convert string to integer - in C++11 you can use "q".

答案 2 :(得分:-1)

you need

nil