Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,142 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,051 people online right now. Registration is fast and FREE... Join Now!




Classes, Helpless newbie

 
Reply to this topicStart new topic

Classes, Helpless newbie

drumwiz
14 May, 2007 - 11:10 AM
Post #1

New D.I.C Head
*

Joined: 18 Apr, 2007
Posts: 11


My Contributions
Well i'm not going to lie, my hw is due tomarrow, i waited until the day before(today) to look at it, and it is way over my head, and I have no idea how to complete it. Plus the hw is not really written well...just thought I would post it to see if there are any kind souls out there willing help me out, otherwise feel free to flame me wink2.gif

QUOTE
HW: Classes
------------------------------------------------------------------------------------------------------------


1. Create the class student that includes the following information about student: name, age and gpa.

Write the following members functions:
(a) Constructor that creates objects of the student class.
(b ) read that prompts the user to enter from the keyboard student name, age and gpa. To terminate reading the user should enter the EOF (ctrl-D in Unix and Ctrl-Z in DOS).
(c ) show that displays on the screen student name, age and gpa.
(d) writefile that writes a record (name, age, gpa) into the file student.dat.
(e) getgpa that returns gpa of the student.
(f) readfile that reads the student record from the file student.dat .

Write a main program that demonstrates the work with this class. Declare all necessary objects and variables. Using read and writefile create the student.dat. After closing the file student.dat reopen it for reading. Read and display all students from the file and compute and display the average gpa for all of them.

2. Create the class triangle that includes three sides of a triangle.

Write the following members functions:
(a) triangle – constructor that creates objects of that class.
(b ) readfile that reads 3 sides from the file triangle.dat. Read until the EOF is detected.
(c ) show that displays on the screen 3 sides of triangle, perimeter and area.
(d) isequilateral that checks whether the triangle is equilateral (it must be a triangle and all sides must be equal.)

Create the input file triangle.dat using an editor and insert values needed for testing of all functions. Write a main program that demonstrates the work with this class. Declare all objects and variables. Display only equilateral triangles from the input file.

Use the following formulas:
Perimeter = a+b+c
Area = sqrt(s*(s-a)*(s-b )*(s-c)), where s=(a+b+c)/2 is the semi-perimeter.


User is offlineProfile CardPM
+Quote Post

spullen
RE: Classes, Helpless Newbie
14 May, 2007 - 11:23 AM
Post #2

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
Dream Kudos: 50
My Contributions
I don't see anything confusing about the instructions. If you think it through I bet you can figure it out.
And then if you have something, but you can't figure out why it's not working then post it and people will help you then.

This post has been edited by spullen: 14 May, 2007 - 11:25 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Classes, Helpless Newbie
14 May, 2007 - 12:04 PM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
We are indeed willing to help, but as mentioned by spullen, the site has a policy by which we prefer to see a good faith effort before providing source code for academic assignments. If you can post what you've written, we'd be glad to provide some guidance. no flames here! smile.gif
User is offlineProfile CardPM
+Quote Post

drumwiz
RE: Classes, Helpless Newbie
14 May, 2007 - 03:19 PM
Post #4

New D.I.C Head
*

Joined: 18 Apr, 2007
Posts: 11


My Contributions
Hmmm Well I have this for problem 1, but theres still so much work to do that i cant figure out:

CODE
#include <iostream>
#include <iomanip>
using namespace std;

class student
{
    char * name;
    int age1;
public:
    void store(char* , int);
    void show(void);
    int age(void);
};

void student::store(char* s, int a)
{
    name = s;
    age1 = a;
}

void student::show(void)
{
    cout << "\Name: " << setw(15) << name;
    cout << "  Age: " << setw(3) << age1 << '\n';
}

int student::age(void)
{
    return age1;
}

int main()
{ student s1, s2, s3;
s1.store("Adam Anderson", 20);
s2.store("Eve Eisenberg", 18);
s3.store("John Johnston", 19);
s1.show(); s2.show(); s3.show();
cout << "\n\nAverage age = ";
cout << (s1.age()+s2.age()+s3.age())/3.0 << '\n';
return 0;
}

is this enough for a source? hah


User is offlineProfile CardPM
+Quote Post

drumwiz
RE: Classes, Helpless Newbie
14 May, 2007 - 03:39 PM
Post #5

New D.I.C Head
*

Joined: 18 Apr, 2007
Posts: 11


My Contributions
Also here is a program that Reads data from a file of students and finds the best student,so the only changes I need to make is to 1) write into the file(name, age, gpa) instead of just reading as if it already existed. And 2)Compute the average of all the GPA's entered instead of search for the highest. How do i do this! I learn best from direct answers smile.gif

CODE
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#define W setw
using namespace std;

class student
{ private:
char first[20], last[20];
int age;
double gpa;
public:
    student();
    int read(ifstream &);
    void show();
    double GPA();
    int better(double);
};
student::student() {gpa = 0.; )

int student::read(ifstream & IS)
{
    IS >> first >> last >> age >> gpa;
    return !IS.eof();
}

void student::show()
{ cout << '\n'<< W(10) << first << W(4) << W(15) << last;
cout << W(12) << W(3) << age << W(6) << gpa;
}
double student::GPA() { return gpa; }

int student::better(double bestGPA)
{
    return gpa>bestGPA;
}
int main(void)
{
    student s, best;
    ifstream IS = "student.dat"; cout << "\n\nStudent file:";
    while(s.read(IS))
    { s.show();
    if(s.better(best.GPA())) best=s;
    }
    cout << "\n\nThe best student:"; best.show();
    return 0;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:42PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month