![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 6
Rep Power: 0
![]() |
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::operator" 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. #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;
} |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
I fixed your syntax errors, but didn't try to run it...
Look over the changes , most were pretty simple. #include <iostream.h>
class school
{
public:
school() {}
~school() {}
void setcl(int x);
void gpa(int x, int y) { yourgpa=(x/y); } // x/y could be a float also check for divide by zero
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) // not intx
{
int y;
for (y=1; y<(x+1); y++)
{
cout << "Enter grade for class " << y;
cin >> yourgrades[(y-1)]; // cin uses >>
}
}
int main()
{
school boy;
int i=0;
int y; //never used
int total=0;
int cl;
float gpa; //never used
char name [50];
cout << "Enter your name: " << endl;
cin.get(name, 49);
do
{
cout << "Enter the number of classes you take: " << endl; //missing semi-colon
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(); //needed ()
return 0;
}Now here's some new code for you to start width. obviously it's not complete. you should keep all the variables related to a student within the student class and then access them using set and get (send) functions. These set functions may be doing more than they should be, but work. You shouldn't necessairly prompt for input with a function called setName... #include <iostream.h>
class student {
public:
student() {}
~student() {}
void setGrades();
void setName();
void setNumberOfClasses();
float sendGPA();
int sendGrades(int x) const {return yourGrades[x];}
private:
int yourGrades[10];
int numberOfClasses;
char name[50];
};
void student::setGrades()
{
int y;
for (y=0; y < numberOfClasses ; y++)
{
cout << "Enter grade for class " << y+1 << endl;
cin >> yourGrades[y];
}
}
void student::setName()
{
cout << "Enter your name: " << endl;
cin.get(name, 49);
}
void student::setNumberOfClasses()
{
cout << "Enter the number of classes you take: " << endl;
cin >> numberOfClasses;
}
float student::sendGPA()
{
float GPA = 0;
//calculate and return GPA
return GPA;
}
int main()
{
student boy;
boy.setName();
boy.setNumberOfClasses();
boy.setGrades();
return 0;
}Last edited by spydoor; Mar 28th, 2005 at 9:35 AM. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 6
Rep Power: 0
![]() |
thanks. i realize my program isnt exactly the most practical, but thanks for helping me find those stupid mistakes....
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|