Class isn't returning.

My code is running fine, but when I call the return function in my header file nothing outputs. Any comments would be very helpful.

Header File
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
#ifndef ESSAYCLASS_H
#define ESSAYCLASS_H

class EssayClass
{
private:
    double Grammar;
    double Spelling;
    double Correctlen;
    double Content;
    double score;
    char letterGrade;
    
public:
    EssayClass ()
{ Grammar = 0.0; 
  Spelling = 0.0;
  Correctlen = 0.0;
  Content = 0.0; 
  score = 0.0;
  letterGrade = ' ';}
EssayClass(double g, double sp, double cl, double c,double s, char lg)
{  g = Grammar;
 sp = Spelling;
 cl = Correctlen;
 c = Content;
 s = score;
 lg = letterGrade;}
void setScore( double g, double sp, double cl, double c)
{g = Grammar;
 sp = Spelling;
 cl = Correctlen;
 c = Content; }

 void addScore()
 { score = Grammar + Spelling + Correctlen + Content; }
 double getScore ()
 { return score; }
 
 void FindletterGrade()
{ 
    if (score > 89)
            letterGrade = 'A';
        else if (score > 79)
            letterGrade = 'B';
        else if (score > 69)
            letterGrade = 'C';
        else
            letterGrade = 'F';
}
 char getletterGrade () const {
     return letterGrade;
 }
 };
#endif 


Main File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include "EssayClass.h"
using namespace std;
int main (){
    double score1, score2, score3, score4;
    EssayClass Student;
    cout << "Please enter up to 30 points for Grammar: "; 
    cin >> score1 ;
    cout << "Please enter up to 20 points for Spelling: ";
    cin >> score2 ; 
    cout << "Please enter up to 20 points for Correct Length: ";
    cin >> score3 ; 
    cout << "Please enter up to 30 points for Content: ";
    cin >> score4;
    Student.setScore(score1, score2, score3, score4) ;
    Student.addScore();
    Student.getScore();
    Student.FindletterGrade();
    Student.getletterGrade();
    

}
closed account (2b5z8vqX)
My code is running fine, but when I call the return function in my header file nothing outputs.

The returned values of the member functions being called are not being written to any output stream like the data in the preceding statements. If you wish to write them to the standard output stream, then simply pass the returned values as arguments to the std::cout::operator << (...) member function. Otherwise, use one of the other methods of outputting data.

http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt
You aren't outputting them (I'm assuming you're talking about the last three lines of your main function).

You are simply calling the function. In your case, that is not enough because the functions don't print inherently; they only return the values. So, you have to print the functions... so to speak.

1
2
3
cout << Student.getScaore() << endl;
cout << Student.FindletterGrade() << endl;
cout << getletterGrade() << endl;


EDIT: @nramsey beat me to the punch!
Last edited on
1
2
3
4
5
6
g = Grammar;
 sp = Spelling;
 cl = Correctlen;
 c = Content;
 s = score;
 lg = letterGrade;

is backwards.

Same with

1
2
3
4
g = Grammar;
 sp = Spelling;
 cl = Correctlen;
 c = Content;


You had it right the first constructor but not the other two.

I might suggest you read up on functions and parameters a little more until you get the feel of them before moving on to data structures.

Thanks for the help. :) Here's my updated code.

Header
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
#ifndef ESSAYCLASS_H
#define ESSAYCLASS_H

class EssayClass
{
private:
    double Grammar;
    double Spelling;
    double Correctlen;
    double Content;
    double score;
    char letterGrade;
    
public:
    EssayClass ()
{ Grammar = 0.0; 
  Spelling = 0.0;
  Correctlen = 0.0;
  Content = 0.0; 
  score = 0.0;
  letterGrade = ' ';}
EssayClass(double g, double sp, double cl, double c,double s, char lg)
{  Grammar = g;
  Spelling = sp;
  Correctlen = cl;
  Content = c;
  score = s;
  letterGrade = lg;}
void setScore( double g, double sp, double cl, double c)
{ Grammar = g;
  Spelling = sp;
  Correctlen = cl;
  Content = c; }

 void addScore()
 { score = Grammar + Spelling + Correctlen + Content; }
 double getScore ()
 { return score; }
 
 void FindletterGrade()
{ 
    if (score > 89)
            letterGrade = 'A';
        else if (score > 79)
            letterGrade = 'B';
        else if (score > 69)
            letterGrade = 'C';
        else
            letterGrade = 'F';
}
 char getletterGrade () const {
     return letterGrade;
 }
 };
#endif 


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
#include <iostream>
#include "EssayClass.h"
using namespace std;
int main (){
    double score1, score2, score3, score4;
    EssayClass Student;
    cout << "Please enter up to 30 points for Grammar: "; 
    cin >> score1 ;
    cout << "Please enter up to 20 points for Spelling: ";
    cin >> score2 ; 
    cout << "Please enter up to 20 points for Correct Length: ";
    cin >> score3 ; 
    cout << "Please enter up to 30 points for Content: ";
    cin >> score4;
    Student.setScore(score1, score2, score3, score4) ;
    Student.addScore();
    
    Student.FindletterGrade();
    cout << "Student received a " << Student.getScore();
    cout << " out of 100 on the essay. Student receives a(n) " << 
            Student.getletterGrade();
    cout << " on the assignment.";
    
return 0;
}
One more thing to mention. Headers are mainly for prototypes and only definitions of templated functions. Then an implementation file is used for the definitions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//test.hpp
class Test
{
    public:
        void test( void );
};

//test.cpp
#include "test.hpp"
void Test::test( void )
{
    //nothing since I am a test
}

//main.cpp
#include "test.hpp"

int main()
{
    return 0;
}

Topic archived. No new replies allowed.