| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
i am trying to write a program that does the following:
asks for name asks for number of classes taken at school asks for grade of each class displays name displays each class number and grade. i am getting several errors when i try to compile, mostly "ostream: perator" type errors ( i am using DJGPP)i know there must be something very basic i am skipping over... also there are probably some syntax errors because after it didn't work the first time i tried re-writing some parts, and then just got even more errors. Code:
#include <iostream.h>
class school
{
public:
school() {}
~school() {}
void setcl(int x);
void gpa(int x, int y) {yourgpa=(x/y);}
float sendgpa(void) const {return yourgpa;}
int sendgrades(int x) const {return yourgrades[x];}
private:
int yourgrades[10];
float yourgpa;
};
void school::setcl(intx)
{
int y;
for (y=1; y<(x+1); y++)
{
cout << "Enter grade for class " << y;
cin << yourgrades[(y-1)];
}
}
int main()
{
school boy;
int i=0;
int y;
int total=0;
int cl;
float gpa;
char name [50];
cout << "Enter your name: " << endl;
cin.get(name, 49);
do
{
cout << "Enter the number of classes you take: " << endl
cin >> cl;
}while ((cl > 11) || (cl < 0));
boy.setcl(cl);
for (i=0; i<cl; i++)
{
total = boy.sendgrades(i) + total;
}
boy.gpa(total, cl);
cout << "Name: " << name << endl;
for (i=0; i < (cl+1); i++)
{
cout << "Class" << i+1 << ": " << boy.sendgrades(i) << endl;
}
cout << "Your GPA: " << boy.sendgpa;
return 0;
}
please help. |
|
#2
|
|||
|
|||
|
here you are
mainly you just had some typos in your code. Code:
#include <iostream> //with c++, the headers drop the .h (don't ask me why, they are just updated somehow)
using namespace std; //with the no-.h headers, you need to include this statement. google "c++ namespaces" or something for more information.
class school
{
public:
school() {}
~school() {}
void setcl(int x);
void gpa(int x, int y) {yourgpa=(x/y);}
float sendgpa(void) const {return yourgpa;}
int sendgrades(int x) const {return yourgrades[x];}
private:
int yourgrades[10];
float yourgpa;
};
void school::setcl(int x) //probably a typo, you missed the space between "int" and "x"
{
int y;
for (y=1; y<(x+1); y++)
{
cout << "Enter grade for class " << y;
cin >> yourgrades[(y-1)]; //most likely another typo: when using cin, use >> instead of <<
}
}
int main()
{
school boy;
int i=0;
int y;
int total=0;
int cl;
float gpa;
char name [50];
cout << "Enter your name: " << endl;
cin.get(name, 49);
cin.ignore(); //cin.get() doesn't remove the delimiting character (in this case, a linebreak) from the keyboard buffer, so without this statement (which clears the buffer) it skips the next name
do
{
cout << "Enter the number of classes you take: " << endl;
cin >> cl;
}while ((cl > 11) || (cl < 0));
boy.setcl(cl);
for (i=0; i<cl; i++)
{
total = boy.sendgrades(i) + total;
}
boy.gpa(total, cl);
cout << "Name: " << name << endl;
for (i=0; i < (cl+1); i++)
{
cout << "Class" << i+1 << ": " << boy.sendgrades(i) << endl;
}
cout << "Your GPA: " << boy.sendgpa(); //make sure you include the parenthese in function calling, even when there are no parameters. otherwise the compiler interprets it as a function pointer.
return 0;
}
this compiles and runs for me in bloodshed dev-c++ 4.9.9.2 as you can see, I did not modify anything in your code except minor typos. i left comments wherever i fixed stuff. however, your program still does not work quite right--I said I had 2 classes, and entered a grade of 4 for both, and the output was Quote:
or something like that. as you can see, where it displays class 3 that should be total or something to that effect. also, i find it counterintuitive that i have to enter grades in numerical format. it would be easy enough to accept a 2-character string (letter and +/-) and write a function to convert that to a number. but it is your program and I am leaving these minor finishing touches to you. Of course, if you try and have more questions, then by all means post back here and I and others will help you out. |
|
#3
|
|||
|
|||
|
thanks for the help...
and i found the reason that when it prints it prints one extra class with a value of zero...i just took the "i+1" thing out from where i printed the grades... all of those were just dumb typos except for the cin.ignore(); AHH how annoying. |
|
#4
|
|||
|
|||
|
glad i could help.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > problem with classes |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|