Input file

Okay so i've recently wrote a program calculating a utility bill. inputting the account number, name, type and usage..for example

{
validAccountNumber = false;
while (validAccountNumber == false)
{
cout << "Enter your 4-digit account number: ";
cin >> accountNumber;
if (accountNumber >= 1000 && accountNumber <= 9999) // sets the valid value of the account number
validAccountNumber = true;

else
{
cout << "Invalid account number. (Account numbers must be between 1000-9999)" << endl; //declares invalid values
exit(1);
}

i now want to change the input type to a data file.. and do the following

1. Ask the user to enter the data file name. Open the data file in read mode. If file opening is unsuccessful display appropriate message and exit the program.

2. Read how many records in the file. If there are no records in the file display an appropriate message and exit the program.

3. Read the next consumer information

4. Calculate utility bill for the consumer and display bill on the screen. After the current bill is displayed, display two blank lines and display the following message: Press [n/N] for next or [q/Q] to quit.

i have an understanding on how to make a simple input / output file program but converting a program i have already written is slightly confusing. any help would be apprecaited.
I think you should write your code here so that someone can help you.Otherwise [personally] I dont think that some will give you the complete code that suits your needs.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>
using namespace std;

int main ()
{

int accountNumber;
bool validAccountNumber;
char accountName [26];character
char serviceCategory;
bool validServiceCategory;
double Kwh;
bool validKwh;
double unit;
bool validUnit;
double electricCharge;
double waterCharge;
double total;
double surcharge;
double sewerCharge;


{
validAccountNumber = false;
while (validAccountNumber == false)
{
cout << "Enter your 4-digit account number: ";
cin >> accountNumber;
if (accountNumber >= 1000 && accountNumber <= 9999)
validAccountNumber = true;

else
{
cout << "Invalid account number. (Account numbers must be between 1000-9999)" << endl; //declares invalid values
exit(1);
}
}
cin.ignore (80,'n');
cout << "Enter your account name: ";
cin.getline(accountName, 25);

validKwh = false;
while (validKwh == false)
{
cout << "Enter your electricity consumption in Kwh's: ";
cin >> Kwh;
if (Kwh >= 0)
validKwh = true;
else if (Kwh = 100)
validKwh = true;
}

validUnit = false;
while (validUnit == false)
{
cout << "Enter your water consumption in units: ";
cin >> unit;
if (unit >= 0)
validUnit = true;
else if (unit = 25)
validUnit = true;
}

cin.ignore (80, 'n');
validServiceCategory = false;
while (validServiceCategory == false)
{
cout << "Enter service category (R)esidential, (C)ommercial, or (I)ndustrial: ";
cin >> serviceCategory;
serviceCategory = toupper(serviceCategory);
if (serviceCategory == 'R' || serviceCategory == 'C' || serviceCategory == 'I') // || determines OR in the declaring of serviceCategory's
validServiceCategory = true;
else if (serviceCategory = 'R') //If an invalid value is entered, the category will be set to 'R' (residential)
validServiceCategory = true;
}
if (serviceCategory == 'R') // Calculating the electrical charges
if (Kwh <= 500)
electricCharge = Kwh * 0.050;
else
electricCharge = Kwh * 0.025;

if (serviceCategory == 'C')
if (Kwh <= 600)
electricCharge = Kwh * 0.080;
else
electricCharge = Kwh * 0.050;

if (serviceCategory == 'I')
if (Kwh <= 2500)
electricCharge = Kwh * 0.060;
else
electricCharge = Kwh * 0.015;

if (serviceCategory == 'R')
if (unit <= 30)
waterCharge = unit * 0.20;
else
waterCharge = unit * 0.10;

if (serviceCategory == 'C')
if (unit <= 30)
waterCharge = unit * 0.20;
else
waterCharge = unit * 0.15;

if (serviceCategory == 'I')
if (unit <= 5000)
waterCharge = unit * 0.10;
else
waterCharge = unit * 0.02;

cout << "******************************************************************************** " << endl << endl;
cout << " Utility District " << endl << endl << endl;
cout << "******************************************************************************** " << endl;
cout << "Account number: " << fixed << left << accountNumber << endl;
cout << "Account name: " << left << accountName << endl << endl;
if (serviceCategory == 'R')
{
cout << "Service category: Residential" <<endl << endl;
}
else if(serviceCategory == 'C')
{
cout << "Service category: Commercial" << endl << endl;
}
else if (serviceCategory == 'I')
{
cout << "Service catgegory: Industrial" << endl << endl;
}
cout << "Electric consumption: " << setw (10) << fixed << setprecision (2) << right << Kwh;
cout << " Electric Charge: $ " << setw (10) << right << fixed << setprecision(2) << electricCharge << endl;

if (serviceCategory == 'R')
sewerCharge = (waterCharge / 2);
if (serviceCategory == 'I')
sewerCharge = (waterCharge * .70);
if (serviceCategory == 'C')
sewerCharge = (waterCharge * .70);

if (serviceCategory == 'R')
if (Kwh > 1000)
surcharge = 5.00;
else
surcharge = 0.00;

if (serviceCategory == 'C')
if (Kwh > 1500)
surcharge = 10.00;
else
surcharge = 0.00;

if (serviceCategory == 'I')
if (Kwh > 10000)
surcharge = 25.00;
else
surcharge = 0.00;
cout << " Surcharge: $ " << setw (10) << right << surcharge;
cout << endl << endl <<"Water consumption: " << setw (10) << right << fixed << setprecision (2) << unit;
cout << " Water Charge: $ " << setw (10) << right << fixed << setprecision (2) << waterCharge;
cout << endl << endl;
cout << " Sewer Charge: $ " << setw (10) << setprecision (2) << right << sewerCharge;
cout << endl << endl;
total = electricCharge + waterCharge + surcharge + sewerCharge;
cout << " Total charge: $ ";
cout << setw (10) << right << setprecision (2) << fixed << total << endl << endl;
cout << "******************************************************************************** " << endl << endl;
cout << endl << "******Done processing utility bill******" << endl << endl;
} // ending while

return 0;
}
[ code ] [ /code ] tags around your code please. It's a huge pain reading it when it's not syntax highlighted.
#include <iostream>
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <iomanip> 
#include <cmath> 
#include <cctype>
using namespace std;

int main ()
{

int accountNumber;
bool validAccountNumber;
char accountName [26];character
char serviceCategory;
bool validServiceCategory;
double Kwh;
bool validKwh;
double unit;
bool validUnit;
double electricCharge;
double waterCharge;
double total;
double surcharge;
double sewerCharge;


{
validAccountNumber = false;
while (validAccountNumber == false)
{ 
cout << "Enter your 4-digit account number: ";
cin >> accountNumber;
if (accountNumber >= 1000 && accountNumber <= 9999) 
validAccountNumber = true;

else
{
cout << "Invalid account number. (Account numbers must be between 1000-9999)" << endl; //declares invalid values
exit(1);
}
}
cin.ignore (80,'n');
cout << "Enter your account name: ";
cin.getline(accountName, 25);

validKwh = false;
while (validKwh == false)
{
cout << "Enter your electricity consumption in Kwh's: ";
cin >> Kwh;
if (Kwh >= 0)
validKwh = true;
else if (Kwh = 100) 
validKwh = true;
}

validUnit = false;
while (validUnit == false)
{
cout << "Enter your water consumption in units: ";
cin >> unit;
if (unit >= 0)
validUnit = true;
else if (unit = 25)
validUnit = true;
}

cin.ignore (80, 'n');
validServiceCategory = false;
while (validServiceCategory == false)
{
cout << "Enter service category (R)esidential, (C)ommercial, or (I)ndustrial: ";
cin >> serviceCategory;
serviceCategory = toupper(serviceCategory);
if (serviceCategory == 'R' || serviceCategory == 'C' || serviceCategory == 'I') // || determines OR in the declaring of serviceCategory's
validServiceCategory = true;
else if (serviceCategory = 'R') //If an invalid value is entered, the category will be set to 'R' (residential)
validServiceCategory = true;
}
if (serviceCategory == 'R') // Calculating the electrical charges
if (Kwh <= 500)
electricCharge = Kwh * 0.050;
else 
electricCharge = Kwh * 0.025;

if (serviceCategory == 'C')
if (Kwh <= 600)
electricCharge = Kwh * 0.080;
else
electricCharge = Kwh * 0.050;

if (serviceCategory == 'I')
if (Kwh <= 2500)
electricCharge = Kwh * 0.060;
else
electricCharge = Kwh * 0.015;

if (serviceCategory == 'R') 
if (unit <= 30)
waterCharge = unit * 0.20;
else 
waterCharge = unit * 0.10;

if (serviceCategory == 'C')
if (unit <= 30)
waterCharge = unit * 0.20;
else 
waterCharge = unit * 0.15;

if (serviceCategory == 'I')
if (unit <= 5000)
waterCharge = unit * 0.10;
else 
waterCharge = unit * 0.02;

cout << "******************************************************************************** " << endl << endl;
cout << " Utility District " << endl << endl << endl;
cout << "******************************************************************************** " << endl;
cout << "Account number: " << fixed << left << accountNumber << endl;
cout << "Account name: " << left << accountName << endl << endl;
if (serviceCategory == 'R')
{
cout << "Service category: Residential" <<endl << endl;
}
else if(serviceCategory == 'C')
{
cout << "Service category: Commercial" << endl << endl;
}
else if (serviceCategory == 'I')
{
cout << "Service catgegory: Industrial" << endl << endl;
}
cout << "Electric consumption: " << setw (10) << fixed << setprecision (2) << right << Kwh; 
cout << " Electric Charge: $ " << setw (10) << right << fixed << setprecision(2) << electricCharge << endl;

if (serviceCategory == 'R') 
sewerCharge = (waterCharge / 2);
if (serviceCategory == 'I')
sewerCharge = (waterCharge * .70); 
if (serviceCategory == 'C')
sewerCharge = (waterCharge * .70);

if (serviceCategory == 'R') 
if (Kwh > 1000)
surcharge = 5.00;
else 
surcharge = 0.00;

if (serviceCategory == 'C')
if (Kwh > 1500)
surcharge = 10.00;
else 
surcharge = 0.00;

if (serviceCategory == 'I')
if (Kwh > 10000)
surcharge = 25.00;
else 
surcharge = 0.00;
cout << " Surcharge: $ " << setw (10) << right << surcharge; 
cout << endl << endl <<"Water consumption: " << setw (10) << right << fixed << setprecision (2) << unit;
cout << " Water Charge: $ " << setw (10) << right << fixed << setprecision (2) << waterCharge;
cout << endl << endl;
cout << " Sewer Charge: $ " << setw (10) << setprecision (2) << right << sewerCharge;
cout << endl << endl;
total = electricCharge + waterCharge + surcharge + sewerCharge;
cout << " Total charge: $ ";
cout << setw (10) << right << setprecision (2) << fixed << total << endl << endl;
cout << "******************************************************************************** " << endl << endl;
cout << endl << "******Done processing utility bill******" << endl << endl;
} // ending while

return 0;
}


sorry
Topic archived. No new replies allowed.