Passing an input file by reference into a function that returns a string

So i am supposed to pass an input file by reference into a function that returns a string type. The point is to read the name of the person in the file and returns the name to main.



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 <fstream>
using namespace std;

string contestantName();
void getJudgeData();
void calcScore();
double findLowest(int,int,int,int,int);
double findHighest(int,int,int,int,int);

int main(int argc, const char * argv[])
{
    ifstream inFile;
    inFile.open("starsearch.docx");
    
    ifstream outFile;
    outFile.open("results.docx");
    
    int number;
    inFile >> number;
    
    for (int count = 1; count <= number; count ++)
    {
        string name;
        contestantName(name);
        getJudgeData(score1);
        getJudgeData(score2);
        getJudgeData(score3);
        getJudgeData(score4);
        getJudgeData(score5);
        
        calcScore();
     
    }
   
    inFile.close();
    outFile.close();
    return 0;
}

string contestantName(& inFile)
{
    inFile >> name;
    return name;

}




So basically, this code reads the the first item in the file, which determines how many contestants there are. Then the part i am having trouble with is that Xcode is giving me hell because it wants me to put a semicolon after the contestantName function. The other function calls are for parts of the program i didn't include. what am i doing wrong?
Line 5: You have forgot to list the parameter type.

Line 25: You are passing a string but I think you want to pass inFile? The string should probably be assigned the return value of the function instead.

line 41: You have forgot to mention what type of reference.

line 43: There is no variable name in scope. You probably want to create it as a local variable on the previous line.

Last edited on
how do i list an input file as the parameter type in the prototype.
and all we have learned so far is that the ampersand passes by reference. nothing else after that.
Line 5:
 
string contestantName(ifstream&);


Line 41:
 
string contestantName(ifstream& inFile)
That 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
92
93
94
95
96


#include <iostream>
#include <fstream>
using namespace std;

string contestantName(ifstream&);
void getJudgeData(int&);
void calcScore(ofstream&, string, int, int, int, int, int);
double findLowest(int,int,int,int,int);
double findHighest(int,int,int,int,int);

int main(int argc, const char * argv[])
{
    ifstream inFile;
    inFile.open("starsearch.docx");
    
    ofstream outFile;
    outFile.open("results.docx");
    
    int number;
    inFile >> number;
    
    for (int count = 1; count <= number; count ++)
    {
        int score1, score2, score3, score4, score5;
        contestantName(inFile);
        getJudgeData(score1);
        getJudgeData(score2);
        getJudgeData(score3);
        getJudgeData(score4);
        getJudgeData(score5);
        
        calcScore(outFile);
     
    }
   
    inFile.close();
    outFile.close();
    return 0;
}

string contestantName(ifstream& inFile)
{
    string name;
    inFile >> name;
    return name;

}

void getJudgeData(int &score)
{
    inFile >> score;


}
void calcScore(ofstream& outFile,string name, int score1,int score2,int score3,int score4,int score5)
{
    double lowScore, highScore;
    findLowest();
    findHighest();
    double average = (score1 + score2 + score3+ score4 + score5 - lowScore - highScore)/3;
    outFile << name << \t << average << endl;

}

double findLowest(int score1,int score2,int score3,int score4,int score5)
{
    int lowScore = score1;
    if (score2 < lowScore)
        lowScore = score2;
    if (score3 < lowScore)
        lowScore = score3;
    if (score4 < lowScore)
        lowScore = score4;
    if (score5 < lowScore)
        lowScore = score5;
    return lowScore;
}

double findHighest(int score1,int score2,int score3,int score4,int score5)
{
    int highScore = score1;
    if (score2 > highScore)
        highScore = score2;
    if (score3 > highScore)
        highScore = score3;
    if (score4 > highScore)
        highScore = score4;
    if (score5 > highScore)
        highScore = score5;
    return highScore;


}


this is what i have so far. Xcode is saying that in line 34, the function calcScore does not exist. and also for line 60 and 61
You have declared calcScore to take 7 arguments but on line 34 you only pass 1 argument.
Topic archived. No new replies allowed.