C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 27th, 2005, 05:50 PM
tooliscrowned tooliscrowned is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 5 tooliscrowned User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 21 sec
Reputation Power: 0
Question problem with classes

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.

Reply With Quote
  #2  
Old March 27th, 2005, 09:37 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
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:
Originally Posted by XP SP2 cmd.exe
Name:aaaaa
Class 1:4
Class 2:4
Class 3:8
GPA:4

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.

Reply With Quote
  #3  
Old March 28th, 2005, 12:49 PM
tooliscrowned tooliscrowned is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 5 tooliscrowned User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 21 sec
Reputation Power: 0
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.

Reply With Quote
  #4  
Old March 28th, 2005, 03:10 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
glad i could help.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > problem with classes


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT