Uninitialized Variables ( Phone bill program )

I'm trying to call a function into another function but It keeps giving me that the two variables I have in in the "read_and_total" function I'm tring to call into the "read_and_process" aren't initialized( int D and int P)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void read_and_process (int acct, int& timeUsed, int& dataUsed, bool& valid)

int error;
valid = validate_account(acct);

if (!valid)
{
cout << "The account Number provided is invalid" << endl;
}

cin >> timeUsed >> dataUsed >> error;
if (error == 1)
{
valid = false;
cout << "The account record has an error" << endl;
}
}




This is the function I want to call in

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void read_and_ total(double regularTimeCost, double regulaDatacost, int acctNum, int dataUsed, int timeUsed)

{
int D, P;


D = D + 1;
P = P + 1;

for ( D = 0; D <= 10000; D++)
{
dataUsed = dataUsed +D;
cout << acctNum << D << dataUsed << regularDataCost
<< endl;

}
{
for (P = 0; P <= 10000; P++)
{
timeUsed = timeUsed + P;
cout << acctNum << P << dataUsed << regularTimeCost
}
Last edited on
closed account (48T7M4Gy)
You need to set the starting values of D and P. Perhaps each can be set at zero? The reason is int D for arguments sake allocates storage but the contents of that storage will be junk unless you initialize it. Starting with junk and then adding 1 to it as in line 7 of the second listing just makes more junk.
@kemort thanks, it worked; and now I was wondering if the for loops I used would be valid in adding to the dataUsed in the first function
closed account (48T7M4Gy)
It's not clear to me which function you are calling so I can only guess.

But if you have a line in void read_and_process ( ... ) then you would write read_and_ total(regularTimeCost, regulaDatacost, acctNum, dataUsed, timeUsed)

If you are in void read_and_ total( ... ) and want to call void read_and_process ( ... ) repeatedly in a loop then you would write:
1
2
3
4
5
for( int i = start; i < something; i change)
{
     read_and_process (acct, timeUsed, dataUsed, valid);
     ... whatever other stuff
}


Don't forget also that you have to supply values to the various parameters being passed.
Last edited on
Topic archived. No new replies allowed.