1

I'm very new to C++ and im trying to create a program that takes in request from user like this:

i7: 43 2 21 A

basically the format it has to follow is after i there should be a number without any space followed directly by : and then spaces between the numbers and finally the last character should be A.. User can choose to input as many numbers as they want in between : and A. and if the input is any different from this format, user gets an error.

I somehow managed to get some part done (although still need to figure out what to do about the whole spacing thing) but i am not able to store that last character A idk why... Here is what I have done so far.

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

int main()
{
    int size;
    vector<long double> nums;
    long double numbers;
    char c1, c2, c3;
    if (cin >> c1)
    {
        if (cin >> size >> c2 && c1 == '<' && c2 == ':')
        {
            while (cin >> numbers)
                nums.push_back(numbers);
            cin>>c3;  //this doesn't work or even get called
            cout<<c3<<endl;
            for(int i=0;i<nums.size();i++)
            {
               cout<<nums[i]<<endl;
            }


        }
        else{
            cout<<"error"<<endl;
        }
    }

    return 0;
}
  • This would be a good moment to fire up your debugger and step through the code line for line. Also: You compare the first character to `<` not `i`. – n314159 Nov 28 '19 at 22:42

1 Answers1

0

Your problem is, that the failbit of cin is set after the while-loop. Call cin.clear() after it and it should work. Related: Why would we call cin.clear() and cin.ignore() after reading input?

n314159
  • 4,990
  • 1
  • 5
  • 20