Infinite Loop

Feb 10, 2022 at 1:58am
I have an infinite loop and I'm trying to figure out where's the error

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//Include the following

#include <iostream>
#include <string>
#include <fstream>  //you must include this library if you wish to do file i/o
using namespace std;


//Prototypes for your functions: input, output, and process will go here

void input(ifstream&, string&, string&, double&, double&, int&);
void output(string, string, double, double, int, double, double, double, double);
void process(double, double, int, double&, double&, double&, double&);


//Function Implementations will go here

///*************************************************************************************
//Name:  input
//Precondition: State what is true before the function is called.
// Example: the varialbes (formal parameters) have not been initialized
//Postcondition: State what is true after the function has executed.
// Example: the varaibles (formal parameters) have been initialized
//Description:  
// Example:Get input (values of cell_number, item_number, quantity, price, processing_plant ) 
//from data file.

//PURPOSE: SHOW ME THAT YOU KNOW HOW TO READ INPUT AND USE INPUT (CALL BY VALUE) & OUTPUT (CALL BY VALUE) PARAMETERS

//*************************************************************************************

void input(ifstream& in, string& cell_number, string& item_number, double& quantity, double& price, int& processing_plant) // example using the call by reference mechanism in C++ -- call record is passed by reference --note & operator
{
	in >> cell_number;
	in >> item_number;
	in >> quantity;
	in >> price;
	in >> processing_plant;
		
		//add more code to read the rest of the variables (item_number, quantity, price and processing_plant)

}

///*************************************************************************************
//Name:  output
//Precondition: State what is true before the function is called.
//Postcondition: State what is true after the function has executed.
//Description:  Describe what the function does (purpose).
//*************************************************************************************


void output(string cell_number, string item_number, double quantity, double price, int processing_plant, double tax_rate, double order_tax, double net_cost, double total_cost)
{

	//Use thee following statement to help you format you our output. These statements are called the magic formula.
	cout.setf(ios::showpoint);
	cout.precision(2);
	cout.setf(ios::fixed);
	/********************************************/


	cout << cell_number << "\t";
	cout << item_number << "\t";
	cout << quantity << "\t";
	cout << price << "\t";
	cout << processing_plant << "\t";
	cout << int (tax_rate) << "\t";
	cout << order_tax << "\t";
	cout << net_cost << "\t";
	cout << total_cost << endl;
	//add more code to print all the variables



}

///*************************************************************************************
//Name:  process
//Precondition: The state what is true before the function is called.
//Postcondition: State what is true after the function has executed.
//Description:  Describe what the function does (purpose).
//*************************************************************************************


//Note: there is one 1 input/output parameter
void process(double quantity, double price, int processing_plant, double& tax_rate, double& order_tax, double& net_cost, double& total_cost)
{

//put your code here to process/calculate the tax_rate, order_tax, net_cost, and total_cost
if (processing_plant >= 0 && processing_plant <= 50)
{
	tax_rate = 6;
}
else if (processing_plant >= 51 && processing_plant <= 110)
{
	tax_rate = 7;
}
else if (processing_plant >= 111 && processing_plant <= 200)
{
	tax_rate = 8;
}
else if (processing_plant >= 201 && processing_plant <= 500)
{
	tax_rate = 9;
}
else
{
	tax_rate = 11;
}



order_tax = quantity * price * (tax_rate/100);
net_cost = quantity * price;
total_cost = net_cost + order_tax;
}


//Here is your driver to test the program
int main()
{
	string cell_number;
	string item_number;
	double quantity;
	double price;
	int processing_plant;
	double tax_rate;
	double order_tax;
	double net_cost;
	double total_cost;

	ifstream in;    //declaring an input file stream
	in.open("purchase_data.txt");

	if (in.fail())
	{
		cout << "Input file did not open correctly" << endl;
	}
	else
	{
		while (!in.eof())
		{

			input(in, cell_number, item_number, quantity, price, processing_plant);
			process(quantity, price, processing_plant, tax_rate, order_tax, net_cost, total_cost);
			output(cell_number, item_number, quantity, price, processing_plant, tax_rate, order_tax, net_cost, total_cost);
			//process(add actual arguments...);
			//output(add actual arguments);
		}
	}

	in.close();

	return 0;
}


Last edited on Feb 10, 2022 at 1:58am
Feb 10, 2022 at 3:34am
The only thing that could cause an infinite loop in your code is bad data in purchase_data.txt. Does it perchance contain a column heading line that you need to skip?

Your main is not correctly structured anyway. Instead of:

1
2
3
4
5
6
while (!in.eof())
{
    input(in,...);
    process(...);
    output(...);
}

It should be more like this:

1
2
3
4
5
while (input(in,...))
{
    process(...);
    output(...);
}

And your input function needs to return the eof state, which could just be returning bool(in):

1
2
3
4
5
bool input(istream& in, int& a, int &b, int& c)
{
    in >> a >> b >> c;
    return bool(in);
}


If you don't want to change your input function to return something, you could do it like this:
1
2
3
4
5
6
7
while (true)
{
    input(in,...);
    if (!in) break;
    process(...);
    output(...);
}

And instead of testing for in.eof() or in.fail() it's often best to test for !in, which includes both cases.

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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct Item
{
    string cell_number;
    string item_number;
    double quantity;
    double price;
    int processing_plant;
    double tax_rate;
    double order_tax;
    double net_cost;
    double total_cost;
};

bool input(istream&, Item& item);
void process(Item& item);
void output(const Item& item);

bool input(istream& in, Item& item)
{
    in >> item.cell_number
       >> item.item_number
       >> item.quantity
       >> item.price
       >> item.processing_plant;
    return bool(in);
}

void output(const Item& item)
{
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout.setf(ios::fixed);

    cout << item.cell_number << "\t";
    cout << item.item_number << "\t";
    cout << item.quantity << "\t";
    cout << item.price << "\t";
    cout << item.processing_plant << "\t";
    cout << int(item.tax_rate) << "\t";
    cout << item.order_tax << "\t";
    cout << item.net_cost << "\t";
    cout << item.total_cost << endl;
}

void process(Item& item)
{
    if (item.processing_plant <= 50)
        item.tax_rate = 6;
    else if (item.processing_plant <= 110)
        item.tax_rate = 7;
    else if (item.processing_plant <= 200)
        item.tax_rate = 8;
    else if (item.processing_plant <= 500)
        item.tax_rate = 9;
    else
        item.tax_rate = 11;
    item.order_tax = item.quantity * item.price * (item.tax_rate / 100);
    item.net_cost = item.quantity * item.price;
    item.total_cost = item.net_cost + item.order_tax;
}

int main()
{
    ifstream in("purchase_data.txt");
    if (!in)
    {
        cerr << "Input file did not open correctly\n";
        return 1;
    }

    // Consume the header line:
    string line;
    getline(in, line);

    Item item;
    while (input(in, item))
    {
        process(item);
        output(item);
    }
    
    // in.close() is called automatically at the end of the block/function.

    return 0;
}

Last edited on Feb 10, 2022 at 3:35am
Topic archived. No new replies allowed.