学校で完了する必要のある運動があり、混乱しています。私には意味がないので、これを実行するための正しい方向へのヒントが必要です。
Employeeクラスにメンバー関数を追加します。
void Employee::format(char buffer[], int buffer_maxlength)
メンバー関数は、従業員の名前と給与でバッファーを埋める必要があります。バッファをオーバーランしないように注意してください。'\ 0'ターミネータをカウントせずに、buffer_maxlength文字を保持できます。
私が得られないのは、関数に渡されるパラメーターが何であるかです。名前を渡してからバッファにフィードするべきではありませんか?または、関数がパラメーターを受け取ってバッファーにデータを入力するべきではありませんか?バッファがパラメータの場合、どのように設定しますか?
ここで混乱しています。
演習がまだわからないため、コーディングを開始していません。
私のためにプログラムを書くのに誰かを必要としないでください、ただここで何が起こっているかについてのヒントが必要です。
ありがとう。
編集:これは私がこれまでに持っているコードです、それはうまくいくようです。よくわからないのは、バッファオーバーランです。バッファのサイズを変更できないので、既存のデータでオーバーランできないことがわかっているサイズにする必要がありますか?これは非効率に思えますが、他に何をすべきかわかりません。
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string.h>
#pragma warning(disable : 4996) // had to include this for the strcpy function, not sure why
using namespace std;
/**
A basic employee class that is used in many examples
in the book "Computing Concepts with C++ Essentials"
*/
class Employee
{
public:
/**
Constructs an employee with empty name and no salary.
*/
Employee();
/**
Constructs an employee with a given name and salary.
@param employee_name the employee name
@param initial_salary the initial salary
*/
Employee(string employee_name, double initial_salary);
/**
Sets the salary of this employee.
@param new_salary the new salary value
*/
void set_salary(double new_salary);
/**
Gets the salary of this employee.
@return the current salary
*/
double get_salary() const;
/**
Gets the name of this employee.
@return the employee name
*/
string get_name() const;
void format(char buffer[], int buffer_maxlength);
private:
string name;
double salary;
char buffer;
};
Employee::Employee()
{
salary = 0;
}
Employee::Employee(string employee_name, double initial_salary)
{
name = employee_name;
salary = initial_salary;
}
void Employee::set_salary(double new_salary)
{
salary = new_salary;
}
double Employee::get_salary() const
{
return salary;
}
string Employee::get_name() const
{
return name;
}
void Employee::format(char buffer[], int buffer_maxlength)
{
string temp_name;
//string space = " ";
char terminator = '\0';
double input_salary = salary;
string s;
stringstream output_salary;
output_salary << input_salary;
s = output_salary.str();
temp_name = name.c_str() + s + terminator;
strcpy(buffer, temp_name.c_str());
cout << buffer << endl;
}
int main()
{
const int BUFFER_SIZE = 100;
char input_buffer[BUFFER_SIZE];
string temp_string;
string space = " ";
Employee bob_buffer("Buffer, Bob", 100000);
bob_buffer.format(input_buffer, BUFFER_SIZE);
system("pause");
return 0;
}
編集:オーバーランから保護するためにstrcpyの代わりにstrncpyを使用