473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

I have another C++ mess on my hands

I can not get this program to work.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. const int SIZE = 80;
  9.  
  10.  
  11.  
  12. class Student
  13. {
  14. protected:
  15.  
  16.     int social;
  17.     string name;
  18.     char *NamePtr;
  19. public:
  20.     Student (int = 999999999 , char [] = "unassigned");
  21.     void setSSN(long);
  22.     long getSSN();
  23.     void setName(char[]);
  24.     char* getName();
  25.     virtual double calcGPA();
  26. };
  27.  
  28. Student::Student(int ssn, string studentname)
  29. {
  30.     social = ssn;
  31.     strcpy (name, studentname);
  32. }
  33.  
  34. void Student::setSSN(int SSN)
  35. {
  36.             social = SSN;
  37.         return;
  38. }
  39.  
  40. long Student::getSSN()
  41. {
  42.     return (social);
  43. }
  44.  
  45. void Student::setName(char name2)
  46. {
  47.         strcpy (name, name2);
  48.         return;
  49. }
  50.  
  51. char* Student::getName()
  52. {    
  53.     NamePtr = name;
  54.     return (NamePtr);
  55. }
  56.  
  57. double Student::calcGPA()
  58. {
  59.     cout << "Student calcGPA" << endl;
  60.     return (0);
  61. }
  62.  
  63. class GradStudent : public Student 
  64. {
  65. private:
  66.     char status;
  67. public:
  68.     GradStudent (char = 'A');
  69.     virtual double calcGPA();
  70.     void SetStatus(char);
  71. };
  72.  
  73. GradStudent::GradStudent(char grade)
  74. {
  75.     status = grade;
  76. }
  77.  
  78. double GradStudent::calcGPA()
  79. {
  80.     double GPA = 0.0;
  81.     if (status == 'A' || status == 'a')
  82.         GPA = 4.0;
  83.     cout << "GradStudent calcGPA";
  84.     return (GPA);
  85. }
  86.  
  87. void GradStudent::SetStatus (char stat)
  88. {
  89.     status = stat;
  90.     return;
  91. }
  92.  
  93. class UnderGradStudent : public Student
  94. {
  95. private:
  96.     double CreditHrs;
  97.     double QualityPts;
  98. public:
  99.     UnderGradStudent ( double = 0, double = 0);
  100.     virtual double calcGPA ();
  101.     void setCredits(int);
  102.     void setQuality(int);
  103. };
  104.  
  105. UnderGradStudent::UnderGradStudent(double Hours, double Points)
  106. {
  107.     CreditHrs = Hours;
  108.     QualityPts = Points;
  109.     if (Hours < 0)
  110.         CreditHrs = 0;
  111.     if (Points < 0)
  112.         QualityPts = 0;
  113. }
  114.  
  115. double UnderGradStudent::calcGPA ()
  116. {
  117.     double GPA = 0.0;
  118.     if (CreditHrs > 0)
  119.         GPA = QualityPts / CreditHrs;
  120.     cout << "UnderGraduateStudent calcGPA";
  121.     return (GPA);
  122. }
  123.  
  124. void UnderGradStudent::setCredits (int C)
  125. {
  126.     CreditHrs = C;
  127.     if (C < 0)
  128.         CreditHrs = 0;
  129.     return;
  130. }
  131.  
  132. void UnderGradStudent::setQuality (int Q)
  133. {
  134.     QualityPts = Q;
  135.     if (Q < 0)
  136.         QualityPts = 0;
  137.     return;
  138. }
  139.  
  140.  
  141. int main()
  142. {
  143.     char ans = 'n';
  144.     char grade;
  145.     string StudentName;
  146.     double StudentSSN;
  147.     int choice, points, credits, continue;
  148.  
  149.  
  150.     Student* a = NULL;
  151.  
  152.     do
  153.     {
  154.         cout << "Enter 1 for undergrad.\nEnter 2 for grad" << endl << "-->";
  155.         cin >> choice;
  156.  
  157.         if (choice == 1)
  158.         {
  159.             a = new UnderGradStudent;
  160.  
  161.             cin.ignore();
  162.             cout << "Please enter student name: ";
  163.             cin.getline(StudentName, SIZE);
  164.             a->setName(StudentName);        
  165.         }
  166.  
  167.         else if (choice == 2)
  168.         {
  169.             a = new GradStudent;
  170.  
  171.             cin.ignore();
  172.             cout << "Please enter student name: ";
  173.             cin.getline(StudentName, SIZE);
  174.             a->setName(StudentName);
  175.  
  176.  
  177.         }
  178.  
  179.  
  180.         else continue;
  181.  
  182.  
  183.         cout <<  "\nName: " << a->getName() << " SSN: " << a->getSSN() << " GPA: "
  184.             << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << a->calcGPA() << endl;
  185.  
  186.  
  187.         delete a;
  188.         a = NULL;
  189.  
  190.         cout << "Continue?: ";
  191.         cin >> continue;
  192.         cin.ignore();
  193.         cout << endl;
  194.  
  195.     }while (toupper(ans) == 'Y');
  196.  
  197.  
  198.     cout << "Press ENTER to quit.";
  199.     cin.ignore( 1000, '\n' );
  200.  
  201.  
  202.     return 0;
  203. }
Oct 6 '15 #1

✓ answered by weaknessforcats

There are lots of errors here. Foremost is that some of your member functions have different arguments from what is in the class declaration.

etc..

I suggest you create a new source file ana have only your class definition and a main:

Expand|Select|Wrap|Line Numbers
  1. class Student
  2. {
  3.  
  4. };
  5.  
  6. int main()
  7. {
  8.    Student s;
  9. }
Verify this compiles.

Now add member data and a constructor.

Verify this compiles.
Verify the program works.

Now add another member function.

Add code in main() to test it.
Verify this compiles.
Verify the program still works.


By working gradually you guarantee the program compiles and runs correctly so any errors would be in the last bit of code you added. Just work through a step at a time.

I see you are using things like strcpy in a C++ program. Bad news. YOu use the assignment operator on string objects.

Post again and let me know how you are coming along.

3 1399
weaknessforcats
9,208 Expert Mod 8TB
There are lots of errors here. Foremost is that some of your member functions have different arguments from what is in the class declaration.

etc..

I suggest you create a new source file ana have only your class definition and a main:

Expand|Select|Wrap|Line Numbers
  1. class Student
  2. {
  3.  
  4. };
  5.  
  6. int main()
  7. {
  8.    Student s;
  9. }
Verify this compiles.

Now add member data and a constructor.

Verify this compiles.
Verify the program works.

Now add another member function.

Add code in main() to test it.
Verify this compiles.
Verify the program still works.


By working gradually you guarantee the program compiles and runs correctly so any errors would be in the last bit of code you added. Just work through a step at a time.

I see you are using things like strcpy in a C++ program. Bad news. YOu use the assignment operator on string objects.

Post again and let me know how you are coming along.
Oct 6 '15 #2
Some of the error messages from the compiler does not make sense, well for me they do not, being new to C++.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class Student
  7. {
  8. protected:
  9.  
  10.     int social;
  11.     string name;
  12.     string *NamePtr;
  13. public:
  14.     Student (); // Default constructor
  15.     Student(int ssn, string StudentName);
  16.     void setSSN(int);
  17.     int getSSN();
  18.     void setName(string);
  19.     string* getName();
  20.     virtual double calcGPA();
  21. };
  22.  
  23. Student ::Student()
  24. {
  25. }
  26. Student::Student(int ssn, string  StudentName)
  27. {
  28.  
  29.     social = ssn;
  30.     strcpy (name, Studentname);
  31.  
  32.     return;
  33. }
  34.  
  35. void Student::setSSN(int SSN)
  36. {
  37.         social = SSN;
  38.         return;
  39. }
  40.  
  41. int Student::getSSN()
  42. {
  43.     return (social);
  44. }
  45.  
  46. void Student::setName(string name2)
  47. {
  48.         strcpy (name, name2);
  49.         return;
  50. }
  51.  
  52. string* Student::getName()
  53. {    
  54.     NamePtr = name;
  55.     return (NamePtr);
  56. }
  57.  
  58. double Student::calcGPA()
  59. {
  60.     cout << "Student calcGPA" << endl;
  61.     return (0);
  62. }
  63.  
  64. class GradStudent : public Student 
  65. {
  66. private:
  67.     char Status;
  68. public:
  69.     GradStudent (char = 'A');
  70.     virtual double calcGPA();
  71.     void SetStatus(char);
  72. };
  73.  
  74. GradStudent::GradStudent(char grade)
  75. {
  76.     Status = grade;
  77. }
  78.  
  79. double GradStudent::calcGPA()
  80. {
  81.     double GPA = 0.0;
  82.     if (Status == 'A' || Status == 'a')
  83.         GPA = 4.0;
  84.     cout << "GradStudent calcGPA";
  85.     return (GPA);
  86. }
  87.  
  88. void GradStudent::SetStatus (char stat)
  89. {
  90.     Status = stat;
  91.     return;
  92. }
  93.  
  94. class UnderGradStudent : public Student
  95. {
  96. private:
  97.     double CreditHrs;
  98.     double QualityPts;
  99. public:
  100.     UnderGradStudent ( double = 0, double = 0);
  101.     virtual double calcGPA ();
  102.     void setCreditHrs(int);
  103.     void setQuality(int);
  104. };
  105.  
  106. UnderGradStudent::UnderGradStudent(double Hours, double Points)
  107. {
  108.     CreditHrs = Hours;
  109.     QualityPts = Points;
  110.     if (Hours < 0)
  111.         CreditHrs = 0;
  112.     if (Points < 0)
  113.         QualityPts = 0;
  114. }
  115.  
  116. double UnderGradStudent::calcGPA ()
  117. {
  118.     double GPA = 0.0;
  119.     if (CreditHrs > 0)
  120.         GPA = QualityPts / CreditHrs;
  121.     cout << "UnderGraduateStudent calcGPA";
  122.     return (GPA);
  123. }
  124.  
  125. void UnderGradStudent::setCreditHrs (int C)
  126. {
  127.     CreditHrs = C;
  128.     if (C < 0)
  129.         CreditHrs = 0;
  130.     return;
  131. }
  132.  
  133. void UnderGradStudent::setQuality (int Q)
  134. {
  135.     QualityPts = Q;
  136.     if (Q < 0)
  137.         QualityPts = 0;
  138.     return;
  139. }
  140.  
  141.  
  142. int main()
  143. {
  144.     char ans = 'n', grade, Status;
  145.     string StudentName;
  146.     int StudentSSN;
  147.     int code, CreditHrs, points;
  148.  
  149.  
  150.     Student* a = NULL;
  151.  
  152.     do
  153.     {
  154.         cout << "Enter 1 for undergrad.\nEnter 2 for grad." << endl << "-->";
  155.         cin >> code;
  156.  
  157.         if (code == 1)
  158.         {
  159.             a = new UnderGradStudent;
  160.  
  161.             cin.ignore();
  162.             cout << "Enter Student name: ";
  163.             cin.getline(StudentName);
  164.             a->setName (StudentName);
  165.             cout << "Enter Student SSN: ";
  166.             cin >> StudentSSN;
  167.             a->getSSN();
  168.  
  169.             cout << "Enter credit hours: ";
  170.             cin >> CreditHrs;
  171.             a->setCreditHrs(CreditHrs);
  172.  
  173.             cout << "Enter quality points: ";
  174.             cin >> points;
  175.             a->setQuality(points);
  176.             cout << endl;
  177.  
  178.  
  179.         }
  180.  
  181.         else if (code == 2)
  182.         {
  183.             a = new GradStudent;
  184.  
  185.             cin.ignore();
  186.             cout << "Enter Student name: ";
  187.             a->getName(StudentName);
  188.  
  189.  
  190.             cout << "Enter Student SSN: ";
  191.             cin >> StudentSSN;
  192.             a->setSSN(StudentSSN);
  193.  
  194.             cout << "Enter Status: ";
  195.             cin >> Status;
  196.             a->SetStatus(Status);
  197.             cout << endl;
  198.         }
  199.  
  200.  
  201.  
  202.  
  203.  
  204.         cout <<  "\nName: " << a->getName() << " SSN: " << a->getSSN() << " GPA: "
  205.             << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << a->calcGPA() << endl;
  206.  
  207.  
  208.         delete a;
  209.         a = NULL;
  210.  
  211.         cout << "Continue?: ";
  212.         cin >> ans;
  213.         cin.ignore();
  214.         cout << endl;
  215.     }
  216.     while (toupper(ans) == 'Y');
  217.  
  218.  
  219.  
  220.     return 0;
  221. }
Oct 9 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. Student::Student(int ssn, string  StudentName)
  2. {
  3.  
  4.     social = ssn;
  5.     //strcpy(name, Studentname);
  6.     name = StudentName;
  7.  
  8.     return;
  9. }
Please stop using strcpy. This is C++ and not C. strcpy only works with char* strings with a \0 terminator.

A C++ string object is not a C-string.

Also:
Expand|Select|Wrap|Line Numbers
  1. string* Student::getName()
  2. {
  3.     NamePtr = name;
  4.     return (NamePtr);
  5. }
  6.  
NamePtr is a string* and name is a string. You can't assign an obect to a pointer.

NamePtr is a hacker's delight. It exposes the private data member member and allows you to change it without using a member function. Remove this from your design. You don't need a string*.

The code should be:
Expand|Select|Wrap|Line Numbers
  1. string Student::getName()
  2. {
  3.     ///NamePtr = name;
  4.     //return (NamePtr);
  5.     return name;
  6. }
But here is your real problem:
Expand|Select|Wrap|Line Numbers
  1. a = new UnderGradStudent;
  2. ......
  3. ....
  4. cin >> points;
  5. a->setQuality(points);
  6.  
setQuality is a method of UnderGradStudent whereas your variable "a" is a Student. You can only call base class methods from a base class pointer.

Only you know that a points to an UnderGradStudent object. The compiler has no idea.

For this to work you need a setQuality method in Student:

Expand|Select|Wrap|Line Numbers
  1. class Student
  2. {
  3.   public:
  4.    virtual void setQuality(int) =0;
  5. };
This code will allow a->setQuality to work and the virtual will cause a call to UnderGradStudent::setQuality.

You might read this: http://bytes.com/topic/c/insights/79...polymorphism-c

Fix your code and post again. And please stop using strcpy or any other C functions from 1968.
Oct 9 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: Quest Master | last post by:
I am interested in developing an application where the user has an ample amount of power to customize the application to their needs, and I feel this would best be accomplished if a scripting...
51
by: nospam | last post by:
THIS IS the DOTNETJUNKIES MESSAGE ------------------------- We're Sorry As many of you know we have recently launched SqlJunkies.com. We have overhauled our runtime and will be using it on...
0
by: Will Honea | last post by:
I've been handed a mess of related data in the form of multiple spead sheets. Each set of data contains part or parts of the what is needed in the final database but there is no common usable...
2
by: M. Simioni | last post by:
Hi, the webserver is a HP ProLiant DL380 G3 1GB RAM Dual U320 SCSI COMPAQ Smart Array 5i + Battery Pack installed N°2 (RAID 1) COMPAQ BD072863B2 - 72.8GB Ultra320, 10K Hot-pluggable, 1"...
0
by: mack | last post by:
what do you do if a superviser never washes his hands an trys to touch you what the hell do i do an hes first at the dougnuts an i never have time to go get none an their krispy kremes WHY ME ?????
19
by: Mockey Chen | last post by:
I using #define as following: #include <stdio.h> #define LOG_PREFIX "Current function: <" __FUNCTION__ ">: " int main() { printf(LOG_PREFIX "some thing.\n"); return 0; }
27
by: Simon Biber | last post by:
I was reading http://en.wikipedia.org/wiki/Poker_probability which has a good description of how to count the frequency of different types of poker hands using a mathematical approach. A sample...
0
by: | last post by:
I'm come from a self-taught ASP/Dreamweaver background and over the last year have used VS.NET/ASP.NET/C# more and more to the point that it is my primary development environment now. VS.NET is an...
13
by: kinghippo423 | last post by:
Hello Everyone, I did a poker program in Java that essencially finds the strenght of a poker hand created Randomly. My program is doing OK...but I'm pretty sure it can be optimised. This is my...
3
by: robert | last post by:
Often I want to extract some web table contents. Formats are mostly static, simple text & numbers in it, other tags to be stripped off. So a simple & fast approach would be ok. What of the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.