mercredi 10 février 2021

istream loop makes the program jump over cin statement

Here is a program that takes a sequence of space-separated numbers and stores then in a vector. The problem is when putting the cin loop before the input of x0, the program jumps the next cin statement, but the reverse is working very normally.

Can you explain to me what I am missing here?

State of jump:

vector<int> V{};
int x0{}, temp{};
        
cout << "Enter sequence\n";
for(temp;cin>>temp;)
{
    if (!isalpha(temp)) V.push_back(temp);
    else
    {
        cin.clear();
        break;
    }
}
cout<<"enter x0 : ";
cin >> x0;// the program skips this!

The working reverse:

vector<int> V{};
int x0{}, temp{};
cout<<"enter x0 : ";
cin >> x0;
cout << "Enter sequence\n";
for(temp;cin>>temp;)
{
    if (!isalpha(temp)) V.push_back(temp);
    else
    {
        cin.clear();
        break;
    }
}

Aucun commentaire:

Enregistrer un commentaire