Hi guys,
I am bringing a problem which I thought I had solved, but it has re-reared its ugly head. The problem is one of user input (I can hear you veterans audibly say "Aargh!"). I wish to remove serial numbers (of type string) from a set. To give you a little context, I am working on a Point-of-Sale system. This function allows the user to specify the serial numbers of itemized products that have been sold and are thus no longer part the inventory. I got what seemed like a perfect solution to this problem on this forum, but for some reason in this, I admit quite different, context it just won't work. Let me post the original solution:
ItemizedProductLine::ItemizedProductLine(ProductCode pc, double rrp, double sp, int oh, char* desc): ProductLine(pc, rrp, sp, oh, desc){
string serial;
// obtain user input to obtain serial numbers. Pressing enter (blank) quits
cout << "Please enter a serial number. Enter to quit: ";
getline(cin, serial);
while(!serial.empty())
{
serialNumbers.insert(serial);
cout << "Please enter a serial number. Enter to quit: ";
getline(cin, serial);
}
}
Ok, so this works swimmingly! It makes sense to me. I should correctly attribute this code to NathanOliver (http://www.daniweb.com/forumsthread286345.html?utm_source=vBMail&utm_medium=email&utm_campaign=ThreadbitDigest). Thanks, Nathan. However, when I try to call the following function, it acts as if I have pressed enter (when blank) and returns to the calling function. I think I should add that this function is a virtual one. I am not sure if that is material. Here is the code for the offending article:
void ItemizedProductLine::Sold(int qty){
// if the requested quantity is greater than available - no sale.
if(qty > onHand)
{
cout << "Only " << onHand << " available - Sale cancelled" << endl;
}
//otherwise all good. Reduce onHand by one and increase the number sold today by //one.
else
{
cout << "Total Price: " << qty * salePrice << endl;
onHand -= qty;
soldToday += qty;
}
string serial;
cout << "Enter serial number to be removed. Enter to quit:";
getline(cin, serial);
while(!serial.empty())
{
// remove the number from the set
serialNumbers.erase(serial);
cout << "Please enter serial number to be removed. Enter to quit: ";
getline(cin, serial);
}
}
Let me explain my reasoning, and we'll see if that is the first port-of-call as regards the problem!
1 - I arrive at the output point of the function. Message is printed to screen.
2 - I request a line of input, as per the first function. Crash! Bang!
3 - If we are getting to the while condition, the string is empty I guess. From here on, I don't really know what is going on.
Point 2, I think is where things are going wrong. My other thought was that I have to clear the input or some such, as I do use getline earlier in the program.
Guys, any help would be greatly appreciated - as ever! Thanks in advance.
Daniel