Why my C++ code is not taking input ? What's wrong with my code?

Viewed 793

I am currently learning C++ and completely newbie to programming. I came across a question and tried solving but ended up with an undesired output. Then with the help of python tutor I tried visualizing the execution of my code , where even after running cin >> q it isn't taking input and displaying an error while executing the next statement where in I tried using the q.

Please, Can anyone tell where is the problem and what makes such errors occur?

Click here to see the problem I tried solving

My C++ Code

#include <iostream>
#include<vector>
using namespace std;

int main() {
    int q,t1,t2;
    vector<int> v;
    cin>>q;
    while(q--){
        if(cin >> t1>>t2){
            if(t1==0)
            {
                v.push_back(t2);
            }
            else if(t1==1)
            {
                cout << v[t2] <<"\n";
            }
        }
        else if(cin >> t1){
            if(t1==2)
            v.pop_back();
        }
        
    }
    return 0;
}

Error I got while visualizing my code in python tutor

2 Answers

I don't know what you mean when you say that cin isn't working. There's nothing in your code that would make it not work.

However your code has bugs. This code

    if(cin >> t1>>t2){
        if(t1==0)
        {
            v.push_back(t2);
        }
        else if(t1==1)
        {
            cout << v[t2] <<"\n";
        }
    }
    else if(cin >> t1){
        if(t1==2)
        v.pop_back();
    }

assumes that you can try to read two numbers from a line, and if that doesn't work go back and try and read one number from the same line. cin doesn't work like that, if you write cin >> t1>>t2 the program will read two numbers, from as many lines as it needs to in order to find two numbers.

Here's how to write the code correctly.

cin >> t1;
if (t1 == 0)
{
    cin >> t2;
    v.push_back(t2);
}
else if (t1 == 1)
{
    cin >> t2;
    cout << v[t2] <<"\n";
}
else if (t1 == 2)
{
    v.pop_back();
}

See how that works? Read the first number, and then read the second number separately if you need to. With this code you can still enter both numbers on the same line if that's what you want to do.

From test input provided by Ala in her links, which is:

8
0 1
0 2
0 3
2
0 4
1 0
1 1
1 2

correct output should be

1
2
4

Her program above yields 2 instead. This is because each occurrence of \n and cin in the code flushes the buffer of cout, and the output gets printed while the user still inputs numbers.

To avoid this, and to get all the output at end program, we can just push the output in a second vector, and then cout its contents at the end:

#include <iostream>
#include <vector>

using namespace std;

int main() {
    int q,t1,t2;
    vector<int> v, w;
    cin >> q;
    while(q--){
        cin >> t1;
        if (t1 == 0)
        {
            cin >> t2;
            v.push_back(t2);
        }
        else if (t1 == 1)
        {
            cin >> t2;
            w.push_back(v[t2]);   // new
        }
        else if (t1 == 2)
            v.pop_back();
    }

    // new
    for (int i = 0; i != v.size(); ++i)
        cout << w[i] << endl;

return 0;
}

This will yield the correct output.

Related