Issue with else part of the statement

What the program is supposed to do is upload barcode, name and price of information from the txt file and ask for a barcode to be entered. Once the barcode is entered, it should list name and price and if the barcode isn't found, then give error "Item isn't found."

Program works fine until I get to the else statement. Once I add the else state, what the program seems to do is it goes through the list and for every item in the list, if it doesn't find the barcode, it displays Item not found. Lets say I have four items, and before I put in the else statement, and enter the barcode, it displays the item, and if the barcode isn't in there, it displays nothing. But once I add the else statement, it displays the "item not found of the other three items and the item).

Not sure exactly how to approach this, should I try something other than else? Any help in heading to the correct direction would be highly appreciated

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93


#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

struct Item
{
	string code;
	string name;
	double price;
};

const int MAX_ITEMS = 100;
Item items[MAX_ITEMS];
int loadData(Item items[], int &count);
string getName(Item items[]);
//double getPrice(Item items[]);
int count = 0;

int main()
{
	string reply;
	cout << "Welcome to Grocery store. " << endl;
	loadData(items, count);
	getName(items);
	//getPrice(items);
	cout << "Thank you for shopping Grocery." << endl;
	cout << "Press q (or any other key) followed by 'Enter' to quit: ";
	getline(cin, reply);
	return 0;
}

int loadData(Item items[], int &count)
{
	string inputFileName, input, reply;
	ifstream inputFile;
	
	//cout << "Please enter the name of the backup file: ";
	//getline(cin, inputFileName);
    inputFileName = "prices.txt"; //direct location of file	
	inputFile.open(inputFileName.c_str());
	
	if (!inputFile.is_open()) 
	{
        cout << "Unable to open input file." << endl;
        cout << "Press enter to continue...";
        getline(cin, reply);
        exit(1);
    }  
    while (getline(inputFile, input))
    {
    	//string str = input;
    	string code1, name1;
    	double price1;
    	items[count].code = (input.substr(0,9).c_str());
    	items[count].name = (input.substr(10,26).c_str());
    	items[count].price = atof(input.substr(35,6).c_str());
    	count++;
	}
	cout << count << " items laded successfully" << endl;
	return 0;
}
string getName(Item items[])
{
	string barcode;
	double item, total;
	total = 0;
	while(barcode != "q")
	{
		cout << "Please enter barcode (Press q to exit): ";
		getline(cin, barcode);
		for(int i = 0; i < count; i++)
		{
			if(items[i].code.find(barcode) == 0)
			{
				cout << setw(3) << right << items[i].name << setw(15) << left << items[i].price << endl;
				item++;
				total = items[i].price + total;
			}
//the below is where I am having issues
			//else
			{
				//cout << "Item not found. " << endl;
			}
		}
	}
	cout << "You bought "<< item << " items." << endl;
	cout << "The total cost is " << total << endl;
}
Last edited on
Here is a way to tackle your problem. Loop through all of your barcodes and set an index if it is found. Then, if the index is set, print the data, else, print not found.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int index = -1;
for(int i = 0; i < count; i++)
{
     if(items[i].code.find(barcode) == 0)
          index = i;
}

if(index != -1)
{
     cout << setw(3) << right << items[i].name << setw(15) << left << items[i].price << endl;
     item++;
     total = items[i].price + total;
}
else
     cout << "Item not found." << endl;


edit
I just noticed that you never initialize item so you'll need to add in item = 0. Also, you getName() function is set up to return a string, but you don't return anything. You should probably change it from string to void. There may be other things but those I see right off the bat.
/edit
Last edited on
I appreciate the response.

I am trying to understand it, don't want to just copy and paste. so initially setting index t o -1, in the first part if barcode is found then set index to entry.

not sure what "-'1" is, but if index is not -1 then display below else display item is not found.

And thanks for about setting item to 0, totally forgot about that
I just chose -1 as a number that will never be in items: i.e. there will never be an items[-1] You could just as easily set it to -12345 or any arbitrary number that you know will never be used.

So, if you loop through all your barcodes and index is still equal to -1, you know that the item was not found. In other words, if you find the item in the array, index is set to i to tell you where in the array the item was found. If the item is not found, index is never changed and remains -1;

Also, I just noticed that in my code there was a typo in line 8 if(index != -`1) the ' shouldn't be there. I'm going to edit that now.

Does this help you understand? If not I'll try again.
I initially thought this had fixed the issue, but all it did was get rid of the items not found, not if i enter a barcode not in the list, it gives me no response and ask for another barcode. I need it to say item not found when barcode entered is not listed. will keep at it, thanks for the help
Did you put all of the new code inside the while loop? Can you post your current code?
I added it into the while loop but had to adjust it to work, below is what I have - just doesn't show item not found when wrong barcode is entered, getting tired of working with this code, been tring different ways to fix it but not luck so far

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <iomanip>
#include <fstream> 
#include <cstdlib> //this is so str can be used

using namespace std;

struct Item //struct part of the program
{
	string code;
	string name;
	double price;
};

const int MAX_ITEMS = 100;  //limit the list to 100 items, can be increased
Item items[MAX_ITEMS];
int loadData(Item items[], int &count); //load part of the program
string getName(Item items[]); //get name and price part of the program

int count = 0; //setting count to 0

int main()
{
	string reply;
	cout << "Welcome to Grocery store. " << endl;
	loadData(items, count); //load part of the program
	getName(items);  //get name and price part of the program
	cout << "Thank you for shopping at Grocery." << endl;
	cout << "Press q (or any other key) followed by 'Enter' to quit: ";
	getline(cin, reply);
	return 0;
}

int loadData(Item items[], int &count)
{
	string inputFileName, input, reply;
	ifstream inputFile;
	
	//entering the name of the file with the list and prices 
	//cout << "Please enter the name of the backup file: ";
	//getline(cin, inputFileName);
    inputFileName = "prices.txt"; //direct location of file	
	inputFile.open(inputFileName.c_str());
	
	if (!inputFile.is_open()) //if file isn't able to be opened correctly
	{
        cout << "Unable to open input file." << endl;
        cout << "Press enter to continue...";
        getline(cin, reply);
        exit(1);
    }  
    while (getline(inputFile, input))  //upload the barcode, name and price
    {
    	items[count].code = (input.substr(0,9).c_str()); //input of the barcode
    	items[count].name = (input.substr(10,26).c_str()); //input for the name
    	items[count].price = atof(input.substr(35,6).c_str()); //input for the price
    	count++;
	}
	cout << count << " items laded successfully" << endl;
	return 0;
}
string getName(Item items[]) //search part of the program
{
	string barcode;
	double item, total;
	item = 0;  //setting item count to zero
	total = 0; //setting total to zero
	while(barcode != "q") //keep on running until q is entered
	{
		cout << "Please enter barcode (Press q to exit): ";
		getline(cin, barcode);
		int index = -1;  //set index to -1
		for(int i = 0; i < count; i++)
		{
     		if(items[i].code.find(barcode) == 0)
     		{
     			index = i; //if barcode is found then change index to i
     			if(index == i) //if index is i then run
				{	
     				cout << setw(3) << right << items[i].name << setw(15) << left << items[i].price << endl;
     				item++;
     				total = items[i].price + total;
     			}
				else
				{
					cout << "Item not found." << endl;
				}
			}
        }
	}
	cout << "You bought "<< item << " items." << endl;
	cout << "The total cost is " << total << endl;
}
Please post a small sample of your input file.

Please explain items[count].code = (input.substr(0,9).c_str());?

Look at the following snippet:
if(items[i].code.find(barcode) == 0)
Please tell me when std::string.find() will return a zero.

Why all those global variables?


Input file has the below data:

10001 Apples (bunch) 4.59
10002 Bananas (bunch) 4.99
10003 Pears (bunch) 5.49
20001 White bread (loaf) 2.69
20002 Brown bread (loaf) 2.89
20003 English Muffins (bag) 3.99
30001 Sugar (5 lb bag) 3.99
30002 Tea (box) 4.29
30003 Folger's Coffee (Can) 13.29

item[count].code is supposed to store the barcode part of the file

the if part, what I am trying to get is that if the barcode that is entered isn't found then that returns a 0 found??
item[count].code is supposed to store the barcode part of the file

But why are you converting the string to a C-string? And why the first 9 characters? How many characters is this barcode? To get the first entry it would probably be easier to use the extraction operator. For the second entry it would probably be easier to use getline() with the optional third parameter, using the '(' character, then followed by the extraction operator to retrieve the price.

the if part, what I am trying to get is that if the barcode that is entered isn't found then that returns a 0 found??

No the std::string.find() function does not return zero when it doesn't find the given string. It returns std::string::npos which is the biggest number a size_t can hold, not 0.

You did not put the code I gave you into your code correctly.

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
while(barcode != "q")
{
     cout << "Please enter barcode (Press q to exit): ";
     getline(cin, barcode);
     int index = -1;  //set index to -1
     for(int i = 0; i < count; i++)
     {
          if(items[i].code.find(barcode) == 0)
               index = i;
     }

     if(index != -1)
     {
          cout << setw(3) << right << items[i].name << setw(15) << left << items[i].price << endl;
          item++;
          total = items[i].price + total;
     }
     else
     {
          cout << "Item not found." << endl;
     }
}

cout << "You bought "<< item << " items." << endl;
cout << "The total cost is " << total << endl;


This should do what you want. However, I agree with jlb that there are some other problems with your code. But, you said it worked except when you put in the else, this should fix that.

edit - added a bracket that I missed
Last edited on
Topic archived. No new replies allowed.