Click here to Skip to main content
16,017,151 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
Please help me with this assignment. I am not good at c++ and this thing has gone past over my head.

1) For this assignment, fill out the following class:

class person {
private: string firstName;
string lastName;
int weight;
public:
. . .
};

You should provide constructors, access functions, and the operator>>.
2) Write a main function that opens an ifstream on the input file, "person.dat". If the stream cannot be opened, output an error message and exit.
The file format is as follows: last_name comma first_name comma weight newline
3) Continuing in your main, open an ofstream to use for output to a file named "output.dat". If the stream cannot be opened, output an error message and exit.
4) Continuing in main, write a loop that will read from the ifstream that reads enough information to create a person object (i.e. first name, last name, and weight). Exit the loop on end-of-file. If there is an error reading the information, output an error message and exit the program.
5) For each person read in (4), decrement their weight by 5 pounds and use the overloaded operator>> to output each person object to the ofstream created in (3) IN THE SAME FORMAT as the original file.
6) Use the given file, person.dat, for example input, but you should try some exceptional cases to make sure your code can detect and properly handle them.
Posted
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 2:48am    
Hm. School assignments...
What king of help can you expect here? Who would be interested in writing such boring code for you?
If you need help, make your own code and ask questions if you face any problems.
--SA
Sandeep Mewara 12-Apr-11 8:43am    
No efort.

What is the problem?
The question as posted contains the step-by-step instructions to do exactly what you need.
Look at it, and read each bit at a time:
1) For this assignment, fill out the following class: 
class person { 
   private: 
      string firstName; 
      string lastName; 
      int weight; 
   public: 
      . . . 
};
You should provide constructors, access functions, and the operator>>.

Your course notes will tell you the mechanics of what to do!
Because this is your homework, I won't give you any useful source code. But, in C# the task would be:
public class person
    {
    private string firstName;
    private string lastName;
    public string FirstName
        {
        get { return firstName; }
        set { firstName = value; }
        }
    public string LastName
        {
        get { return lastName; }
        set { lastName = value; }
        }
    public person()
        {
        firstName = "unknown";
        lastName = "unknown";
        }
    public person(string first, string last)
        {
        firstName = first;
        lastName = last;
        }
    public override string ToString()
        {
        return firstName + " " + lastName;
        }
    }


Look at it stage by stage, and try to work out what is being asked of you. If you can't do even this simple stuff with your notes, books, Google and tutor available, then change course. Now. Before you waste any more of your (and everybody else's) time!
 
Share this answer
 
Comments
Himansu sekhar bal 12-Apr-11 3:10am    
this is correct answer,thanks
C++
#include <iostream>
#include <fstream>
using namespace std;
 class person { 
private: string firstName; 
string lastName; 
int weight; 
public: 
. . . 
};
 


int main()
{

string line;
  ifstream myfile ("person.dat");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }
  else cout << "Unable to open file"; //this is for data input.
}
 ofstream myfile ("person.dat");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";//this is code for of stream.
}

here you need to declare an object for your class. And arrange yourself in your code this will help you.
 
Share this answer
 
v3
Comments
OriginalGriff 12-Apr-11 3:28am    
If you post code fragments, please enclose them in a code block either using the widget or "pre" tags - this preserves the formatting and makes it easier to read.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900