checkout program issue

so this is a checkout program. the problem i have is that the program doesnt work with the other PLU numbers only the 1st one. any thoughts on how to fix this?
4011 BANANAS 1 0.49 123.2
4383 MINNEOLAS 1 0.79 187.3
3144 TANGERINES 1 1.19 135.5
4028 STRAWBERRIES_PINT 0 0.99 104
4252 STRAWBERRIES_HALF_CASE 0 3.99 53
4249 STRAWBERRIES_FULL_CASE 0 7.49 67

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
#include <iostream>

#include <string>
#include <fstream>
using namespace std;



struct storePro
{
   int PLUcode;
   string itemName;
   int itemType;
   double itemPrice;
   double itemLevel;
};
int main()
{

 
   ifstream filename("checkoutList.txt");
   storePro array[6];
   int o;

  
   {
      for (int o = 0; o < 6; o++)
      {

         filename >> array[o].PLUcode >> array[o].itemName >> array[o].itemType >> array[o].itemPrice >> array[o].itemLevel;

      }
     
   }


   cout << " ";
   cout << " ";
   int userChoice;


   cout << "Enter 1 to checkout or 0 to end checkout.";
   cin >> userChoice;


   do
   {
      //cout << "Welcome to the Checkout! " << endl;
      if (userChoice == 1)
      {
         int count = 0;
         int PLU = 0;
         int userIn;
         float units;
         float sum = 0;
         //cout << "Please enter PLU number or 0 to leave.";
         //cin >> userIn;
         do
         {
            PLU = 0;

            cout << "Please enter PLU number or 0 to leave.";
            cin >> userIn;

            if (userIn == 0)

            {
               break;
            }
            for (int element = 0; element < 6; element++)
            {
               if (array[element].PLUcode == userIn)
               {
                  PLU = element;
                  break;
               }
Last edited on
What do you mean by "doesn't work"? Does it crash? Does it send a bomb with your name on it to the FBI? Does it email your grandmother a virus? You need to be specific.
lol , it says code not found if i put any number except for 4011
*edit: it doesnt continue to search for the others

1
2
3
4
5
6
7
8
9
if (array[element].PLUcode == userIn)
{
   PLU = element;
    break;
 }
else
{
   cout<< "code not found"<<endl;
 }
Last edited on
Correction: it repeatedly says that for each non-match until it does find a match. Solution: don't have that output inside the loop.
ah so it does work without it, but where would i place it?
If PLU is unchanged after the loop, you know it was not found. You will need to set PLU to an invalid value, such as -1.
what do you mean unchanged? the PLU number before or after the user input?
Last edited on
You assign to PLU in the loop only when you find a match. Thus, if you never assign to it, you never found a match.
something like this? it works but it creates another separate issue
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
PLU = -1;
            cout << "Please enter PLU number or 0 to leave.";
            cin >> userIn;

            if (userIn == 0)

            {
               break;
            }

            for (int o = 0; o < 6; o++)
            {
               if (array[o].PLUcode == userIn)
               {
                  PLU = o;
                  //invalid = o;
                  break;
               }


            }
            if (PLU == -1)
            {
               cout << "PCU code was not found." << endl;
               PLU = 0;
               break;
            }
Last edited on
Yes.

A alternative solution is to use a separate boolean variable:
1
2
3
4
5
6
bool found = false;
//...
if(!found)
{
    //...
}
Last edited on
so the if(!found) would replace if(PLU==-1)? keep everything else the same?
Yep.
i dont think im doing it right
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
 bool found = false;
            PLU = 0;
            //PLU = -1;
            cout << "Please enter PLU number or 0 to leave.";
            cin >> userIn;

            if (userIn == 0)

            {
               break;
            }

            for (int o = 0; o < 6; o++)
            {
               if (array[o].PLUcode == userIn)
               {
                  PLU = o;
                  //invalid = o;
                  break;
               }


            }
            //if (PLU == -1)
            if(!found)
            {
               cout << "PCU code was not found." << endl;
               
               break;
            }
On line 18 you need to set found = true; - I thought you got the idea.
oh ok ill try it, different question, almost everytime i make a change and build it i get an error that says cannot open .......... for writing LINK, do you know how to fix this?
You still have the program open - close all the program windows and rebuild.
ok thanks for all your help :)
Topic archived. No new replies allowed.