loop issue

so this is a checkout program. Hers my problem: the user enters 1 to enter checkout and then enters the PLU code followed by the weight of it or unit, then they have to choice to enter another PLU code or 0 to exit. Once the user enters 0 it displays the total cost and is suppose to then asks the user again to press 1 to checkout or 0 to exit,instead, it asks to enter the PLU code or 0 to exit. any thoughts on how i can 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <string>
#include <fstream>
using namespace std;



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


   fstream filename("theList.txt");
   storePro array[6];
   int o;

   //if (!filename.fail())
   {
      for (int o = 0; o < 6; o++)
      {

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

      }
      for (int o = 0; o < 5; o++)
      {
         cout << array[o].PLUcode << " " << array[o].itemName << " "
            << array[o].itemType << " " << array[o].itemPrice << " "
            << array[o].itemLevel << endl;
      }
   }


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


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

   do
   {
      
      if (userChoice == 1)
      {
         int count = 0;
         int PLU = 0;
         int userIn;
         float units;
         float sum = 0;
        

         do
         {
            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;
                  found = true;
                  break;
               }


            }
           
            if (!found)
            {
               cout << "PCU code was not found." << endl;
               cout << "Please enter PLU number or 0 to leave.";
               cin >> userIn;
               //break;
            }
            //PLU = 0;
            if (array[PLU].itemType == 1)
            {
               cout << "Pounds: ";
               count++;

            }
            else
            {
               cout << "Units: ";
               count++;
            }

            cin >> units;
            sum = sum + units + array[PLU].itemPrice;
          
         } while (userIn != 0);
         cout << "$"  << sum << endl;
         
      }
      else
      {
         return 0;
      }

   } while (userChoice != 0);

   return 0;
}
Last edited on
You have one loop on 49 and another loop on 61. On line 66, you ask the user to input 0 in order to exit. You exit the "61 loop" on line 72 and end up back in the "49 loop". You need to have some other piece of code that will allow you to exit from the "49 loop" based on user input.

This would be the basic outline of how the program would look if I did it:

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
int main()
{
    //variable declarations
    bool codeFound;
    bool checkout = true;

    //input from file

    //prompt user
    cin >> userChoice;

    if (userChoice == 0)
        return 0;

    while (checkout)
    {
        codeFound = false;

        while (!codeFound && checkout) //my professor keeps telling me using break is bad practice
        {
            //prompt user for code or 0 to exit
            cin >> code;
            
            if (code == 0)
                checkout = false;
            else
                //search array for code and update codeFound
            
            if (!codeFound && checkout)
                //tell user code wasn't found, try again
        }

        if (checkout)
        {
            //prompt user for units
            cin >> units;

            //calculate sum, you used '+' instead of '*' in your code
        }
    }

    //display sum

    return 0;
}
so theres nothing i can fix from my code?
You can just add

1
2
if (userIn == 0)
    break;

between lines 111 and 112 of your code.
after the user enters 0 int the "enter PLU code or 0 to exit" its suppose to ask to enter 1 to checkout or 0 to exit again
what? That doesn't even make sense. You want to tell the user to enter '0' to exit and then tell them again to enter 0 to exit? What do you really want to do?
Topic archived. No new replies allowed.