Scope problems with my functions

I'm trying to create a program where I get data from a file and calculate certain things using functions. I want a function to get the data, calculate some things, and then write to file (or just cout it until I figure everything out).

I basically calculate Meal Cost within my "calcData" function. I would like to then send that to my "sendData" function, where I would output the data to a file (or just to the console in the mean time). The error that I'm getting is that "mealCost" is not declared within the sendData scope.

I also am getting a little confused about when to use references for variables in function parameters. I get the basic premise that if you have two functions in a program, you don't want to just make a copy of a variable, and that you want it to change (i.e. something like int addNum(int & x){return x + 5;}). It's just a little confusing when I start to deal with a lot of variables.

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

using namespace std;

void getData(int &, int &, char &, char &, float &);
void calcData(int, int, char, char, float);
void sendData(int, int, char, char, float, float);

ifstream inFile;

const float taxRate = 0.18;
const float weekendSurcharge = .07;

int main()
{

int numAdults;
int numChildren;
char mealType;
char dayType;
float depositAmount;

cout << "\nThis program will calculate data for a catering company " << endl << endl;

cout << "Adults " << "Children " << "Meal" << " Weekend? " << setw(5) << "Dep. Amount " << endl;

inFile.open("file.txt");

if (!inFile){
cout << "\nError: File could not be opened. ";
}

while(!inFile.eof()){

getData(numAdults, numChildren, mealType, dayType, depositAmount);
calcData(numAdults, numChildren, mealType, dayType, depositAmount);
sendData(numAdults, numChildren, mealType, dayType, depositAmount, mealCost);

}
    return 0;
}

void getData(int & numAdults, int & numChildren, char & mealType, char & dayType, float & depositAmount){

inFile >> numAdults >> numChildren >> mealType >> dayType >> depositAmount;

}

void calcData(int numAdults, int numChildren, char mealType, char dayType, float depositAmount){

float mealCost;
float totalTax = 0;
float totalSurcharge = 0;
float discountAmount = 0;

if (mealType == 'S'){

mealCost = ((numAdults * 21.75) + (numChildren * (21.75*.60)));
totalTax = mealCost * taxRate;

mealCost +=taxRate;

if (dayType == 'Y')
{
totalSurcharge = mealCost * weekendSurcharge;

mealCost +=totalSurcharge;
}}

else{

mealCost = ((numAdults * 25.80) + (numChildren * (25.80*.60)));
totalTax = mealCost * taxRate;

mealCost +=taxRate;

if (dayType == 'Y')
{
totalSurcharge = mealCost * weekendSurcharge;

mealCost +=totalSurcharge;
}}

if (mealCost < 100){

discountAmount = .015 * mealCost;
mealCost -= discountAmount;

}

else if (mealCost >= 100 && mealCost < 400){

discountAmount = .025 * mealCost;
mealCost -= discountAmount;

}

else if (mealCost >= 400){

discountAmount = .035 * mealCost;
mealCost -= discountAmount;

}

}

void sendData(int numAdults, int numChildren, char mealType, char dayType, float depositAmount, float & mealCost){

cout << fixed << showpoint << setprecision(2);

cout << setw(5) << numAdults << setw(5) <<  numChildren << setw(8) << mealType << setw(6) << dayType << setw(5) << setw(13) << right << depositAmount << mealCost << endl;


}

/*
*/
Topic archived. No new replies allowed.