Can someone tell me why my function is not working?

Pages: 12
Oct 20, 2017 at 2:04pm
can anyone help with the weighted grade program we have to write in programming 1? heres my code, for some reason my function is not being called properly and it wont activate, but when ii tried the code without the function and just the code inserted it worked perfectly



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

using namespace std;

double calcFuntion(double&, char, double&, double&, double&, double&, double&, double&, double&, double&, double&, double&);

int main()
{

ifstream inFile;
ofstream outFile;
int studentID;
double midterm, final, research, group, participation;
char grade;
double midWeight = 0.25, finalWeight = 0.25, researchWeight = 0.2;
double groupWeight = 0.2, participationWeight = 0.1;
double totalNumGrade = 0;

inFile.open("studentRec.txt");
outFile.open("studentLetterGrade.txt");

inFile >> studentID >> midterm >> final >> research >> group >> participation;

outFile << fixed << setprecision(2);
while(inFile)
    {


    if((totalNumGrade > -1) && (totalNumGrade < 101))
        {    
totalNumGrade = (midWeight * midterm) + (finalWeight * final) + (researchWeight * research) + (groupWeight * group) + (participationWeight * participation);
double calcFunction((totalNumGrade, grade, midWeight, midterm, finalWeight, final, researchWeight, research, groupWeight, group, participationWeight, participation));
    
        }
        else
            {
                outFile << "Invalid grade entries" << endl;    
            }
        
    outFile << studentID << " " << totalNumGrade << " " << grade << endl;

    inFile >> studentID >> midterm >> final >> research >> group >> participation;
        
    }
return 0;
}

double calcFunction(double& totalNumGrade, char grade, double& midWeight, double& midterm, double& finalWeight,
         double& final,    double& researchWeight, double& research, double& groupWeight, double& group,
        double& participationWeight, double& participation)
    {    
totalNumGrade = (midWeight * midterm) + (finalWeight * final) + (researchWeight * research) + (groupWeight * group) + (participationWeight * participation);

            
        if (totalNumGrade >= 90)
                    {grade = 'A';}
           else if (totalNumGrade >= 80)
                   {grade = 'B';}
             else if (totalNumGrade >= 70)
                  {grade = 'C';}
            else if (totalNumGrade >= 60)
                   {grade = 'D';}
              else
                 {grade = 'F';}
          return grade;
    };
Last edited on Oct 20, 2017 at 3:00pm
Oct 20, 2017 at 2:19pm
Spelling error, calcFuntion and calcFunction.

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

using namespace std;

double calcFunction(double&, char, double&, double&, double&, double&, double&, double&, double&, double&, double&, double&);

int main()
{
    ifstream inFile;
    ofstream outFile;
    int studentID;
    double midterm, final, research, group, participation;
    char grade;
    double midWeight = 0.25, finalWeight = 0.25, researchWeight = 0.2;
    double groupWeight = 0.2, participationWeight = 0.1;
    double totalNumGrade = 0;

    inFile.open("studentRec.txt");
    outFile.open("studentLetterGrade.txt");

    inFile >> studentID >> midterm >> final >> research >> group >> participation;

    outFile << fixed << setprecision(2);
    while (inFile)
    {
        if ((totalNumGrade > -1) && (totalNumGrade < 101))
        {
            totalNumGrade = (midWeight * midterm) + (finalWeight * final) + (researchWeight * research) + (groupWeight * group) + (participationWeight * participation);
            calcFunction(totalNumGrade, grade, midWeight, midterm, finalWeight, final, researchWeight, research, groupWeight, group, participationWeight, participation);
        }
        else
        {
            outFile << "Invalid grade entries" << endl;
        }

        outFile << studentID << " " << totalNumGrade << " " << grade << endl;

        inFile >> studentID >> midterm >> final >> research >> group >> participation;
    }
    return 0;
}

double calcFunction(
        double& totalNumGrade, char grade, double& midWeight, double& midterm, double& finalWeight,
        double& final, double& researchWeight, double& research, double& groupWeight, double& group,
        double& participationWeight, double& participation)
{
    totalNumGrade = (midWeight * midterm) + (finalWeight * final) + (researchWeight * research) + (groupWeight * group) + (participationWeight * participation);

    if (totalNumGrade >= 90)      {grade = 'A';}
    else if (totalNumGrade >= 80) {grade = 'B';}
    else if (totalNumGrade >= 70) {grade = 'C';}
    else if (totalNumGrade >= 60) {grade = 'D';}
    else {grade = 'F';}

    return grade;
}
Last edited on Oct 20, 2017 at 2:20pm
Oct 20, 2017 at 2:46pm
i feel like a idiot now, but i fixed the spelling and its not outputting the letter grade at all, Thank you though
Oct 20, 2017 at 2:46pm
also how did you get the code to pop out like that?
Oct 20, 2017 at 2:51pm
Oct 20, 2017 at 2:59pm
There are a number of problems.
1. calcFunction updates grade. But it's a parameter passed by value into the function. So the updated value never makes it out of the function, except by the return value.

2. calcFunction returns a double. But you return the grade thru that value, which is a char, which gets re-interpreted as an int, then converted to a double. Not ideal.

3. You call calcFunction, but never use the return code.

You could make grade a local variable within calcFunction, and change the function to return a char instead of the double. Then you could pick up the return value when you call the function.

And you'd have one less parameter to that function, which really just uses totalNumGrade. So it should be clear that the function should just take one parameter if you move that calculation out of it, which you already do.
Last edited on Oct 20, 2017 at 3:02pm
Oct 20, 2017 at 3:02pm
Thanks
Oct 20, 2017 at 3:04pm
What IDE would y'all recommend? Right now I am just using notepad and running it in the borland compiler
Last edited on Oct 20, 2017 at 3:04pm
Oct 20, 2017 at 3:19pm
@kbw
So because it never updates, except by the return value, the function is pretty much useless as is.

Are you saying that I should change the double function into something similar to this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char calcFunction(
        double& totalNumGrade, char grade, double& midWeight, double& midterm, double& finalWeight,
        double& final, double& researchWeight, double& research, double& groupWeight, double& group,
        double& participationWeight, double& participation)
{
    totalNumGrade = (midWeight * midterm) + (finalWeight * final) + (researchWeight * research) + (groupWeight * group) + (participationWeight * participation);

    if (totalNumGrade >= 90)      {grade = 'A';}
    else if (totalNumGrade >= 80) {grade = 'B';}
    else if (totalNumGrade >= 70) {grade = 'C';}
    else if (totalNumGrade >= 60) {grade = 'D';}
    else {grade = 'F';}

    return grade;
}



and could further explain what you mean by local variable?
Oct 20, 2017 at 3:26pm
I looked into local variable and so if i make grade a local variable within the calcfunction would that mean that in my int main i would get rid of it?
Oct 20, 2017 at 3:28pm
Since you are on Windows, use the IDE which has good support for debugging C++ code.
Visual Studio Community 2017 would be the stand out choice.
https://www.visualstudio.com/downloads/

In the installer, choose the “Desktop development with C++” workload.
Oct 20, 2017 at 4:26pm
I looked into local variable and so if i make grade a local variable within the calcfunction would that mean that in my int main i would get rid of it?

No. You would assign the return value from this function to your grade variable that is local in main().

If you are always using the same weights, there is not reason to pass them into the function as arguments. You can define them local to the function also, or define constants in global scope. You don't really need to assign weights local to the scope of main(). If you plan to modify the program down the line so that weights can vary at run time, then you will need to pass them in as arguments, and you can ignore this whole paragraph.

By the way, why are you passing in references to double for all of the scores and weights? Since you are not returning anything in these values and they are not big objects, there is no reason you can't just pass in plain old doubles (not references).

Last, I highly recommend you add the argument names to the function declaration in line 6. Technically the names are optional, but it makes it a lot easier to read the argument names and know what each one is. You have 11 double& arguments. Can you easily tell me which is which by reading only line 6? As your programs get more complex, you will need to do this. So start now.
Oct 20, 2017 at 8:02pm
The reason that i am using them as a reference is bc im not too sure on how to use them any other way
Oct 20, 2017 at 8:10pm
Just get rid of the "&"s.

Try this tutorial section:

http://www.cplusplus.com/doc/tutorial/functions/#reference
Oct 20, 2017 at 8:28pm
This is my updated code, when i try to run it, I get

1
2
3
4
5
C:\CIT 1310>bcc32 WeightedGrade.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
WeightedGrade.cpp:
Error E2193 WeightedGrade.cpp 35: Too few parameters in call to 'calcFunction(double,char,double,double,double,double,double,double,double,double,double,double)' in function main()
*** 1 errors in Compile ***



what would be the cause of this? I have already double checked to make sure the functions were exact but i feel like i am missing something



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

using namespace std;

char calcFunction(double totalNumGrade, char grade, double midWeight, double midterm, double finalWeight,
	         double final, double researchWeight, double research, double groupWeight, double group,
	        double participationWeight, double participation);

int main()
{

	ifstream inFile;
	ofstream outFile;
	int studentID;
	double midterm, final, research, group, participation;
	char grade;
	double midWeight = 0.25, finalWeight = 0.25, researchWeight = 0.2;
	double groupWeight = 0.2, participationWeight = 0.1;
	double totalNumGrade = 0;

	inFile.open("studentRec.txt");
	outFile.open("studentLetterGrade.txt");

	inFile >> studentID >> midterm >> final >> research >> group >> participation;

	outFile << fixed << setprecision(2);
	while(inFile)
	    {


	    if((totalNumGrade > -1) && (totalNumGrade < 101))
	        {    
	totalNumGrade = (midWeight * midterm) + (finalWeight * final) + (researchWeight * research) + (groupWeight * group) + (participationWeight * participation);
	grade = calcFunction((totalNumGrade, grade, midWeight, midterm, finalWeight, final, researchWeight, research, groupWeight, group, participationWeight, participation));
    
	        }
	        else
	            {
	                outFile << "Invalid grade entries" << endl;    
	            }
        
 	   outFile << studentID << " " << totalNumGrade << " " << grade << endl;

 	   inFile >> studentID >> midterm >> final >> research >> group >> participation;
        
	    }
	return 0;
	}

	char calcFunction(double totalNumGrade, char grade, double midWeight, double midterm, double finalWeight,
	         double final, double researchWeight, double research, double groupWeight, double group,
	        double participationWeight, double participation)
	    {    
	totalNumGrade = (midWeight * midterm) + (finalWeight * final) + (researchWeight * research) + (groupWeight * group) + (participationWeight * participation);

            
     	   if (totalNumGrade >= 90)
                  {grade = 'A';}
           else if (totalNumGrade >= 80)
                  {grade = 'B';}
           else if (totalNumGrade >= 70)
                  {grade = 'C';}
           else if (totalNumGrade >= 60)
                  {grade = 'D';}
           else
                 {grade = 'F';}

          return grade;

    };
Oct 20, 2017 at 8:49pm
I was able to get it to work. I want to say thank you to all of you for helping me with this and pushing me towards the information i need. I now feel like i actually understand how to create functions better than when i started.
Here is my finished code. Anyone who wishes to give any advice on how i could of written it better, please do and don't worry about being rude or anything because it will only help me to further improve.

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

using namespace std;

char calcFunction(double totalNumGrade, char grade, double midWeight, double midterm, double finalWeight,
	         double final, double researchWeight, double research, double groupWeight, double group,
	        double participationWeight, double participation);

int main()
{

	ifstream inFile;
	ofstream outFile;
	int studentID;
	double midterm, final, research, group, participation;
	char grade;
	double midWeight = 0.25, finalWeight = 0.25, researchWeight = 0.2;
	double groupWeight = 0.2, participationWeight = 0.1;
	double totalNumGrade = 0;

	inFile.open("studentRec.txt");
	outFile.open("studentLetterGrade.txt");

	inFile >> studentID >> midterm >> final >> research >> group >> participation;

	outFile << fixed << setprecision(2);
	while(inFile)
	    {


	    if((totalNumGrade > -1) && (totalNumGrade < 101))
	        {    

       	totalNumGrade = (midWeight * midterm) + (finalWeight * final) + (researchWeight * 
        research) + (groupWeight * group) + (participationWeight * participation);

        grade = calcFunction(totalNumGrade, grade, midWeight, midterm, finalWeight,
	         final, researchWeight, research, groupWeight, group,
	        participationWeight, participation);
    
	        }
	        else
	            {
	                outFile << "Invalid grade entries" << endl;    
	            }
        
 	   outFile << studentID << " " << totalNumGrade << " " << grade << endl;

 	   inFile >> studentID >> midterm >> final >> research >> group >> participation;
        
	    }
	return 0;
	}

	char calcFunction(double totalNumGrade, char grade, double midWeight, double 
        midterm, double finalWeight, double final, double researchWeight, double research, 
        double groupWeight, double group,
        double participationWeight, double participation)
	    {    
	totalNumGrade = (midWeight * midterm) + (finalWeight * final) + (researchWeight * 
                research) + (groupWeight * group) + (participationWeight * participation);

            
     	   if (totalNumGrade >= 90)
                  {grade = 'A';}
           else if (totalNumGrade >= 80)
                  {grade = 'B';}
           else if (totalNumGrade >= 70)
                  {grade = 'C';}
           else if (totalNumGrade >= 60)
                  {grade = 'D';}
           else
                 {grade = 'F';}

          return grade;

    };



I did notice that my if statement is not working as i initially thought it would so now thats what i will be trying to correct
Oct 22, 2017 at 2:29pm
I meant:
 
char calcFunction(double totalNumGrade);
Last edited on Oct 22, 2017 at 2:29pm
Oct 22, 2017 at 5:21pm
@kbw

ok so if I would of set my function that way, all i would of had to to was then use the if statement i had and the return the char grade at the end?
Oct 23, 2017 at 10:27am
No. It just means you don't pass all those redundant parameters to the function. Had you not noticed that most of those parameters aren't used?

Grade is a function of your total score. For example, if you get 95%, you'd expect an A. This is the idea that the function is encoding.
Last edited on Oct 23, 2017 at 10:30am
Oct 23, 2017 at 3:55pm
ok so it would of been more like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char calcFunction(double totalNumGrade, char grade)

	    {    
     	   if (totalNumGrade >= 90)
                  {grade = 'A';}
           else if (totalNumGrade >= 80)
                  {grade = 'B';}
           else if (totalNumGrade >= 70)
                  {grade = 'C';}
           else if (totalNumGrade >= 60)
                  {grade = 'D';}
           else
                 {grade = 'F';}

          return grade;

    };



since i already have the totalNumGrade in the code previously it would feed in that value i have stored in there and then evaluate it to the if statement?
Pages: 12