How to pass a numbers from a file to a function?

Hi ive been searching google for quite some while now and i cant figure out how to use the function getFile to return the whole file back into the main function.

it just returns the last integer when i run it. please help! assignment is due tonight.


heres the original question for a heads up.

Using functional decomposition, design and write a C++ program that inputs a series of 24 hourly temperatures
from a file, and you will create your own input data file with necessary data for your test cases. You will output
a bar chart (using stars) of the temperatures for the day. The temperature should be printed to the left of the
corresponding bar, and there should be a heading that gives the scale of the chart. The range of temperatures
should be from –30 to 120. Because it is hard to display 150 characters on the screen, you should have each star
represent a range of 3 degrees. That way, the bars will be at most 50 characters wide. Here is a partial
example, showing the heading, the output for a negative temperature, and the output for various positive
temperatures. Note how the temperatures are rounded to the appropriate of stars



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

using namespace std;

void getFile(int &num);

int main()
{
    int num;
    getFile(num);
    cout << num;
    
    
system ("PAUSE");   
return 0;
}

void getFile(int &num)
{
     ifstream inFile;
    
    inFile.open("C:\\Users\\Peter\\Documents\\CSP31A\\Assignment3question2.txt");
    
    if(!inFile){
                cout << "\nOpen file song.dat not successful";
                }
                
                inFile >> num;
                
                while (inFile >> num){
                      cout << num;
                      cout << endl;
                }
}
Maybe you should use an array to hold the 24 values. Pass the array to the function as a parameter. There's an example of an array as a parameter in the tutorial, see printarray
http://www.cplusplus.com/doc/tutorial/arrays/
im so stuck :\
Did you try the array? Where are you stuck?
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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void getFile(int num);

int main()
{
    int num;
    getFile(num);
    
    
    
system ("PAUSE");   
return 0;
}

void getFile(int num)
{
     ifstream inFile;
    
    inFile.open("C:\\Users\\Peter\\Documents\\CSP31A\\Assignment3question2.txt");
    
    if(!inFile){
                cout << "\nOpen file song.dat not successful";
                }
                int i=0;
                inFile >> num[i];
                
                while (inFile >> num[i]){
                      i++;
                      cout << num[i];
                      cout << endl;
                }
}


i get this error. 30 C:\Users\Peter\Documents\CSP31A\Assignment03question2.cpp invalid types `int[int]' for array subscript
This may help. There may still be errors, but it is closer:
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

const int SIZE = 24;

void getFile(int num[]);
void showValue(int num[]);

int main()
{
     int num[SIZE] = {0};
    
    getFile(num);
    
    showValue(num);

    system ("PAUSE");   
    return 0;
}

void getFile(int num[])
{
    ifstream inFile;
    
    inFile.open("C:\\Users\\Peter\\Documents\\CSP31A\\Assignment3question2.txt");

    if (!inFile) {
       cout << "\nOpen file song.dat not successful";
    }
        
    int i=0;
    
    while (inFile >> num[i])
    {
          //cout << num[i];
          //cout << endl;
          i++;
          
          if (i > SIZE)  // be careful not to exceed size of array.
              break;
    }
}

void showValue(int num[])
{
    for (int i=0; i<SIZE; i++)
    {
        cout << num[i] <<endl;
    }
}
Last edited on
i forgot to tell you that the program needs to read more than 24 till end of file.
Well, you need to make sure the array is big enough. If you know roughly how many values are in the file, them make the array somewhat larger than that. Otherwise, you may be better off using a vector instead - but that would add another piece to the learning curve.

Also, if you make the array bigger than the actual amount of data, you would need to count how many items were read from the file (which you do already) and pass that count back to main() so it knows how much data there is.
Last edited on
well theres 24 hours but if there is more than that the program needs to check it
gonna work on this tomorrow and turn it in late lol..
Topic archived. No new replies allowed.