Thread: cin with conditions

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    203

    cin with conditions

    I want to keep inputting data into vector<int> until it's full.

    My code is below but I have to input an extra int before the while () loop terminates with the right size vector and the program moves on to the next steps. I've tried cin.ignore() as shown to remove the newline character but can't seem to make it work. Any helps always appreciated, thanks.

    PS: I understand there are several other ways of initializing the vector (for e.g. auto-initializing this small size vector or a do ... while loop that seems to work) but I'm focusing on getting my use of cin right here. Thanks

    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    
    constexpr auto QUARTERS = 4;
    
    int main(){
    
    vector<int> v ;
    int temp {0};
    
    cout<<"Please enter the sales numbers"<<endl;
    
    while ((cin>>temp) && (v.size() < QUARTERS)){
        v.push_back(temp);
       //     cin.clear();
        cin.ignore(1000,'\n');
        }
    
        cout<<"The size of the vector is: "<<v.size()<<endl;
        cout<<"The elements of the vector are: "<<endl;
        for(auto &i : v){
            cout<<i<<endl;
        }
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,480
    If you reverse the order of your while() conditions it may work as you expect. By the way since you're retrieving numbers you don't really need the ignore().

    Code:
    while(v.size() < QUARTERS && cin >> temp)
    {
    ...

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Indeed it does, many thanks.

    But now I'm trying to explain this back to myself for the way I'd written the logical-and expressions: the program comes back to the while loop for the 5th time and because (cin >> temp) occurs first, a number is read from the input stream and stored in temp and only then the program finds out that we've already filled up the vector. Fine, in that case shouldn't the program still break out of the while loop since && evaluates as FALSE?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Regardless of other reasons to not run the statement, cin >> temp; has to finish what it's doing before the stream state can be evaluated. The act of reading an integer and whether it failed or not has bearing on the state of the stream; i.e. true if good, false if bad.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,480
    And remember you are testing size() before you have altered the state of the vector. The first time through the loop the size is zero, it doesn't go to 1 until after the push_back() call. Also note that with a multiple condition control statement using "and" if the first statement evaluates false the second statement is not evaluated.

    Jim

  6. #6
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Thanks to both, this was vexing me for some time and you have pointed me in the right direction. Appreciate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if conditions
    By RyanC in forum C Programming
    Replies: 4
    Last Post: 08-12-2015, 06:29 AM
  2. Some questions about conditions
    By student111 in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2012, 02:47 AM
  3. if and while conditions
    By firehydrant in forum C Programming
    Replies: 12
    Last Post: 03-05-2011, 03:07 AM
  4. How to set conditions for a input?
    By QuaX in forum C Programming
    Replies: 7
    Last Post: 04-20-2009, 05:05 AM
  5. IF statement - to conditions
    By cornacum in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 08:39 PM

Tags for this Thread