| | |
Need Help with code!! (Used code tags this time around.)
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
You already have a 'global' (ok so it is local to the class, but all of the class methods can see it) data area defined. See this section under private in your class:
The only reason you aren't sharing the data is because you hide it by re-declaring the same variables inside your functions:
Here
and here
You can just comment out (or delete) the declarations under each function and they will use the class data, sharing it between them.
I noticed why you aren't having to enter four grades for each class, it is the break inside the
I might ask why you even have that loop anyway. Why not just do something like the following? (I have commented out your code where it is not needed or where I have modified it.)
When I was testing, I found another small bug. I gave the student 4 classes with grades of a, a, b, c and the GPA reported was 3, but it should have been 3.25
The average calculation was made using integer math because all of the parts in it were integer (even though the result is a double). I changed it to this:
The 4.0 on the end forces the division to be done in floating point math.
c++ Syntax (Toggle Plain Text)
private: double average; string courseName [4]; char gradeLetter [4]; int gradePoint [4];
The only reason you aren't sharing the data is because you hide it by re-declaring the same variables inside your functions:
Here
c++ Syntax (Toggle Plain Text)
void displayMessage () { char gradeLetter [4] = {}; string courseName [4] = {""};
and here
c++ Syntax (Toggle Plain Text)
double getAverage () { int gradePoint [4] = {}; char gradeLetter [4] = {}; double average = 0.0;
You can just comment out (or delete) the declarations under each function and they will use the class data, sharing it between them.
I noticed why you aren't having to enter four grades for each class, it is the break inside the
for (y ... loop.I might ask why you even have that loop anyway. Why not just do something like the following? (I have commented out your code where it is not needed or where I have modified it.)
c++ Syntax (Toggle Plain Text)
void displayMessage () { //char gradeLetter [4] = {}; //string courseName [4] = {""}; for ( int x = 0; x < 4; x++ ) { cout<< "Please enter name of course:"; getline(cin, courseName [x]); //for ( int y = 0; y < 4; y++ ) //{ cout<< "Enter grade recieved:"; //cin>> gradeLetter [y]; cin>> gradeLetter [x]; // break; //} cin.ignore (100, '\n'); } system("cls"); for ( int x = 0; x < 4; x++ ) { //for ( int y = 0; y < 4; y++ ) //{ //cout<< courseName[x]<< right<< setw (7)<< gradeLetter[y]<< '\n'<<'\n'; cout<< courseName[x]<< right<< setw (7)<< gradeLetter[x]<< '\n'<<'\n'; // break; //} } }
When I was testing, I found another small bug. I gave the student 4 classes with grades of a, a, b, c and the GPA reported was 3, but it should have been 3.25
The average calculation was made using integer math because all of the parts in it were integer (even though the result is a double). I changed it to this:
c++ Syntax (Toggle Plain Text)
average = (gradePoint[0] + gradePoint[1] + gradePoint[2] + gradePoint[3]) / 4.0;
The 4.0 on the end forces the division to be done in floating point math.
•
•
Join Date: Jan 2009
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
Is there a way for me to allow global access for the data entered into the "char gradeLetter" identifier of the "displayMessage ()" function? So that when I run the "getaverage ()" function these char values would already have been there, and it would just be a matter of comparing. I have placed my concern in RED. Oh I don't have a problem with the four grades. It's four courses and a letter grade for each.
#include <iostream> #include <string> #include <iomanip> using namespace std; class GpaCalculator { public: void displayMessage () { char gradeLetter [4] = {}; string courseName [4] = {""}; for ( int x = 0; x < 4; x++ ) { cout<< "Please enter name of course:"; getline(cin, courseName [x]); for ( int y = 0; y < 4; y++ ) { cout<< "Enter grade recieved:"; cin>> gradeLetter [y]; break; } cin.ignore (100, '\n'); } system("cls"); for ( int x = 0; x < 4; x++ ) { for ( int y = 0; y < 4; y++ ) { cout<< courseName[x]<< right<< setw (7)<< gradeLetter[y]<< '\n'<<'\n'; break; } } } double getAverage () { int gradePoint [4] = {}; char gradeLetter [4] = {}; double average = 0.0; for ( int y = 0; y < 4; y++ ) { if (gradeLetter [y] == 'a') { gradePoint [y] = 4; } else if (gradeLetter [y] == 'b') { gradePoint [y] = 3; } else if (gradeLetter [y] == 'c') { gradePoint [y] = 2; } else if (gradeLetter [y] == 'd') { gradePoint [y] = 1; } } average = ( gradePoint [0] + gradePoint [1] + gradePoint [2] + gradePoint [3] ) / 4; return average; } private: double average; string courseName [4]; char gradeLetter [4]; int gradePoint [4]; }; int main() { GpaCalculator cal; cal.displayMessage (); cout<< "Your GPA is:"<< cal.getAverage (); return 0; }
View below:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class GpaCalculator
{
public:
void displayMessage ()
{
for ( int x = 0; x < 4; x++ )
{
cout<< "Please enter name of course:";
getline(cin, courseName [x]);
cout<< "Enter grade recieved:";
cin>> gradeLetter [x];
cin.ignore (100, '\n');
}
system("cls");
for ( int x = 0; x < 4; x++ )
{
cout<< courseName[x]<< right<< setw (7)<< gradeLetter[x]<< '\n'<<'\n';
}
}
double getAverage ()
{
double average = 0.0;
for ( int x = 0; x < 4; x++ )
{
if (gradeLetter [x] == 'a')
gradePoint [x] = 4.0;
else
if (gradeLetter [x] == 'b')
{
gradePoint [x] = 3.0;
}
else
if (gradeLetter [x] == 'c')
{
gradePoint [x] = 2.0;
}
else
if (gradeLetter [x] == 'd')
{
gradePoint [x] = 1.0;
}
}
average = (gradePoint[0] + gradePoint[1] + gradePoint[2] + gradePoint[3])/4.0;
return average;
}
private:
string courseName [4];
char gradeLetter [4];
double gradePoint [4];
};
int main()
{
GpaCalculator cal;
cal.displayMessage ();
cout<< "Your GPA is:"<< cal.getAverage ();
return 0;
}•
•
Join Date: Jan 2009
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
Hey! I must tell you thanks for all your help. You have shed so much light on my program. It's almost done it's just that I'm still getting a int value even though I have placed the 4.0 and also changed the data type for "gradePoint" to double. What is causing this problem?
View below:
#include <iostream> #include <string> #include <iomanip> using namespace std; class GpaCalculator { public: void displayMessage () { for ( int x = 0; x < 4; x++ ) { cout<< "Please enter name of course:"; getline(cin, courseName [x]); cout<< "Enter grade recieved:"; cin>> gradeLetter [x]; cin.ignore (100, '\n'); } system("cls"); for ( int x = 0; x < 4; x++ ) { cout<< courseName[x]<< right<< setw (7)<< gradeLetter[x]<< '\n'<<'\n'; } } double getAverage () { double average = 0.0; for ( int x = 0; x < 4; x++ ) { if (gradeLetter [x] == 'a') gradePoint [x] = 4.0; else if (gradeLetter [x] == 'b') { gradePoint [x] = 3.0; } else if (gradeLetter [x] == 'c') { gradePoint [x] = 2.0; } else if (gradeLetter [x] == 'd') { gradePoint [x] = 1.0; } } average = (gradePoint[0] + gradePoint[1] + gradePoint[2] + gradePoint[3])/4.0; return average; } private: string courseName [4]; char gradeLetter [4]; double gradePoint [4]; }; int main() { GpaCalculator cal; cal.displayMessage (); cout<< "Your GPA is:"<< cal.getAverage (); return 0; }
![]() |
Similar Threads
- Code tags whine-a-thon (DaniWeb Community Feedback)
- Preferences for Code Tags (DaniWeb Community Feedback)
- code tags (DaniWeb Community Feedback)
- code problems (C++)
Other Threads in the C++ Forum
- Previous Thread: Grr Linker problems?
- Next Thread: Need help reading a txt file into C++
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





