Get data using ifstream

Oct 5, 2019 at 9:53pm
Hello.
I'm trying to get the information from a .txt file and using it on this program.
My problem is, that when it compiles, I get numbers like -85546484968 and so on. I would appreciate any help or what I am doing wrong.
The numbers in the file are like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

and this is my code

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

using namespace std;
struct company
{

	
	int n, n1, n2, n3;
	int s, s1, s2, s3;
	int e, e1, e2, e3;
	int w, w1, w2, w3;
};


void getData(company&);
void dispDivInfo(company);


int main()
{

	company branch1;
	getData(branch1);
	dispDivInfo(branch1);

	/*getData(branch2);
	dispDivInfo(branch2);*/

	/*getData(branch3);
	dispDivInfo(branch3);


	getData(branch4);
	dispDivInfo(branch4);*/

	return 0;
}





void getData(company &amount)
{
	ifstream myfile("quarterlysalesfigures.txt");
	int n, n1, n2, n3; int s, s1, s2, s3; int e, e1, e2, e3; int w, w1, w2, w3;
	myfile.open("quarterlysalesfigures.txt");
	myfile >> n >> n1 >> n2 >> n3 >> s >> s1 >> s2 >> s3
	>> e >> e1 >> e2 >> e3 >> w >> w1 >> w2 >> w3;
	
	myfile.close();
}


void dispDivInfo(company amount)
{

	cout << "For the North is " << amount.n << amount.n1 << amount.n2 << amount.n3 << endl;
	cout << "For the South is " << amount.s << amount.s1 << amount.s2 << amount.s3 << endl;
	cout << "For the East is " << amount.e << amount.e1 << amount.e2 << amount.e3 << endl;
	cout << "For the West is " << amount.w << amount.w1 <<amount.w2 << amount.w3 << endl;
}
Last edited on Oct 5, 2019 at 10:36pm
Oct 5, 2019 at 9:58pm
In getData() you read lots of local variables ... but they don't find their way into company account.
Oct 5, 2019 at 10:00pm
lastchance; I thought that using myfile >> variablename assigned the value into these variables
Oct 5, 2019 at 10:08pm
I thought that using myfile >> variablename assigned the value into these variables

That's right: these variables, but sadly not those in company account, which would be the ones sent back to the calling program if they had ever been set.

You have read the data into a lot of local variables, which end up pushing up the daisies when the function returns.


Try
myfile >> account.n >> account.n1 >> account.n2 >> etc.
Last edited on Oct 5, 2019 at 10:11pm
Oct 5, 2019 at 10:22pm
I tried with

myfile >> account.n >> account.n1 >> account.n2 >> etc.

but I got the same result.
Oct 5, 2019 at 10:26pm
Have you checked that your file exists and has been opened correctly? At the moment you are opening it twice.

When you do come to display them, make sure you put some spaces between the numbers.
Last edited on Oct 5, 2019 at 10:28pm
Oct 5, 2019 at 10:35pm
It says in one of the warnings that, in line 63 (where the int are) that those variables are unreferenced.

I erased one of the "something.txt" and put the spaces but I got the same result.
Oct 5, 2019 at 10:42pm
If you have a lot of unused variables then the compiler will warn you. If they aren't needed, remove them.

Test the state of your stream myfile. If it's not good then your file didn't open - probably because it's not in the expected folder, or is named slightly differently.

You can also try writing out one or two of the variables that you (think you) have read in getData().
Oct 5, 2019 at 10:47pm
OH !

I erased the ints from the line 63, and I added the '&' before amount in getData and it worked!

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
{/*
Complete Corporate Sales Data (#3) in chapter 11 on page 659 of the text book.

The main program should read in the sales data from the attached input file.
Modify the program as follows by creating the following functions -

1. getData - Takes no arguments, reads in the data from the file into the divisions,
returns a populated company division.

2.  dispDivInfo - Takes a division as an argument, Be sure the division cannot
be modified in dispDivInfo Displays the divisions info to the screen.*/

#include <iostream> 
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <conio.h>

using namespace std;
struct company
{

	string calculus;
	int n, n1, n2, n3;
	int s, s1, s2, s3;
	int e, e1, e2, e3;
	int w, w1, w2, w3;
};



void getData(company &);
void dispDivInfo(company);


int main()
{
	

	company branch1;

	
	getData(branch1);
	dispDivInfo(branch1);

	/*getData(branch2);
	dispDivInfo(branch2);*/

	/*getData(branch3);
	dispDivInfo(branch3);


	getData(branch4);
	dispDivInfo(branch4);*/

	return 0;
}





void getData(company &amount)
{
	ifstream myfile;
	
	
	myfile.open("quarterlysalesfigures.txt");
	
	myfile >> amount.n >> amount.n1 >> amount.n2 >> amount.n3 >>
		amount.s >> amount.s1 >> amount.s2 >> amount.s3 >>
		amount.e >> amount.e1 >> amount.e2 >> amount.e3 >>
		amount.w >> amount.w1 >> amount.w2 >> amount.w3;
	


	
	myfile.close();
}


void dispDivInfo(company amount)
{

	cout << "For the North is " << amount.n << amount.n1 << amount.n2 << amount.n3 << endl;
	cout << "For the South is " << amount.s << amount.s1 << amount.s2 << amount.s3 << endl;
	cout << "For the East is " << amount.e << amount.e1 << amount.e2 << amount.e3 << endl;
	cout << "For the West is " << amount.w << amount.w1 <<amount.w2 << amount.w3 << endl;
}
}
Topic archived. No new replies allowed.