I'm writing a program for my C++ class, this is a short clip of it:

    for(customer = 1; customer <= 5; customer++)
        customer_Num[customer][2] = rand() % 999 + 1; // Receipt number
        cout << "Customer Name:" << endl;
        cout << "Customer's address:" << endl;
        cout << "Customer's age:" << endl;
        cin >> customer_Num[customer][1];

        //TEST PHASE
        cout << customer_Num[customer][2] << endl
             << customer_Non[customer][1] << endl
             << customer_Non[customer][2] << endl
             << customer_Num[customer][1] << endl
             << quantity << endl;

Now if I don't put the cin.ignore, when it gets to customer #2, it'll skip Customer Name: and move on to Customer's address. I think its safe to assume that the reason I need cin.ignore is because of the \n when I press enter after customer's age, but then I would think that I would only need cin.ignore(); but without a number in there (which is default 1 I believe), I get the same result as before, skipping through the second Customer name.cin.ignore(2); seems to work just fine but I want to understand why. Can someone explain to me why I need that 2 in there?

In your system you could have a CR/LF for \n. This would mean you have two charecters to get rid of. An easy way to get arroung this is to take in everything as a string and then convert the string to get what you need. If you want to use ignore than I would suggest using cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'). You do need the <limits> header to use this. What this says is to eat everything in the stream up to the maximum size of the stream untill you hit a \n, then consume the \n.

Edited 4 Years Ago by NathanOliver

I see, that makes a lot of sense, thanks for the solution too even though I can't use it since its material we haven't covered.

This question has already been answered. Start a new discussion instead.