To compare your strings, ignoring any case difference, you should first of all change the strings to all upper case or all lower case after reading them in, such as:
transform(names[count]begin(), names[count].end(),names[count].begin(), toupper);
Use tolower as the last parameter if you want lower case.
Do this to the data you read in to the array, and to the item2 string. Then your equality test will work reliably.
Ensure that you add #included to your code.
Alternatively, if your compiler supports it, you could use the c-style string comparison function that ignores case:
if( stricmp( names[count].c_str( ), item2.c_str( ) ) == 0 ) //they match
The c_str() method of the string class give a c-style equivalent of the string (actually a pointer to char array) so that the older compare function can be used.
As you read in an item to be purchased, you will have to compare it to every item name in your array, until a match is found or you exceed count locations. Then check the parallel index location in the inventory array for a non-zero count.
If you will be doing many purchase transactions, it might be worthwhile to sort the array of strings before begining purchases. Simple linear search can then stop when you've gone past the point of finding a match, or better yet, use Binary Search.
Val
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
>transform(names[count]begin(), names[count].end(),names[count].begin(), toupper);
Good idea, unsound implementation. The problem is that toupper is overloaded and your compiler may create an ambiguity behind the scenes. On top of that, even if your compiler does the right thing and picks the C toupper, the behavior is still undefined because transform expects a function or function object with C++ linkage, and the C toupper is explicitly declared with C linkage. This is better:
#include <cctype>
struct upper {
int operator() ( int c ) {
return std::toupper ( static_cast<unsigned char> ( c ) );
}
};
...
std::transform ( src.begin(), src.end(), src.begin(), upper() );
>Alternatively, if your compiler supports it, you could use the c-style string comparison function that ignores case:
A better way would be to hide it behind a macro (or function) so that you don't lose portability:
#define compare_insensitive stricmp
...
if( compare_insensitive( names[count].c_str( ), item2.c_str( ) ) == 0 ) //they match
Or write your own comparison function, because it's not difficult at all.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I'd set up the test and decision code like:
bool found = false; //add this to your variable declarations
found = false;
for (i =0; i < count && !found; ++i)
{
if (item2 == names[i] ) //is there a matching item
{
found = true; //lets us stop short when found item
if inventory[i] != 0){ //if so, are any in stock?
cout <<"Sold!" << endl;
inventory[i] = inventory[i] - 1; }
else
cout << "Sorry out of stock!" << endl;
}
}
if( !found ) //ran all the way through
cout << "no such item in list" << endl;
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228