I want to know how to pass value from constructor to a function

I want to know how to pass value from constructor I have created to a function called void displayValue to display the object value created in the main part...here 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
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
97
98
99
100
101
     #include <iostream>
using namespace std;

class StudentRecord
{
      
             
      public :
             StudentRecord(int new_quiz1, int new_quiz2);
             StudentRecord();
             void displayValue(); 
             double calculateResult();
             char findGrade();
             double quiz1,quiz2;
             double midterm;
             double final;
      private : 
             char grade;
             double quiz1_percent, quiz2_percent,total_quiz,midterm_percent,final_percent;
             double numeric_score;
             
};

StudentRecord::StudentRecord()
{
        quiz1 = 0;
        quiz2 = 0;
        }
        
StudentRecord::StudentRecord(int new_quiz1, int new_quiz2)
{
     quiz1 = new_quiz1;
     quiz2 = new_quiz2;
     }

void StudentRecord::displayValue()
{
     cout<<"The mark of quiz 1 and quiz 2 is : "<<endl;
}      
int main()
{
    StudentRecord result;
    StudentRecord p;
    StudentRecord q(8,9);
    cout<<" Enter first quiz mark "<< endl;
    cin>> result.quiz1;
    cout<<" Enter second quiz mark "<<endl;
    cin>> result.quiz2;
    cout<< " Enter midterm exam mark " <<endl;
    cin>> result.midterm;
    cout<< " Enter final exam mark "<<endl;
    cin>> result.final; 
    result.calculateResult();
    q.displayValue();
    
    system("pause");
    return 0;
}

double StudentRecord::calculateResult()
{     
      StudentRecord result_percent;
     
     quiz1_percent = 1.00 *(result_percent.quiz1/10.0);
     quiz2_percent = 1.00*(result_percent.quiz2/10.0);
     total_quiz = quiz1_percent + quiz2_percent;
     midterm_percent = 1.00*(result_percent.midterm/100.0);
     final_percent = 1.00*(result_percent.final/100.0);
     result_percent.numeric_score = ((total_quiz*0.25) + (midterm_percent*0.25) + (final_percent*0.50))/1.00;
     cout<< " Total quiz marks : "<<result_percent.total_quiz<<endl;
    cout<< " Midterm : "<<result_percent.midterm_percent<<endl;
    cout<< " Final exam : " << result_percent.final_percent<<endl;
    cout<< " Course average score : "<< result_percent.numeric_score << endl;
    cout<< " Your grade : " << result_percent.findGrade() << endl;
    cout<<endl;
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
}

char StudentRecord::findGrade()
{
     char grade;
     
     
     if(numeric_score  < 90)
        grade = 'B';
     else if(numeric_score  < 80)
        grade = 'C';
     else if(numeric_score  < 70)
        grade = 'D';
     else if(numeric_score  < 60)
        grade = 'F';    
     else
        grade = 'A';
        
        return grade;
        }
    

Last edited on
What exactly you want to do.?
i want to display the q(8,9) ,which i wan to pass to the function void displayValue() 1st, den call it in the main program
closed account (D80DSL3A)
You don't need to pass the values. As data members of the class, quiz1 and quiz2 can simply be used in member functions.

1
2
3
4
void StudentRecord::displayValue()
{
     cout<<"The mark of quiz 1 and quiz 2 is : "<<quiz1<<" and "<<quiz2<<endl;
} 


Simply call the function from the constructor:

1
2
3
4
5
6
StudentRecord::StudentRecord(int new_quiz1, int new_quiz2)
{
     quiz1 = new_quiz1;
     quiz2 = new_quiz2;
     displayValue();
}


Unfortunately there are other problems with your code. For example, you are using a local instance of StudentRecord in calculateResult(). I'm sure you intend to use the values for the instance calling the function. An example of this is line 64:
quiz1_percent = 1.00 *(result_percent.quiz1/10.0);
Which should be:
quiz1_percent = 1.00 *(quiz1/10.0);// also, why multiply by 1.00 ?
Similarly for other lines in the function. This function also needs to return a double value since you declared that as the return type. If it doesn't need to return any value then change the return type to void.

There is also a logical error with findGrade(). If numeric_score < 90 the grade will always be a 'B', never 'C', 'D' or 'F'.

Hope this helps.
ok...i get what u mean, after removing all the result_percent , i still cant get the exactly output, the output is sumting strange number...and aso i place the displayValue(); in the constructor, it stil din display the content of the function void Student::displayValue() ..
I understanding the concept u have explained, but jst dun get the exactly matching idea, it's just sumting need to sort it out...i feel im more close to the solution
 Enter first quiz mark:
7
Enter second quiz mark:
7
Enter midterm exam mark:
7.6
Enter final exam mark:
7.7

 Total quiz marks : 4.83816e-312
 Midterm : 3.01037e-307
 Final Exam : 1.0123e+268
 Course average score : 0
 Your grade : ╨

Press any key to continue . . . 


this is the output i get
closed account (D80DSL3A)
i wud nd 2 c yr mofifyd cod 2 tel wat u did.
ok...here 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
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
97
98
99
100
101
#include <iostream>
using namespace std;

class StudentRecord
{
      
             
      public :
             StudentRecord(int new_quiz1, int new_quiz2);
             StudentRecord();
             void displayValue(); 
            void calculateResult();
             char findGrade();
             double quiz1,quiz2;
             double midterm;
             double final;
      private : 
             char grade;
             double quiz1_percent, quiz2_percent,total_quiz,midterm_percent,final_percent;
             double numeric_score;
             
};

StudentRecord::StudentRecord()
{
        quiz1 = 0;
        quiz2 = 0;
        }
        
StudentRecord::StudentRecord(int new_quiz1, int new_quiz2)
{
     quiz1 = new_quiz1;
     quiz2 = new_quiz2;
     displayValue();
     }

void StudentRecord::displayValue()
{
      
     cout<<"The mark of quiz 1 and quiz 2 is : "<<quiz1<<"and"<<quiz2<<endl;
     
     }
int main()
{
    StudentRecord result;
    StudentRecord p;
    StudentRecord q(8,9);
    cout<<" Enter first quiz mark "<< endl;
    cin>> result.quiz1;
    cout<<" Enter second quiz mark "<<endl;
    cin>> result.quiz2;
    cout<< " Enter midterm exam mark " <<endl;
    cin>> result.midterm;
    cout<< " Enter final exam mark "<<endl;
    cin>> result.final; 
    result.calculateResult();

    system("pause");
    return 0;
}

void StudentRecord::calculateResult()
{     
     
     quiz1_percent = (quiz1/10.0);
     quiz2_percent = (quiz2/10.0);
     total_quiz = quiz1_percent + quiz2_percent;
     midterm_percent = (midterm/100.0);
     final_percent = (final/100.0);
    numeric_score = ((total_quiz*0.25) + (midterm_percent*0.25) + (final_percent*0.50))/1.00;
     cout<< " Total quiz marks : "<<total_quiz<<endl;
    cout<< " Midterm : "<<midterm_percent<<endl;
    cout<< " Final exam : " << final_percent<<endl;
    cout<< " Course average score : "<< numeric_score << endl;
    cout<< " Your grade : " << findGrade() << endl;
    cout<<endl;
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
   
}

char StudentRecord::findGrade()
{
     char grade;
     
     
     if(numeric_score  < 90)
        grade = 'B';
     else if(numeric_score  < 80)
        grade = 'C';
     else if(numeric_score  < 70)
        grade = 'D';
     else if(numeric_score  < 60)
        grade = 'F';    
     else
        grade = 'A';
        
        return grade;
        }
    


Other than the code being very messy, I also have troubles figuring out what's causing the problems. This kind of output is normally caused by using unitialized variables, but I can't seem to find which one you didn't init.
ya...i have been serching the whole way trying to figure it out...i knw the output shud be related to failure of passing variable,declaration or initialized, but every variable i have created seems being declared and initialized properly, cant get any idea...any other solutions are welcome
closed account (D80DSL3A)
Very odd. I copied your code to my compiler and used the same values as you did. Here is the output I get:

The mark of quiz 1 and quiz 2 is : 8and9
Enter first quiz mark
7
Enter second quiz mark
7
Enter midterm exam mark
7.6
Enter final exam mark
7.7
Total quiz marks : 1.4
Midterm : 0.076
Final exam : 0.077
Course average score : 0.4075
Your grade : B

Press any key to continue . . .


These are the results I'd expect from your code.
I can't imagine why you aren't getting these results. You did build and run the new code, right?
What you got looks like what I'd expect from the old code.
wat the hell...tis is funny...i did run the exactly,y i getting odd result...lolz!!! u r using Dev Blooshed C++ ?
it's working!!! after rebuilding the file, it works exactly wat u display, thx for ur help, but i stil duno how to modify the course average score floating point to become exactly like mark percentage e.g : 40.75 .... and so do on midterm,quiz mark and so on...
closed account (D80DSL3A)
You're welcome. Glad your program works now. You can now experiment with your formulas to get them right. That's a math issue, not a programming issue.

You may do well to focus more carefully on your work. I'd bet the carelessness displayed in your spelling is also affecting your programming.
ok...thx anyway...1 more ting i wanna know...since you are experience in writting C++ program, can you suggest anything I can do to so i can improve my programming skill and logical thinking? through exercise, books, working, or any other suggestions?
closed account (D80DSL3A)
Reading a book or taking a class is needed to build knowledge, but for building skill I find it's practice, practice, practice!!
Topic archived. No new replies allowed.