trouble working with a class
Dec 1, 2013 at 7:02pm Dec 1, 2013 at 7:02pm UTC
Need a little point in the right direction working with classes with separate header / driver / initialization files
currently receiving these error messages
1>c:\users\michael\documents\proj6\money.cpp(51): error C2059: syntax error : '.'
1>c:\users\michael\documents\proj6\money.cpp(53): error C2181: illegal else without matching if
on code
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
#include <iostream>
#include "money.h"
#include <iomanip>
using namespace std;
money::money(int d , int c)
{
dollars = d;
cents = c;
}
void money::setMoney(int d, int c)
{
dollars = d;
cents = c;
}
void money::getMoney(int & d, int & c) const
{
d = dollars;
c = cents;
}
void money::print() const
// in the form $1.05
{
if (cents < 10)
cout << "$" << dollars << ".0" << cents << endl;
else
cout << "$" << dollars << "." << cents << endl;
}
int money::dollarsToCents() const
// converts $1.05 to 105 cents
{
int centsconvert;
centsconvert = (dollars + (cents / 100)) * 100;
cout << "$" << dollars << "and " << cents << "cents is " <<
cout << centsconvert << "cents" << endl;
return centsconvert;
}
bool money::equalMoney(money otherMoney) const
{
if (money.cents == otherMoney.cents && money.dollars == otherMoney.dollars)
true ;
else
false ;
}
my driver program looks like
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
#include <iostream>
#include "money.h"
#include <iomanip>
using namespace std;
int main()
{
money M1, M3;
money M2(7,75);
int d, c, dollars, cents;
M1.setMoney(5,5);
cout << "enter the dollar value for M3" << endl;
cin >> dollars;
cout << "enter the cent value for M3" << endl;
cin >> cents;
M3.setMoney(dollars,cents);
cout << "money 1 is " ;
M1.print();
cout << endl;
cout << "money 2 is " ;
M2.print();
cout << endl;
cout << "money 3 is " ;
M3.print();
cout << endl;
if (M2.equalMoney(M3))
cout << "Money2 and Money3 are the same value" << endl;
else
cout << "Money2 and Money3 are different values" << endl;
system("pause" );
return 0;
}
and this is my header file with class included
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class money
{
public :
money(int d = 0, int c = 0);
void setMoney(int d, int c);
void getMoney(int & d, int & c) const ;
void print() const ; // in the form $1.05
int dollarsToCents() const ; // converts $1.05 to 105 cents
bool equalMoney(money otherMoney) const ; // checks if two money objects are equal
private :
int dollars;
int cents;
};
any other tips you may offer other then the error messages are also greatly appreciated, thanks
Dec 1, 2013 at 7:20pm Dec 1, 2013 at 7:20pm UTC
49 50 51 52 53 54 55
bool money::equalMoney(money otherMoney) const
{
if (money.cents == otherMoney.cents && money.dollars == otherMoney.dollars)
true ;
else
false ;
}
money.cents and
money.dollars should be just
cents and
dollars , respectively.
Also, you forgot to put
return
before your
true
and
false
.
300!
Topic archived. No new replies allowed.