okay I'm making a POS system for a grocery store and i'm kind of stuck.
for example, lets say I have a variable called cheese which is equal to 5. I have another variable called product. A user enters 'cheese' (without the quotes) when prompted so that product now equals cheese. I want to multiply product by 2, which should equal 10. I then want another variable called total to equal product*2. Does that make sense?
here's my header file where i declare most variables, arrays, etc.
1 2 3 4 5 6 7
string items[50];
string price[50];
string index = 0;
string input;
double chosen;
double total = 0.0;
char transIndex[80];
void transaction()
{
system("cls");
int quantity = 0;
cout << "Please enter the name of the product: ";
cin >> input;
cout << endl;
cout << "How many? ";
cin >> quantity;
checkproduct();
if(itemPass = true)
{
if(strcmp(input, items[index]) == 0)
{
chosen = items[index];
}
total = total+chosen*quantity;
cout << "Total price: $" << total << endl;
}
cout << endl << endl;
posttransaction();
}
void checkproduct()
{
cout << "Checking to make sure item exists...";
for(int index=0; index<50; index++)
{
if(input == items[index])
{
cout << "Item passed." << endl;
itemPass = true;
//transIndex = items[index];
}
}
if(itemPass == false)
{
cout << "Please enter a valid product. ";
system("pause");
transaction();
}
}
it doesn't work for me. I get two errors: no matching function for call to `strcmp(std::string&, std::string&)'
and no match for'operator[]' in 'items[index]'
I don't think you're starting with the right concepts. I would have thought that you needed a price list that holds the price of each item. Each customer has a basket with items in it. When the customer goes to the checkout, you'd look up the price of each item from the price list and sum the values to get the final price.
Back to your syntax errors: if(strcmp(input, items[index]) == 0) is incorrect. if (input == items[index]) is the correct way to compare strings.
it's only storing numbers so an int would work better. that isn't the problem though. I replaced index to 0 on line 17 and 19 and now i only get and error on line 19 which says: cannot convert `std::string' to `double' in assignment
but i changed the array price to one of doubles and then i got a problem in my cacher :S i should be able to fix it though.
the problem was here, on line 4, where I take the price list from a text file and add it to the price array
cannot convert `std::string' to `double' in assignment
Means pretty much what it says. You're trying to take a string and make it fit into a double. You need to parse the string and transform it into the correct type.
A good way of doing this would be to use boost::lexical_cast()
You'd need to have the boost libraries installed, which is beyond the scope of this topic. An alternative, slightly outdated method but still functional would be to use atof() like:
price[i] = atof(linePrice.c_str())
You need the .c_str() to convert from std::string (C++) to what the atof() function expects.
Boost isn't a standard set of libraries (EDIT: although some will become standard in the next standard, I take it). While it is undoubtedly a library set everyone should have, that doesn't mean someone will know it or will allow you to use it. :(
For reference, though: http://cplusplus.com/articles/numb_to_text/#boost
it worked! thanks Pax and kbw, you helped me so much; which is why it kills me asking for help again. when I go through the caching process, it reaches here
1 2 3 4 5 6
if(!productlist.is_open()) // if file failed to open, send user back to start
{
cout << "Product list failed to initialize, teleporting you to the menu." << endl;
system("pause");
menu();
}
(line 13)
and displays that message. I went into debugging mode and it told be there was an access violation. I googled it any nothing came up. could you lend, just one more hand, please?
I have a theory that it's an issue with opening the file. There's no problem with the target file (items.txt) as i could open it and print out it's contents on the screen with a new .cpp file in another folder. The code for opening items.txt in the original file seems perfect.... this makes no sense
EDIT: I just figured it out. It's so stupid. I just switched to using wxDev-C++ from Dev-C++ today, and I didn't realise that I had to put the items.txt inside the "Default Profile" folder.
Well that's annoying.
Thankyou all so much for your help though. And sorry for wasting you time at the end.