reading in integers from a file

for the life of me i cannot figure out why this wont just read in and output correctly. when i cout it, it wont get the third number and i end up with:
2800000
0
24
input is 2800000.00 24 8.40
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
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
	
	ifstream inData;
	ofstream outData;
	string infileName;
	string outfileName;
	long intBalance;
	float annInterest;
	float numOfpayments;
	
	inData.open("zon.txt");
	
	/*cout << "Enter the input data file name:" << endl;
	
	cin >> infileName;
	
	inData.open(infileName.c_str());
	
	// make it a loop error message so user doesnt have to restart program. Annoying 
	if(!inData)
	{
		cout << "Can't open the input file. Program is stopping" << endl;
		return 1;
		
	}
	*/
	
	/*cout << "Enter the name of the file where you want the results stored: " << endl;
	
	cin >> outfileName;
	
	outData.open(outfileName.c_str());
	*/
	
	inData >> intBalance >>annInterest >>numOfpayments;
	
	

	
	
	
	
	
	
	
	
	
	return 0;
	
}
Last edited on
@sky3

It's probably because you declared intBalance to be an integer. So, when it reads the file, it stops at the point. annInterest reads the 00, and then numof payments reads the 24. Try changing intBalance to a float and annInterest to the integer.
when i declared it as a float, it wouldnt read the entirety of 280000?
@sky3

Try this way..

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

using namespace std;

int main()
{
	
	ifstream inData;
	ofstream outData;
	string infileName;
	string outfileName;
	float intBalance;
	int annInterest;
	float numOfpayments;
	
	inData.open("zon.txt");
	
	/*cout << "Enter the input data file name:" << endl;
	
	cin >> infileName;
	
	inData.open(infileName.c_str());
	
	// make it a loop error message so user doesnt have to restart program. Annoying 
	if(!inData)
	{
		cout << "Can't open the input file. Program is stopping" << endl;
		return 1;
		
	}
	*/
	
	/*cout << "Enter the name of the file where you want the results stored: " << endl;
	
	cin >> outfileName;
	
	outData.open(outfileName.c_str());
	*/
	
	inData >> intBalance >>annInterest >>numOfpayments;
cout << fixed << setprecision(2) << intBalance << endl;
cout << annInterest << endl;
cout << numOfpayments << endl;

return 0;
}
yea i just realized that lol.

it was using the scientific notion for 2.8 million wow.
input was doing its job correctly the whole time
thanks
Topic archived. No new replies allowed.