Function Parameters to already finished code...?

So I finished up an assignment for school, but after speaking with my teacher about my assignment, he told me how my variables need to be passed through the function parameters for a number of reasons which I understand. However, I don't know where exactly to start to get each function and function call have the parameters of the variables I've already put in the code... Any tips to get started from having no parameters to having all of my code work with them? Would be greatly appreciated!

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
#include <iostream>
#include <cstdlib>
using namespace std;

const int SIZE_OF_TEST = 10; 
const int SIZE_OF_CLASS = 3; 
char answerKey[SIZE_OF_TEST]; 
char studentAnswer[SIZE_OF_TEST]; 
string studentName[SIZE_OF_CLASS]; 
double answersCorrect = 0; 
double percentage = 0;

/* Function Prototypes  */
void inputData();
void inputStudentData();
void processData();
void outputData();


int main()
{
    inputData();                           
    inputStudentData();                     
    cout << "All done. Thanks!" << endl;
	return 0;
}


void inputData(){                                                        
    cout << "TRUE/FALSE ANSWER KEY \(T or F only please!\)" << endl;
    for (int i = 0; i < SIZE_OF_TEST; i++) {
    cout << "Question " << i+1 << ": ";
    do
    cin >> answerKey[i];
    while (answerKey[i] !='T' && answerKey[i] !='F');                   
    }
};

void inputStudentData(){
    cout << endl << endl << "STUDENTS" << endl;

    for (int i = 0; i < SIZE_OF_CLASS; i++) {
        cout << "Please enter in the name of Student " << i+1 << ": ";                          
        cin >> studentName[i];
        cout << "Please enter in the results of " << studentName[i] <<"'s test: " << endl;      
            for (int i = 0; i < SIZE_OF_TEST; i++) {
            cout << "Question " << i+1 << ": ";
            do
            cin >> studentAnswer[i];
            while (studentAnswer[i] !='T' && studentAnswer[i] !='F');    
        }
        processData();          
        outputData();           
        answersCorrect = 0; 
        percentage = 0;    
    }
}


 void processData(){
for (int i = 0; i < SIZE_OF_TEST; i++) {
    if (studentAnswer[i] == answerKey[i]){      
        answersCorrect++;                       
            }
        }
cout << "Questions wrong: ";
for (int i = 0; i < SIZE_OF_TEST; i++) {
    if (studentAnswer[i] != answerKey[i]){
        cout << "#" << i+1 << ", ";}
        percentage = (answersCorrect / SIZE_OF_TEST) * 100; 
    }
 }

void outputData(){
cout << endl << answersCorrect << " out of " << SIZE_OF_TEST << " marks." << endl;     
   cout << "Their mark is: " << percentage << "\%." << endl << endl;
}

Last edited on
edit: nevermind
Last edited on
You could do it one step at a time. For example take the variable double percentage = 0;
Move it from the global scope and instead put it inside function main();

You can do a search in the code editor, to find where it is used. But equally, you could just compile the program, and check the error messages, that will tell you where it is used.

If the function needs to modify the value, pass it by reference, add it to the parameter list like this:
void processData(double & percentage);
and where the variable is not to be modified, just pass it by value, like this:
void outputData(double percentage);

In the case of the arrays you don't need to pass by reference, since an array is passed as a pointer. For example, this would be the declaration;
void processData(double & percentage, char answerKey[]);

Of course, the function prototypes and the definitions must be kept synchronised. The function calls must also be modified accordingly, such as this, inside function main(), inputStudentData(percentage, answerKey);

Note that the examples I've quoted are not the finished code, as I suggested to work through the parameters one at a time.

One more thing, line 70 is inside a loop, I suspect it should be after the end of the loop.
percentage = (answersCorrect / SIZE_OF_TEST) * 100;
Last edited on
Sorry, I keep running through my code as I'm typing this and fixing problems here and there but now I have one in my processData function call..
 
processData(answerKey[SIZE_OF_TEST], studentAnswer[SIZE_OF_TEST], &answersCorrect, &percentage);

which gives me this

error: cannot convert `double*' to `double' for argument `3' to `void processData(char, char, double, double)'|

Last edited on
If you are calling the function, the code should look something like this:

processData(answerKey, studentAnswer, answersCorrect, percentage);

You may find it beneficial to take a short time-out from working on the code and look at the tutorial, where it discusses passing parameters by reference or by value.
http://www.cplusplus.com/doc/tutorial/functions2/


Passing arrays as parameters is described on this page:
http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
Agh okay, seems like the only error I have now is it's saying that

" undefined reference to `inputStudentData(char*, char*, std::string*, double, double)' "

I get the basics of parameters, just working backwards through a code that was working perfectly fine beforehand is challenging for a beginner. Well for me, anyways.
Last edited on
undefined reference - sounds like the function prototype doesn't match the definition.
OH MY GOD I FINALLY GOT IT!!!!!!! Faaack. Thanks for your help, I'm seriously fucking grateful lol.

Edit: Yeah that was it. LOL ugh working backwards wasn't the best idea.
Last edited on
Glad you got it done. Actually, although you found this tricky, its a good skill to have, re-organising existing code can be useful - and if you need to do this in future, it will seem much easier.
Topic archived. No new replies allowed.