Variables in Functions

Hello to all, I am new to C++ and having difficulty getting my code to execute properly. I cannot understand how to properly write the code to initialize variables in my Process function. The information for the relays and call length should be coming from my external data file (located in the same directory as my source file). Any advice at all will be 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
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <string>
#include <fstream>

using namespace std;


class call_record
{
public:
	string cell_number;
	int relays;
	int call_length;
	double net_cost;
	double tax_rate;
	double call_tax;
	double total_cost;
};

void Input(ifstream &, call_record &);
void Output(const call_record &);
void Process(call_record &);


void Input(ifstream & in, call_record & customer_record)
{
	in >> customer_record.cell_number;
	in >> customer_record.relays;
	in >> customer_record.call_length;
}




void Output(const call_record & customer_record)
{
	cout.setf(ios::showpoint);
	cout.precision(2);
	cout.setf(ios::fixed);

	cout << customer_record.cell_number << "  ";
	cout << customer_record.relays << "  ";
	cout << customer_record.call_length << "  ";
	cout << customer_record.net_cost << "  ";
	cout << customer_record.tax_rate << "  ";
	cout << customer_record.call_tax << "  ";
	cout << customer_record.total_cost << "  ";
}



void Process(call_record & customer_record)
{	
	int relays, call_length;
	double net_cost, tax_rate, call_tax, total_cost;

	net_cost = relays / 50.0 * 0.40 * call_length;

	if (relays <= 5) {
		tax_rate = 0.01;
	}
	else if (relays <= 11 && relays >= 6) {
		tax_rate = 0.03;
	}
	else if (relays <= 20 && relays >= 12) {
		tax_rate = 0.05;
	}
	else if (relays <= 50 && relays >= 21) {
		tax_rate = 0.08;
	}
	else if (relays > 50) {
		tax_rate = 0.12;
	}

	call_tax = net_cost*tax_rate;

	total_cost = net_cost + call_tax;

	
}

int main()
{
	call_record customer_record;

	ifstream in;
	in.open("call_data.txt");

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

	in.close();
	return 0;
}
The information for the relays and call length should be coming from my external data file

No. Not for the void Process(call_record &);. That function receives a reference to a call_record object. The necessary info should be in that object and the results apparently should be stored into the same object.

On lines 27-29 you do write to a call_record object and on lines 41-47 you do read from such object. Your Process() should read and write too (but with = rather than << and >> ).
Last edited on
Hello DJSpeal,

I addition to keskiverto's comments The "call_record" class is wrong, kind of backwards. By defining all your variables as "public" it defeets the purpose of using a class. These variables should be "private" and create member functions or chnge the functions you have to be member functions. As you have it a "struct" would contain all these variables under one name.

In main the while loop checks for "eof". Not the best way to use a while loop. By the time the loop reaches the "eof" the last record will process twice. Have a look at this recent post.
http://www.cplusplus.com/forum/beginner/222607/#msg1020980

For the if statement I would add some type of a short pause followed by "exit(1);" because there is no reason to continue with the program is the file does not open. You could loose the else and {} for the block. You really do not need an if/else statement because if true you will exit the program otherwise it is OK to continue with the program.

Hope that helps,

Andy
Hints:
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
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <vector>

class call_record {
public:
    std::string cell_number;
    int relays {};
    int call_length {};
    double net_cost {};
    double tax_rate {};
    double call_tax {};
    double total_cost {};
    call_record& Process();
    friend std::istream& operator>>(std::istream& is, call_record& cr)
    {
        is >> cr.cell_number >> cr.relays >> cr.call_length;
        return is;
    }
    friend std::ostream& operator<<(std::ostream& os, const call_record& cr)
    {
        os.setf(std::ios::showpoint);
        os.precision(2);
        os.setf(std::ios::fixed);
        os << "Number: " << cr.cell_number << "; relays: " << cr.relays
           << "; duration: " << cr.call_length << "; cost: " << cr.net_cost
           << "; tax rate: " << cr.tax_rate << "; taxes: " << cr.call_tax
           << "; total: " << cr.total_cost;
        return os;
    }
};

call_record& call_record::Process()
{
    net_cost = relays / 50.0 * 0.40 * call_length;

    if (relays <= 5) { tax_rate = 0.01; }
    else if (relays <= 11 && relays >= 6)  { tax_rate = 0.03; }
    else if (relays <= 20 && relays >= 12) { tax_rate = 0.05; }
    else if (relays <= 50 && relays >= 21) { tax_rate = 0.08; }
    else if (relays > 50)                  { tax_rate = 0.12; }
    call_tax = net_cost * tax_rate;
    total_cost = net_cost + call_tax;
    return *this;
}

int main()
{
    std::vector<call_record> customer_record;
    std::ifstream in("call_data.txt");
    if (!in) {
        std::cout << "Input file did not open correctly.\nExiting now.\n";
        return 0;
    } else {
        for(std::string line; std::getline(in, line); /**/) {
            std::istringstream ss(line);
            call_record cr;
            ss >> cr;
            cr.Process();
            customer_record.push_back(cr);
        }
    }
    in.close();
    
    // Print out read data:
    for(const auto& a : customer_record) { std::cout << a << '\n'; }
    return 0;
}


call_data.txt:

123456789 09 13
912345678 13 17
891234567 55 20


Output:
Number: 123456789; relays: 9; duration: 13; cost: 0.94; tax rate: 0.03; taxes: 0.03; total: 0.96
Number: 912345678; relays: 13; duration: 17; cost: 1.77; tax rate: 0.05; taxes: 0.09; total: 1.86
Number: 891234567; relays: 55; duration: 20; cost: 8.80; tax rate: 0.12; taxes: 1.06; total: 9.86

Topic archived. No new replies allowed.