有人可以帮我这个代码吗? [英] Can somebody help me for this code?

查看:60
本文介绍了有人可以帮我这个代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请协助我完成这项作业.我不擅长c ++,这件事已经超出了我的脑海.

1)对于此作业,请填写以下课程:

班级人员{
私人:字符串firstName;
字符串lastName;
整型重量
公众:
. . .
};

您应该提供构造函数,访问函数和运算符>> ;.
2)编写一个主要函数,以在输入文件"person.dat"上打开ifstream.如果无法打开流,则输出错误消息并退出.
文件格式如下:last_name逗号first_name逗号权重换行符
3)继续在您的主目录中,打开一个ofstream以用于输出到名为"output.dat"的文件.如果无法打开流,则输出错误消息并退出.
4)在main中继续,编写一个将从ifstream读取的循环,该循环读取足够的信息以创建人员对象(即名,姓和体重).在文件末尾退出循环.如果读取信息时出错,则输出错误消息并退出程序.
5)对于在(4)中阅读的每个人,将他们的体重减少5磅,并使用重载操作符.将每个人对象输出到在(3)在相同格式中创建的ofstream作为原始文件.
6)使用给定的文件person.dat作为输入,例如,但是您应该尝试一些例外情况,以确保您的代码可以检测到并正确处理它们.

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.

推荐答案

出了什么问题?
发布的问题包含分步说明,以完全按照您的要求进行操作.
看一下,并一次读取每个位:
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>>.


您的课程笔记将告诉您该怎么做!
因为这是您的作业,所以我不会给您任何有用的源代码.但是,在C#中,任务将是:


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;
        }
    }



逐步查看它,并尝试解决您的要求.如果您无法使用笔记,书籍,Google和家教来做这些简单的事情,那么请改变路线.现在.在您浪费您(和其他所有人)的时间之前!



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!


#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.


这篇关于有人可以帮我这个代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆