Hello faidi,
I will try to explain what keskiverto is saying.
You need to understand the difference between "cin >>" and "getline(...)".
Note: with "cin >>" or "getline(...)" both will store what you type in an input buffer until you press Enter and then process it into the variable associated with the input.
"cin >>" is known as formatted input which means the variable to the right of ">>" determines what is expected. If "answer" was defined as an "int" it expects a number and anything else will cause "cin" to fail. With "answer" defined as a "char" you can type a letter or a number and it will accept it.
The other part of "cin >>" is that it will extract from the input buffer up to the first white space or new line (\n) whichever comes first leaving whatever is left in the input buffer for the next "cin >>" or "getline" to use. So, the next call to "cin >>" does not wait for keyboard input it just extracts whatever is left in the input buffer. The same is true for "getline". In the end "cin >>" will always leave the "\n" in the input buffer. Not generally a problem for "cin >>", but a problem when followed by a "getline".
"getline", on the other hand, will extract everything up to and including the "\n", but will discard the "\n" leaving the input buffer empty. This is useful when you have input that contains a space or two that you want to keep.
As keskiverto mentioned the use of the "ignore" function will clear the input buffer. In my time here I have seen this used most often:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
This will produce the largest number available for your computer and compiler based on what is in the header file limits. I tend to put this line after the last "cin >>" or before a "getline" so that the "getline" will work properly. Although I would say it is better to put the "ignore" after the last "cin >>".
This should help:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
while (answer != 'N')
{
cout << "\n\nEnter service code :";
cin.getline(serviceCode, 5); //When enter loop 2nd time and above, compiler ignored this
cin.getline(serviceCode, 5); //Added another line here
//The rest of the code ( selection ) -process
cout << "\n+++++++++++++++++++++++++++SPEEDTYRE SERVICE RECEIPT+++++++++++++++++++++";
cout << "\nCustomer Name:" << customerName;
cout << "\nVehiclePlate no.:" << vehiclePlate;
cout << "S\nervice Description:" << serviceDesc;
TOTprice = TOTprice + earlyPrice;
cout << "\nTotal :RM" << TOTprice;
cout << "\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
cout << "\n Do you want to make another service?(Y/N):";
cin >> answer;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
|
As I look over your code I wonder if you are required to use character arrays? A "std::string" would be better. The "std::string" would require the header file "string" not "cstring". Since your program is in C++ I would suggest using the "string" header file and "std::string". Which would change
cin.getline(vehiclePlate,10);
to
std::getline(std::cin, vehiclePlate);
. I think you will find using a "std::string" much easier.
Note: Proper indenting helps.
Hope that helps,
Andy