classes

hey guys am writting a program using classes that prompts for class name and grades and than outputs the name of the class and its class average but my code isnt running any suggestions? thanx in advance


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
102
103
104
105
106
107
108
109
110
111
 #include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "class_average.h"

using namespace std;

int main()
{

 string classs;
 int grade;
 class class_average main_class(classs,grade);

 cout<< "please enter class name"<<endl;
 getline(cin, classs);
 cout<<" class name is:  "<<main_class.displays()<<endl;
 cout<<"please enter grades and -1 to quit"<<endl;
 cin>>grade;
 cout<<main_class.average()<<endl;

  return 0;
}

#ifndef CLASS_AVERAGE_H
#define CLASS_AVERAGE_H
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;
class class_average
{
public:
    class_average(); // default constructor
    class_average( string, int ); // overloaded constructor
    ~class_average();
    string getClass() const;
    int getGrade() const;
    void setClass(string);
    void setGrade(int);
    double average();
    void displays();

private:
    string newClass;
    int newGrade;



};

#endif // CLASS_AVERAGE_H


#include "class_average.h"

class_average::class_average()
{
    newGrade=0;

}
class_average::class_average(string classs, int grade)
{
    classs=newClass;
    grade=newGrade;
}
class_average::~class_average()
{

}
string class_average::getClass() const
{
    return newClass;
}
int class_average::getGrade() const
{
    return newGrade;
}
void class_average:: setClass( string classs)
{
    classs=newClass;
}
void class_average::setGrade( int grade)
{
    grade=newGrade;
}
double class_average::average()
{
    int total=0;
    float averages;
    vector<int> data;
    while((cin>>newGrade)&&(newGrade!=-1))
    {
        data.push_back(newGrade);
    }

    for(int i=0; i<data.size(); i++)
    {
        total=total+newGrade;
    }
    averages=total/(data.size());
    return averages;
}
void class_average::displays
{
   cout<<newClass<<endl;
}
Line 14 remove class .
Line 65-69 , newClass = classs;newGrade = grade.
hey thanx for the reply but its not working i am unable to output the name of the class and my average is way wrong with exponents for simple intergers.....
still these are to be replace , I am gonna open Visual Studio then to see .
In class_average.cpp your method display is missing ()

In main.cpp you cannot put a void return in a stream

cout<<" class name is: "<<main_class.displays()<<endl;

replace it by :
1
2
 cout<<" class name is:  "<< endl;
 main_class.displays();

this line :
int grade;
with crash since you cannot set an attribute (in constructor) with an unassign value
Note : you should name your classes with a capital
this doesn't even compile.
for example, line 107:
 
void class_average::displays

you've missed the '()' after displays.

even you've you've got them in, this would still not compile:
cout<<" class name is: "<<main_class.displays()<<endl;
as your displays() returns void. Consider something like this:
1
2
3
4
string class_average::displays()
{	
	return newClass;
}
if you want to call it like you're doing at the moment.

This:
averages=total/(data.size());

will mostly yield the wrong result. google "integer division" too find out why.

you tell the compiler that averages() returns a double, but you actually return a float:
1
2
3
float averages;
....
return averages;


You have not initialised grade on line 13, before passing it into your object.

I'd sort those out first.
Last edited on
Topic archived. No new replies allowed.