How can i Extract Data from a txt file

How can i extract data from a text file and implement the answers to the code and give the results with the loop ending when it runs out of data extracted from the text file.

for some reason when i run it, it infinitely loops and takes no data out of the text file.

any help i can get i really appreciate it!



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
  //
// Program:    billfromfile.cpp		Date:  October 2nd, 2013
// Programmer: Anton Siedlecki
// Assignment:  2     Question: 1
//
// Purpose of program:
// 
// 
//
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int accountNumber;
char serviceCode;
double regularMinutes;
double dayTimeMinutes, eveningTimeMinutes;
double dayTime, eveningTime;
double overallCharge;
double const REGULAR_BASE_RATE=10.00;
double const PREMIUM_BASE_RATE=25.00;

int main()
{
		cout.setf(ios::fixed);
		cout.setf(ios::showpoint);
		cout.precision(2);
	
		fstream inputStream;

 	inputStream.open("billingdata.txt");

	
	cout << "Enter your account number: ";
	inputStream >> accountNumber;
	cout << "\n";
	cout <<"Enter your service code: (R/r-regular, P/p-premium): ";
	inputStream >> serviceCode;
	
	while (!inputStream.eof())
	{switch (serviceCode)
	{
	case 'r':
		cout << endl << "How many minutes were used this month ? $";
		inputStream >> regularMinutes;
		overallCharge= (regularMinutes<=50) ? REGULAR_BASE_RATE : 
					((regularMinutes-50.00)*0.20)+REGULAR_BASE_RATE;
		cout << endl <<"The monthly charge for account "
			 << accountNumber << "is $" << overallCharge;
		break;
	case 'R':
		cout << endl << "How many minutes were used this month ? ";
		inputStream >>regularMinutes;
		overallCharge= (regularMinutes<=50) ? REGULAR_BASE_RATE : 
					((regularMinutes-50.00)*0.20)+REGULAR_BASE_RATE;
		cout << endl <<"The monthly charge for account "
			 << accountNumber << " is $" << overallCharge <<endl;
		break;
	case 'P':
		cout << endl << "How many daytime minutes were used this month ? ";
		inputStream >>dayTimeMinutes; 
		cout << endl << "How many evening minutes were used this month ? ";
		inputStream >>eveningTimeMinutes;

		dayTime = (dayTimeMinutes<=75) ? PREMIUM_BASE_RATE/2.0 :
			((dayTimeMinutes-75)*0.10)+(PREMIUM_BASE_RATE/2);
		eveningTime = (eveningTimeMinutes<=100) ? PREMIUM_BASE_RATE/2.0 : 
			((eveningTimeMinutes-100)*0.05)+(PREMIUM_BASE_RATE/2.0);
		overallCharge = dayTime + eveningTime;
		
		cout << endl <<"The monthly charge for account "
			 << accountNumber << " is $" << overallCharge <<endl;
		break;
	case 'p':
			cout << endl << "How many daytime minutes were used this month ? ";
		inputStream >>dayTimeMinutes; 
		cout << endl << "How many evening minutes were used this month ? ";
		inputStream >>eveningTimeMinutes;

		dayTime = (dayTimeMinutes<=75) ? PREMIUM_BASE_RATE/2.0 :
			((dayTimeMinutes-75)*0.10)+(PREMIUM_BASE_RATE/2);
		eveningTime = (eveningTimeMinutes<=100) ? PREMIUM_BASE_RATE/2.0 : 
			((eveningTimeMinutes-100)*0.05)+(PREMIUM_BASE_RATE/2.0);
		overallCharge = dayTime + eveningTime;
		
		cout << endl <<"The monthly charge for account "
			 << accountNumber << " is $" << overallCharge <<endl;
		break;
	default:
		cout << "** Invalid Service Code – Monthly Bill Not Calculated **"<<endl;
		cout << endl <<"The monthly charge for account "
			 << accountNumber << " is $" << overallCharge <<endl;
	}
	}
	inputStream.close(); 

	return 0;

}
Didn't actually read code thorough, but there is some advice:
1) Check if file is actually open after opening it. (use is_open() memder function)
2) Post billingdata.txt content. Maybe there is something wring with it.
3) Either change service code to upper/lowercase before switch or use fallthrough to get rid of excess code:
1
2
3
4
case 'P':
case 'p':
//code
break;
Topic archived. No new replies allowed.