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 April 4th, 2005, 01:38 AM
k0ld k0ld is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 1 k0ld User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m
Reputation Power: 0
Simple array/class problem???

Here is the code:

#include <iostream>
#include <cstdlib>
using namespace std;
class student
{
public:
void inputScores(); // Prompts user to enter 2 quiz, a mid term, and a final score; store
// them in corresponding data members.
void computeGrade(); // Compute average and then convert the average to letter grade
void displayRecord(); // Displays the name; two quiz, midterm, and final scores; letter grade
// of a student in a single row, e.g., John 95 85 90 90 90 A
private:
string name;
int quiz1; // 0 - 10
int quiz2; // 0 - 10
int midterm; // 0 - 100
int final; // 0 – 100
float average;
char grade; // letter grade: A, B, C, D, or F
int num;
int i;
};

int main()
{
int test;
const int SIZE = 10;
student stats;
student s[SIZE];

stats.inputScores();
stats.computeGrade();
stats.displayRecord();
cin >> test;
return 0;

};
void student::displayRecord()
{
cout <<"\n\n\nQuiz1 Quiz2 Midterm Final Average Grade Name" << endl;
cout <<"_____ _____ _______ _____ _______ _____ ____" << endl;

for ( i = 0; i < num; i++)
{
cout << s.quiz1[i] << " " << s.quiz2[i] <<" " << s.midterm[i] <<" ";
cout << s[i].final << " " << s[i].average << " " << s[i].grade << " ";
cout << s[i].name << endl;

}
}


void student::computeGrade()
{
float quizaverage, midtermaverage, finalaverage;

for ( i = 0; i < num; i++)
{
quizaverage = ((s[i].quiz1 + s[i].quiz2) / 2) * .25;
midtermaverage = s[i].midterm * .25;
finalaverage = s[i].final * .5;
s[i].average = quizaverage + midtermaverage + finalaverage;

if (average >= 90 && average <= 100)
grade = 'A';
else if (average >= 80 && average <= 89)
grade = 'B';
else if (average >= 70 && average <= 79)
grade = 'C';
else if (average >= 60 && average <= 69)
grade = 'D';
else
grade = 'F';
}
}
void student::inputScores()
{

cout << "Enter the number of students whose grades you wish to compute: ";
cin >> num;
for (i = 0; i < num; i++)
{
cout <<"\nEnter the students name: ";
cin >> s.name[i];
cout << "\nEnter the first quiz grade: ";
cin >> s.quiz1[i];
cout << "\nEnter the second quiz grade: ";
cin >> s.quiz2[i];
cout << "\nEnter your midterm grade: ";
cin >> s.midterm[i];
cout << "\nEnter your final test grade: ";
cin >> s.final[i];
}
}


As you can see, I attempted both ways using s.quiz1[i] and s[1].quiz1 however both turned up as errors saying I need to declare S in a function first??? I owe you big time to whoever can help me out with this.

Reply With Quote
  #2  
Old April 4th, 2005, 04:18 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
quiz1 and quiz2 are single variables, not arrays.

Reply With Quote
  #3  
Old April 7th, 2005, 09:56 AM
mail2virag mail2virag is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 3 mail2virag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 31 sec
Reputation Power: 0
Talking

"s" is not in a scope of main(). Its scope is at function level only, so declared it

in the start of main() function.

Reply With Quote
  #4  
Old April 7th, 2005, 10:03 AM
mail2virag mail2virag is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 3 mail2virag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 31 sec
Reputation Power: 0
Talking

Correct code. Sorry for the misunderstanding in above reply.
Code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

class student 
{
public:
 void inputScores(); // Prompts user to enter 2 quiz, a mid term, and a final score; store 
// them in corresponding data members.
 void computeGrade(); // Compute average and then convert the average to letter grade
 void displayRecord(); // Displays the name; two quiz, midterm, and final scores; letter grade 
// of a student in a single row, e.g., John 95 85 90 90 90 A
private:
 string name;
 int quiz1; // 0 - 10
 int quiz2; // 0 - 10
 int midterm; // 0 - 100
 int final; // 0 – 100
 float average;
 char grade; // letter grade: A, B, C, D, or F
 int num;
 int i;
};

int main()
{
int test;
const int SIZE = 10;
student stats;
student s[SIZE];

stats.inputScores();
stats.computeGrade();
stats.displayRecord();
cin >> test;
return 0;

};

void student::displayRecord()
{
cout <<"\n\n\nQuiz1 Quiz2 Midterm Final Average Grade Name" << endl;
cout <<"_____ _____ _______ _____ _______ _____ ____" << endl;

for ( i = 0; i < num; i++)
{
cout << quiz1 << " " << quiz2 <<" " << midterm <<" "; 
cout << final << " " << average << " " << grade << " ";
cout << name << endl;
}
}


void student::computeGrade()
{
float quizaverage, midtermaverage, finalaverage;

for ( i = 0; i < num; i++)
{ 
quizaverage = ((quiz1 + quiz2) / 2) * .25; 
midtermaverage = midterm * .25;
finalaverage = final * .5;
average = quizaverage + midtermaverage + finalaverage; 

if (average >= 90 && average <= 100)
grade = 'A';
else if (average >= 80 && average <= 89) 
grade = 'B';
else if (average >= 70 && average <= 79) 
grade = 'C';
else if (average >= 60 && average <= 69) 
grade = 'D';
else
grade = 'F';
}
}
void student::inputScores()
{

cout << "Enter the number of students whose grades you wish to compute: ";
cin >> num;
for (i = 0; i < num; i++)
{
cout <<"\nEnter the students name: ";
cin >> name;
cout << "\nEnter the first quiz grade: ";
cin >> quiz1;
cout << "\nEnter the second quiz grade: ";
cin >> quiz2;
cout << "\nEnter your midterm grade: ";
cin >> midterm;
cout << "\nEnter your final test grade: ";
cin >> final;
} 
}

 

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Simple array/class problem???


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