У меня есть класс DollarAmount, который принимает пользовательский ввод как "14.51" и т. Д. c, пока 0.0 не станет типом. Он выполняет такие вещи, как сортировка и поиск суммы всех объектов. Я создал производный класс SpendingRecord, который должен принимать описание после того, как пользователь введет число, например: «14.51 книга истории». Вот где я не уверен, как поступить. Я думал об использовании родительского оператора istream для чтения до первого пробела, а затем каким-то образом переключился на дочерний оператор istream, чтобы прочитать остальные. Все еще не уверен, как закодировать это, и, вероятно, нецелесообразно. (В классе родителей исключены другие функции, кроме istream и ostream, но предполагается, что класс работает).
Любая помощь приветствуется.
заголовок:
class DollarAmount
{
protected:
int dollar = 0;
int cents = 0;
int money = 0; // total money in cents
double avg = 0.00;
int maxsequence = 0; //items in array
public:
DollarAmount(int num = 0); // constructor
double getavg() const { return avg; };
// returns average variable
int getDollar() const { return dollar; };
// returns dollar variable
int getMoney() const { return money; };
// returns money variable (in cents value)
int getCent() const { return cents; };
// returns cents
int getmaxsequence() const { return maxsequence; };
// returns the number of elements in array
void sortobjects(DollarAmount a[], int length);
// precondiiton: Takes in an array and array length
// postcondition: rearranges the array into ascending form
void median(DollarAmount a[], int length);
//precondiiton: Takes in an array and array length
//postcondition: Outputs the median value of the array
friend istream& operator >> (istream& ins, DollarAmount& arg);
// precondiiton: Accepts an object argument between 0.00 and 9999.99
// postcondition: stores each object in the array with three variables money, dollar, and cent
friend ostream& operator << (ostream& out, const DollarAmount& arg);
// precondiiton: Accepts an object argument between 0.00 and 9999.99
// postcondition: Outputs a value
friend DollarAmount operator + (const DollarAmount& arg1, const DollarAmount& arg2);
// precondiiton: Accepts two object arguments between 0.00 and 9999.99 ex: 7.24 + 5.21
// postcondition: Returns temp object with dollar, cent, money variables
friend bool operator > (const DollarAmount& arg1, const DollarAmount& arg2);
// precondiiton: Accepts two object arguments between 0.00 and 9999.99
// postcondition: returns true or false depending on arg1 > arg2
//All of the below functions are from lab 1
void converter1(string array1[], string array2[], string array3[], int x);
//precondiiton: Accepts three arrays of strings and an int value between 0 and 9999
//postcondition: Ouputs the int value in english format ex: 74 -> seventy four
void converter2(int y);
//precondiiton: Accepts an int value
//postcondition: outputs a fraction in the format "y/100"
};
class SpendingRecord : public DollarAmount
{
string description;
public:
SpendingRecord();
SpendingRecord(string s);
string getDes() const { return description; };
friend istream& operator << (istream& ins, const SpendingRecord& arg);
friend ostream& operator << (ostream& out, const SpendingRecord& arg);
};
функции:
SpendingRecord::SpendingRecord()
{
description = "";
}
SpendingRecord::SpendingRecord(string s)
{
description = s;
}
istream& operator << (istream& ins, const SpendingRecord& arg)
{
return ins;
}
ostream& operator << (ostream& out, const SpendingRecord& arg)
{
out << arg.getMoney() / 100.00 << arg.getDescription();
return out;
}
DollarAmount::DollarAmount(int num)
{
money = num;
dollar = num / 100;
cents = num % 100;
}
istream& operator >> (istream& ins, DollarAmount& arg)
{
int num = 0;
string input;
bool wrongInput = false;
do
{
if (wrongInput == true)
cout << "Enter the expenditure record (e.g., $1.95, coffee, enter 0.0 to end):$";
ins >> input;
int index = input.find(".");
input = input.substr(0, index) + input.substr(index + 1, 2);
try
{
num = stoi(input);
}
catch (std::exception const& e)
{
//cout << "error : " << e.what() << endl;
goto label1;
}
if (cin.fail() || num < 0 || num > 999999) //in case the above fails, e.g., ten dollor five cents...
{
label1:
cout << "Wrong input types. Try again:\n";
cin.clear(); //clear the error flags in cin
cin.ignore(2048, '\n'); //ignore everthing in the input buffer, up to 2048 char,
//up to the newline char =< ignore the rest of the line
wrongInput = true;
}
else
wrongInput = false;
} while (wrongInput == true || num < 0 || num > 999999); // if input false ask for input again
arg.dollar = num / 100; //the input converted to cents before is now converted to dollar and cents
arg.cents = num % 100;
arg.money = num;
arg.maxsequence = arg.getmaxsequence() + 1;
return ins;
}
ostream& operator << (ostream& out, const DollarAmount& arg)
{
out << arg.getMoney() / 100.00;
return out;
}