i really can't give a title to this. please assist!

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];


and here's my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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]'

please help!
Last edited on
you should look up stl::string methods c_str() and compare(), and decide if you want to go the strcmp() route or the pure C++ stl::string route.

also, take a close look at Line 15 - is an assignment operator what you really want there?
Last edited on
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.

Last edited on
oh sorry, I forgot to mention that I have like a caching function which populates the arrays 'items' and 'price'.

but i did try what you suggested and I get this error:
no match for 'operator[]' in 'items[index]'
IS index supposed to be a string or an int?
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
1
2
3
4
5
     for(int i=0; !pricelist.eof(); i++)
     {
             getline(pricelist,linePrice);
             price[i] = linePrice;
     }


I tried a couple things to solve it but it just seems to get worse. any tips?

edit: sorry, here's the code for my whole cacher:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
void cacher()
{
     system("cls"); // clear screen
     cache = true;
     string lineProduct;
     string linePrice;
     
     cout << "Caching product list.... ";
     
     ifstream productlist;
     productlist.open("items.txt"); // open file
     
     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();
     }
     
     for(int i=0; !productlist.eof(); i++) // for loop to add each item to the 'items' array (defined in globalVars.h)
     {
             getline(productlist,lineProduct);
             items[i] = lineProduct; // add entry to array
     }
     
     productlist.close(); //close file
     
     cout << "done." << endl;
     cout << "Caching price list.... ";
     
     ifstream pricelist;
     pricelist.open("pricelist.txt");
     
     if(!pricelist.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();
     }
     
     for(int i=0; !pricelist.eof(); i++)
     {
             getline(pricelist,linePrice);
             price[i] = linePrice;
     }
     
     pricelist.close();
     
     cout << "done." << endl;
     cout << "Cache process complete. ";
     system("pause");
     
     menu();
}
Last edited on
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()
thanks for the reply, but how would I use lexical_cast() in this? could you demonstrate please :)
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.
It's just moved onto the next error:
chosen = items[index];

where chosen is a double and items[index] is a string. You can convert the string to a double with:
chosen = atof(items[index].c_str());

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

An article on the topic may be found here:
http://cplusplus.com/articles/numb_to_text/

-Albatross
Last edited on
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?

thanks :)
Do you have more than 50 entries in the file? You may have overrun the arrays.

An alternative to using fixed length arrays is to use a standard vector, an array that grows.

To use it, replace the declaration from:
string line[50];
to
vector<string> line;

You add items to it with:
line.push_back(lineProduct);

You access it the same way as you would an array. The array size is line.size(), so you're loops should use that as its limit.
I'd create a structure, containing a std::string and an unsigned integer:
1
2
3
4
5
6
7
#include <string>
struct Product
{
    std::string Name;
    unsigned Price;
    Product(std::string n,unsigned p): Name(n), Price(p) {}
};

Store all these products in a vector and simply check the products' name (using strcmp()) and get their price when needed.
Last edited on
What you both suggested doesn't seem it work.

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.
Last edited on
Topic archived. No new replies allowed.