Exercise - Book Programming-Principles-and-Practice-Using-C++

Discussion in 'C++' started by Mauriciocuri, Apr 19, 2021.

  1. Mauriciocuri

    Mauriciocuri New Member

    Joined:
    Apr 18, 2021
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    I'm Brazilian. I try to implement a very simple exercise from the book Principles and Practices of Programming with C ++, the statement is as follows:
    1. Write a program that consists of a while loop that reads (at each loop iteration) two ints and then prints them out. End the program when a '|' is read.

    Here is my code:
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    int main()
    {
        vector<int>num;
        int temp;
        int i=0;
        cout << "Write 2 numbers: \n";
        while ( cin >> temp )
        {
            
            cout << "First number = " << temp << "\n Write the second: ";
            num.push_back(temp);
            ++i;
            cin >> temp;
            num.push_back(temp);
            cout << "\n The numbers: " << num[i-1] << " and " << num[i];
            cout << endl;
            ++i;
        }
    }
    I would like to complement my program so that the program ends with the typing "|". The problem is that my program ends when I type any character other than numbers.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice