overloading operators Error

#include "money.h"
using namespace std;



#include <iostream>


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
{
cout << "$" << dollars << ".";

if (cents < 10)
cout << "0";

cout << cents << endl;
}

int money::dollarsToCents() const
{
return (dollars * 100 + cents);
}

bool money::equalMoney(money otherMoney) const
{
return (dollars == otherMoney.dollars && cents == otherMoney.cents);
}



ostream & operator<<(ostream &out, const money &m)
{
int d, c;
m.getMoney(d,c);

out << "$" << d << ".";

if (c == 0)
out << "00";
else if (c < 10)
out << "0" << c;
else
out << c;

return out;
}

istream& operator>>(istream& is, money& Money)
{
int d,c;
char ch;
is>>d>>ch>>c;
Money.setMoney(d,c);
return is;
}
money operator+(money a, money b)
{
money temp(a.dollars+b.dollars,a.cents+b.cents);
return temp;
}
bool operator==(money a, money b)
{
return (a.dollars==b.dollars && a.cents==b.cents);
}
bool operator!=(money a, money b)
{

return !(a==b);

}

my header file

#include <iostream>
#ifndef MONEY_H
#define MONEY_H

class money
{

public:
money(int = 0, int = 0);
void setMoney(int, int);
void getMoney(int&, int&) 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
int dollars;
int cents;



};



ostream & operator<<(ostream &out, const money &m);
istream & operator>>(std::istream &in, const money &m);
money operator+(money, money);
bool operator==(money, money);
bool operator!=(money, money);
#endif


ok im getting a boat load of errors
1>c:\users\ezaz\desktop\money project\money project\money.h(24): error C2143: syntax error : missing ';' before '&'
1>c:\users\ezaz\desktop\money project\money project\money.h(24): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ezaz\desktop\money project\money project\money.h(24): error C2065: 'out' : undeclared identifier
1>c:\users\ezaz\desktop\money project\money project\money.h(24): error C2059: syntax error : 'const'
1>c:\users\ezaz\desktop\money project\money project\money.h(25): error C2143: syntax error : missing ';' before '&'
1>c:\users\ezaz\desktop\money project\money project\money.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ezaz\desktop\money project\money project\money.h
1>c:\users\ezaz\desktop\money project\money project\money.h


there too many to post ,
but there are no red lines on the code .
I think you need std:: in front of the stream types in your header.

In the future, please use [code][/code] tags around your code.
thank you i just added
using namespace st
Ok i got the errors fixed and it compiles, but when i enter a value it goes to the second line then it crashes. i have no idea maybe you can look at my code and see what im not seeing
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
header file
#include <iostream>
#ifndef MONEY_H
#define MONEY_H
using namespace std;
class money
{      
	
	public:
		money(int = 0, int = 0);
		
		void setMoney(int, int);
		void getMoney(int&, int&) const;
			
		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;

		
};



ostream & operator<<(ostream& cout,  money right);
istream & operator>>(istream& cin,  money& right);
money operator+(money, money);
bool operator==(money, money);
bool operator!=(money, money);

#endif


Cpp file

#include "money.h"
#include <iostream>
#include <cassert>
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;
}




int money::dollarsToCents() const
{
	return (dollars * 100 + cents);
}

bool money::equalMoney(money otherMoney) const
{
	return (dollars == otherMoney.dollars && cents == otherMoney.cents);
}



ostream & operator<<(ostream& cout,  money right)
{
    int d, c;
    right.getMoney(d,c);
   
    cout << "$" << d << ".";
   
    if (c == 0)
        cout << "00";
    else if (c < 10)
        cout << "0" << c;
    else
        cout << c;
       
    return cout;
}

istream& operator>>(istream& cin, money& right)
{
	int d,c;
	char ch;
	cin>>d>>ch>>c;
	right.setMoney(d,c);
	return cin;
}
money operator+(money left, money right)
{
	money result;
	int rd,ld,rc,lc,resultd,resultc;
	left.getMoney(ld,lc);
	right.getMoney(rd,rc);

	resultd= ld+rd;
	resultc=lc+rc;

	result.setMoney(resultd,resultc);
	return result;
	
}
bool operator==(money right, money left)
{
	return left.dollarsToCents()==right.dollarsToCents();
}
bool operator!=(money left, money right)
{

	return left.dollarsToCents()!=right.dollarsToCents();

}


driver

#include<iostream>

#include"money.h"
using namespace std;

int main()
{
	money m1;
	money m2(10);
	int dollar,cent;
	
	cout<<"m1 created with default constructor:\t"<<m1<<endl;
	cout<<"m2 created with m2(10):\t"<<m2<<endl;
	cout<<"Now enter new value for m1: ";
	cin>>m1;

	m1.getMoney(dollar,cent);
	cout<<"Now, m1 has "<<dollar<<" dollars and "<<cent<<" cents"<<endl;

	cout<<"Enter amount of dollars for m2: ";
	cin>>dollar;
	cout<<"Enter amount of cents for m2: ";
	cin>>cent;
	m2.setMoney(dollar,cent);

	cout<<"m1 + m2 = "<<(m1+m2)<<endl;
	cout<<"m1 == m2: "<<(m1==m2)<<endl;
	cout<<"m1 != m2: "<<(m1!=m2)<<endl;

	system("pause");
	return 0;
}
when i enter a value it goes to the second line then it crashes

What do you mean by "the second line"? The second line of code? The second line of input? The second line of output?

Can you be more specific about what behaviour you're seeing?
i ment after i put in a dollar amount it doesnt output the second question , as soon as i press enter it doesnt ask the second question it just freezes for 5 seconds then it goes away and my compiler crashes
Topic archived. No new replies allowed.