keep getting these 4 errors.. leaning towards the simplify function
Dec 10, 2013 at 11:37pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
//.cppfile-
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "DollarsCents.h"
using namespace std;
DollarsCents::DollarsCents(int numDollars, int numCents)
{
dollars = numDollars;
cents = numCents;
while (cents < 0)
{
cents += dollars*100 + cents;
dollars--;
}
simplify();
}
istream& operator >> (istream& in, DollarsCents& dcRef)
{
char dot;
in >> money.dollars >> dot >> money.cents;
return in;
}
ostream& operator << (ostream& out, const DollarsCents& dcRef)
{
out << "$" << money.dollars << "." << std::setfill('0' ) << std::setw(2) << money.cents;
return out;
}
DollarsCents DollarsCents::operator + (const DollarsCents& dcRef)
{
DollarsCents dc_add;
dc_add.dollars = dollars + money.dollars;
dc_add.cents = cents + money.cents;
dc_add.simplify();
return dc_add;
}
DollarsCents DollarsCents::operator - (const DollarsCents& dcRef)
{
DollarsCents dc_subtract;
if (cents > money.cents)
{
dc_subtract.dollars = (dollars - 1) - money.dollars;
dc_subtract.cents = (cents + 100) - money.cents;
}
else
{
dc_subtract.dollars = dollars - money.dollars;
dc_subtract.cents = cents - money.cents;
}
dc_subtract.simplify();
return dc_subtract;
}
DollarsCents DollarsCents::operator /(const int num)
{
int totalCents = (dollars*100) + cents;
totalCents /= num;
DollarsCents dc_divide(0, totalCents);
dc_divide.simplify();
return dc_divide;
}
DollarsCents DollarsCents::operator *(const int num)
{
int totalCents = (dollars*100) + cents;
totalCents *= num;
DollarsCents dc_multiply(0, totalCents);
dc_multiply.simplify();
return dc_multiply;
}
DollarsCents& DollarsCents::operator += (const DollarsCents& dcRef)
{
dollars += money.dollars;
cents += money.cents;
simplify();
return *this ;
}
DollarsCents& DollarsCents::operator -= (const DollarsCents& dcRef)
{
if (money.cents > cents)
{
dollars -= (dollars - 1) - money.dollars;
cents -= (cents + 100) - money.cents;
}
else
{
dollars -= dollars - money.dollars;
cents -= cents - money.cents;
}
simplify();
return *this ;
}
DollarsCents& DollarsCents::operator /=(const int num)
{
cents += dollars*100;
dollars = 0;
cents /= num;
simplify();
return *this ;
}
DollarsCents& DollarsCents::operator *=(const int num)
{
cents += dollars*100;
dollars = 0;
cents *= num;
simplify();
}
Last edited on Dec 10, 2013 at 11:39pm UTC
Dec 10, 2013 at 11:38pm UTC
//errors-
4 0 .cpp In file included from DollarsCents.cpp
23 2 .h [Error] expected unqualified-id before '{' token
138 1 .cpp [Error] expected '}' at end of input
4 0 .cpp In file included from DollarsCents.cpp
16 28 .h [Error] expected unqualified-id at end of input
[/output]
Last edited on Dec 10, 2013 at 11:41pm UTC
Dec 10, 2013 at 11:42pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
.h file
#include <iostream>
#include <iomanip>
#include <cstdlib>
#define DollarsCents_H
class DollarsCents
{
public :
DollarsCents();
DollarsCents(int =0, int =0);
int getDollars();
int getCents();
void simplify();
{
while (cents >= 100)
{
cents-=100;
dollars++;
}
}
DollarsCents operator + (const DollarsCents& dcRef );
DollarsCents operator - (const DollarsCents& dcRef);
DollarsCents operator / (const int num);
DollarsCents operator * (const int num);
DollarsCents& operator += (const DollarsCents& dcRef);
DollarsCents& operator -= (const DollarsCents& dcRef);
DollarsCents& operator /= (const int num);
DollarsCents& operator *= (const int num);
bool operator < (const DollarsCents& dcRef);
bool operator <= (const DollarsCents& dcRef);
bool operator > (const DollarsCents& dcRef);
bool operator >= (const DollarsCents& dcRef);
bool operator == (const DollarsCents& dcRef);
bool operator != (const DollarsCents& dcRef);
friend std::istream& operator >>(std::istream &in, const DollarsCents& dcRef);
friend std::ostream& operator <<(std::ostream &out, const DollarsCents& dcRef);
private :
int dollars;
int cents;
};
Dec 11, 2013 at 7:58am UTC
Fix these lines DollarsCents(int =0, int =0);
void simplify();
Topic archived. No new replies allowed.