Variables in while loops

So I'm doing an assignment where I need to take a partID and quantity from a text file and put it into a table in the command console (dealing with tax and grand totals later) but right now I'm stuck here, what I don't understand is where I should put the if else if statements regarding the partID to determine price for that part number. When I place the if else if statements out of the while loop, it would seem to me that the equation for the price would be stored, but it doesn't show up in the loop. When I try to put the if else if statements in the while loop, it just shows up as A for all of the partIDs. How can I fix this so that the price can be determined and I can loop the statement to output the table with partID, amount ordered, and price?

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

const double TAX_RATE = .17;
const double SHIPPING = 14.95;

int main() 
{
    // Get everything from text file //
	cout << setprecision(2) << showpoint << fixed;
	ifstream in;
	in.open("order.txt");
	char part;
	double quantity;
	double price;

	
    // Headers of Code //
    cout << "Thank You! Your order is summarized below:" << endl;
	cout << setw(52) << setfill('-') << "-" << endl;
	cout << setfill(' ');
	cout << "| " << setw(15) << left << "Product" << "| " << setw(15) << "Quantity" << "| " << setw(15) << "Line Total" << "|" << endl; 
	cout << setw(52) << setfill('-') << "-" << endl;
	cout << setfill(' ');
	
	if(part = 'A')
	    {
	        price = quantity * 17.46;
	    }
	    else if(part = 'B')
	    {
	        price = quantity * 10.13;
	    }
	    else if(part = 'C')
	    {
	        price = quantity * 2.11;
	    }
	    else if(part = 'D')
	    {
	        price = quantity * 23.13;
	    }
	    else if(part = 'E')
	    {
	        price = quantity * 74.56;
	    }
	    else if(part = 'F')
	    {
	        price = quantity * 1.11;
	    }
	    else if(part = 'G')
	    {
	        price = quantity * 9.34;
	    }
	    else if(part = 'H')
	    {
	        price = quantity * 3.45;
	    }
	  
	
	while (!in.eof())
	{
	    in >> part >> quantity;
	    
	    cout << "| " << setw(15) << left << part << "| " << setw(15) << quantity << "| " << setw(15) << price << "|" << endl;
	    cout << setw(52) << setfill('-') << "-" << endl;
	    cout << setfill(' ');
	}

	
	
	

	return 0;
}
Hello domweng,

First you should start by post the input file, or at least a fair sample. This way everyone will be using the same information.

I noticed: while (!in.eof()), this does not work the way you are thinking. It would work much better as: while (in >> part >> quantity).

Starting at line 30 where does "part" get it value? Since you did not initialize the variable when defined it just contains a garbage value.

After you open the file how do you know it is open and usable?

If you have studied "switch/case" it may be easier to work with than the if/else if statements.

All your magic numbers are fine, but consider making them constant variables. This way there is only 1 place to make changes and less chance of missing something especially in larger programs.

Andy
Hello domweng,

Testing your program I found: if(part = 'A'). This statement will always be true.

The (=) means to set and the (==) means to compare.

Also watch your indenting lines 31 - 61.

Andy
Hey guys thanks I got it figured out, just needed to use a switch.

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
/* Program name: Orders.cpp
*  Author: Dominic Wenger
*  Date last updated: 3/4/2021
* Purpose: Use a loop to read from an order input to calculate the order total.  
*/

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

const double TAX_RATE = .17;
const double SHIPPING = 14.95;

int main() 
{
    // Get everything from text file //
	cout << setprecision(2) << showpoint << fixed;
	ifstream in;
	in.open("order.txt");
	char part;
	int quantity;
	double price;
	double subtotal = 0;
	double tax;
	double grand;

    // Headers of Code //
    cout << "Thank You! Your order is summarized below:" << endl;
	cout << setw(52) << setfill('-') << "-" << endl;
	cout << setfill(' ');
	cout << "| " << setw(15) << left << "Product" << "| " << setw(15) << "Quantity" << "| " << setw(15) << "Line Total" << "|" << endl; 
	cout << setw(52) << setfill('-') << "-" << endl;
	cout << setfill(' ');
	
	// Loop Statement //
	while (!in.eof())
	{
        in >> part >> quantity;
    // Check Which Product //   
        switch(part)
	{
	    case 'A' :
	    price = quantity * 17.46;
	    break;
	    case 'B' :
	    price = quantity * 10.13;
	    break;
	    case 'C' :
	    price = quantity * 2.11;
	    break;
	    case 'D' :
	    price = quantity * 23.13;
	    break;
	    case 'E' :
	    price = quantity * 74.56;
	    break;
	    case 'F' :
	    price = quantity * 1.11;
	    break;
	    case 'G' :
	    price = quantity * 9.34;
	    break;
	    case 'H' :
	    price = quantity * 3.45;
	    break;
	}
	// Make Table //
	    cout << "| " << setw(15) << left << part << "| " << setw(15) << quantity << "| " << setw(15) << price << "|" << endl;
	    cout << setw(52) << setfill('-') << "-" << endl;
	    cout << setfill(' ');
	    subtotal += price;
	    
	}
	// Finish Table With Remaining Data //
	    tax = subtotal * TAX_RATE;
	    grand = subtotal + tax + SHIPPING;
	    cout << "| " << setw(15) << left << "Subtotal" << "| " << setw(15) << " " << "| " << setw(15) << subtotal << "|" << endl;
	    cout << setw(52) << setfill('-') << "-" << endl;
	    cout << setfill(' ');
	    cout << "| " << setw(15) << left << "Tax" << "| " << setw(15) << " " << "| " << setw(15) << tax << "|" << endl;
	    cout << setw(52) << setfill('-') << "-" << endl;
	    cout << setfill(' ');
	    cout << "| " << setw(15) << left << "Shipping" << "| " << setw(15) << " " << "| " << setw(15) << SHIPPING << "|" << endl;
	    cout << setw(52) << setfill('-') << "-" << endl;
	    cout << setfill(' ');
	    cout << "| " << setw(15) << left << "Grand Total" << "| " << setw(15) << " " << "| " << setw(15) << grand << "|" << endl;
	    cout << setw(52) << setfill('-') << "-" << endl;
	    cout << setfill(' ');


	
	
	

	return 0;
}
Hello domweng,

The if statements can be made to work, but I do like the "switch" better.

Just for an idea this is what I did:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    constexpr double APRICE{ 17.46 }, BPRICE{ 10.13 };

    while (in >> part >> quantity)
    {
        switch (part)
        {
            case 'A':
                price = quantity * APRICE;
                break;
            case 'B':
                price = quantity * BPRICE;
                break;
         }
     }


Andy
Use a look-up table. Also, your code to read the file isn't right. The stream extraction should be the while condition, not testing for eof. eof is set when a read is tried but doesn't succeed - not when a read has succeeded with no more data following. Consider (not tried):

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

using namespace std;

int main()
{
	const double TAX_RATE {.17};
	const double SHIPPING {14.95};
	const double Prices[] {17.46, 10.13, 2.11, 23.13, 74.56, 1.11, 9.34, 3.45};

	ifstream in("order.txt");

	if (!in)
		return (cout << "Cannot open input file\n"), 1;

	cout << setprecision(2) << showpoint << fixed;
	cout << "Thank You! Your order is summarized below:\n";
	cout << setw(52) << setfill('-') << "-\n";
	cout << setfill(' ');
	cout << "| " << setw(15) << left << "Product" << "| " << setw(15) << "Quantity" << "| " << setw(15) << "Line Total" << "|\n";
	cout << setw(52) << setfill('-') << "-\n";
	cout << setfill(' ');

	char part {};
	int quantity {};
	double subtotal {};

	while (in >> part >> quantity) {
		const auto price {(part >= 'A' && part <= 'H') ? quantity * Prices[part - 'A'] : 0.0};

		cout << "| " << setw(15) << left << part << "| " << setw(15) << quantity << "| " << setw(15) << price << "|\n";
		cout << setw(52) << setfill('-') << "-\n";
		cout << setfill(' ');
		subtotal += price;
	}

	const auto tax {subtotal * TAX_RATE};
	const auto grand {subtotal + tax + SHIPPING};

	cout << "| " << setw(15) << left << "Subtotal" << "| " << setw(15) << " " << "| " << setw(15) << subtotal << "|\n";
	cout << setw(52) << setfill('-') << "-\n";
	cout << setfill(' ');
	cout << "| " << setw(15) << left << "Tax" << "| " << setw(15) << " " << "| " << setw(15) << tax << "|\n";
	cout << setw(52) << setfill('-') << "-\n";
	cout << setfill(' ');
	cout << "| " << setw(15) << left << "Shipping" << "| " << setw(15) << " " << "| " << setw(15) << SHIPPING << "|\n";
	cout << setw(52) << setfill('-') << "-\n";
	cout << setfill(' ');
	cout << "| " << setw(15) << left << "Grand Total" << "| " << setw(15) << " " << "| " << setw(15) << grand << "|\n";
	cout << setw(52) << setfill('-') << "-\n";
}

Last edited on
@domweng:
* Refactoring code to functions can simplify
* What if part is 'X'?

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
double calcPrice( char part, int quatity )
{
  double price = 0; // default value
  if ( part == 'A' ) { // operator==, not operator=
    price = quantity * 17.46;
  }
  else if ( part == 'B' ) {
    price = quantity * 10.13;
  }
  return price;
}

void print( const string& title, double amount );
  cout << "| " << setw(15) << left << title << "| " << setw(15) << " " << "| " << setw(15) << amount << "|\n";
  cout << setw(52) << setfill('-') << "-" << '\n';
  cout << setfill(' ');
}

// used in
while (in >> part >> quantity) {
  const auto price = calcPrice( part, quantity );
  // ...
}

print( "Subtotal", subtotal );
print( "Tax", tax );
print( "Shipping", SHIPPING );
Topic archived. No new replies allowed.