String stream in money class

Hi guys,

Need a little help again.

I built a money class, tested all the functions, and everything works as it should. However, I don't have a full understand of stringstream, which I strongly suspect is causing issues when printing money objects. Stringstream echoes the money object to the screen, but as a double. I would like to get rid of this "echo" effect, can anyone help please?

It's also my understanding I have to use Stringstream in order to convert double to string then manipulate.

Thanks,

Mike

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
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <sstream>
using namespace std;

class Money
{
public:
 Money(){}
 Money(string s):amount(s){}
 Money (double d)
 {
  stringstream ss;    
  ss.setf(ios::fixed);
  ss.setf(ios::showpoint);
  ss.precision(2);
  ss<<d;
  ss>>amount;
  int location=amount.find_first_of('.');
  int distance;
  for(int i=0;i<amount.length();i++)
  {
   if(i==0)
    putchar('$');
   putchar(amount[i]);
   distance=location-i-1;//from end of array
   if(distance>0 && distance%3==0)
    putchar(',');         
  } 
 }
 
friend istream& operator>>(istream& in, Money& m);
friend ostream& operator<<(ostream& out, Money& m);

Money operator+(Money& m){
 return Money(double(Money(amount))+double(m));      
}
Money operator-(Money& m){
 return Money(double(Money(amount))-double(m));      
}
Money operator/(Money& m){
 return Money(double(Money(amount))/double(m));      
}
Money operator/(double d){
 return Money(double(Money(amount))/d);      
}
Money operator*(double d){
 return Money(double(Money(amount))*d);      
}

operator double()
{
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(2);   
  int i, j, count=0;
  char sol[36];
  for(i=0,j=0;i<amount.length();i++)
  {
   if(isdigit(amount[i]) || amount[i]=='.')     
    sol[j++]=amount[i]; 
   if(amount[i]=='.' || amount[i]==',') 
    count++;
  }
  sol[amount.length()-count+1]='\0';        
  return (atof(sol));       
}       
private:      
 string amount;     
};

int main()
{
 char choice;
 Money m1("$23,435.23"), m2("$5,245.11");
 do{
  //cin>>m1;
  //cin>>m2;
  //cout<<endl;
  cout<<"First amount is: "<<m1;
  cout<<"Second amount is: "<<m2<<endl<<endl;    
  double scalar=3.14;
  double one=m1;
  double two=m2;
  
  cout<<"Addition: ";
  Money addition=m1+m2;
  cout<<addition<<endl;//problme here with printing solution
  
  cout<<"Subtraction: ";
  Money subtraction=m1-m2;
  cout<<subtraction<<endl;   //problme here with printing solution
  
  cout<<"Division: ";
  Money division=m1/m2;
  cout<<division<<endl; //problme here with printing solution
  
  cout<<"Amount one multiplied by "<<scalar<<" is: ";
  Money mult_scalar=m1*scalar;
  cout<<mult_scalar<<endl;//problme here with printing solution
      
  cout<<"Amount one divided by "<<scalar<<" is: ";
  Money div_scalar=m1/scalar;
  cout<<div_scalar<<endl;  //problme here with printing solution
  
  double divi=m1/m2;
  cout<<"\nDouble m1/m2 is: "<<divi<<endl;
   
  double multi=m1*m2;
  cout<<"Double m1/m2 is: "<<multi<<endl;
   
      
  cout<<"Again (y/n): ";
  cin>>choice;    
 }while(choice=='y' || choice=='Y');
  cout<<"Have a nice day: ";
  exit(1);
  _getch();
  return 0;
}
ostream& operator<<(ostream& out, Money& m){
 out<<m.amount<<endl;        
 return out;         
}
istream& operator>>(istream& in, Money& m){
 cout<<"Enter an amount of money: ";
 getline(in,m.amount);        
 return in;        
}


You're doing things wrong...totally! Anyway, the evil function is
Money (double d) ... whenever you try to convert that double into string it will ALSO! print that number ... and then again you print it with cout that's why it echoes.

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
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <sstream>
using namespace std;

class Money
{
public:
 Money(){}
 Money(string s):amount(s){}
 Money (double d)
 {
  stringstream ss;    
  ss.setf(ios::fixed);
  ss.setf(ios::showpoint);
  ss.precision(2);
  ss<<d;
  ss>>amount;
  int location=amount.find_first_of('.');
  int distance;
  for(int i=0;i<amount.length();i++)
  {
   if(i==0)
    putchar('$');
   putchar(amount[i]);
   distance=location-i-1;//from end of array
   if(distance>0 && distance%3==0)
    putchar(',');         
  } 
 }
 
friend istream& operator>>(istream& in, Money& m);
friend ostream& operator<<(ostream& out, Money& m);

Money operator+(Money& m){
 return Money(double(Money(amount))+double(m));      
}
Money operator-(Money& m){
 return Money(double(Money(amount))-double(m));      
}
Money operator/(Money& m){
 return Money(double(Money(amount))/double(m));      
}
Money operator/(double d){
 return Money(double(Money(amount))/d);      
}
Money operator*(double d){
 return Money(double(Money(amount))*d);      
}

operator double()
{
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(2);   
  int i, j, count=0;
  char sol[36];
  for(i=0,j=0;i<amount.length();i++)
  {
   if(isdigit(amount[i]) || amount[i]=='.')     
    sol[j++]=amount[i]; 
   if(amount[i]=='.' || amount[i]==',') 
    count++;
  }
  sol[amount.length()-count+1]='\0';        
  return (atof(sol));       
}       
private:      
 string amount;     
};

int main()
{
 char choice;
 Money m1("$23,435.23"), m2("$5,245.11");
 do{
  //cin>>m1;
  //cin>>m2;
  //cout<<endl;
  cout<<"First amount is: "<<m1;
  cout<<"Second amount is: "<<m2<<endl<<endl;    
  double scalar=3.14;
  double one=m1;
  double two=m2;
  
  cout<<"Addition: ";
  Money addition=m1+m2;
  //cout<<addition<<endl;//problme here with printing solution
  cout<<endl;
  
  cout<<"Subtraction: ";
  Money subtraction=m1-m2;
 // cout<<subtraction<<endl;   //problme here with printing solution
  cout<<endl;
  
  cout<<"Division: ";
  Money division=m1/m2;
  //cout<<division<<endl; //problme here with printing solution
  cout<<endl;
  
  cout<<"Amount one multiplied by "<<scalar<<" is: ";
  Money mult_scalar=m1*scalar;
  //cout<<mult_scalar<<endl;//problme here with printing solution
  cout<<endl;
      
  cout<<"Amount one divided by "<<scalar<<" is: ";
  Money div_scalar=m1/scalar;
  //cout<<div_scalar<<endl;  //problme here with printing solution
  cout<<endl;
  
  double divi=m1/m2;
  cout<<"\nDouble m1/m2 is: "<<divi<<endl;
  //same here .. no more divi
   
  double multi=m1*m2;
  cout<<"Double m1/m2 is: "<<multi<<endl;
  //same here ... no more multi
   
      
  cout<<"Again (y/n): ";
  cin>>choice;    
 }while(choice=='y' || choice=='Y');
  cout<<"Have a nice day: ";
  exit(1);
  _getch();
  return 0;
}
ostream& operator<<(ostream& out, Money& m){
 out<<m.amount<<endl;        
 return out;         
}
istream& operator>>(istream& in, Money& m){
 cout<<"Enter an amount of money: ";
 getline(in,m.amount);        
 return in;        
}
Last edited on
Why don't you simply work with doubles? Then just print whatever you want with cout...If you'd had double amount as a data member and not string , then you could simply do
1
2
3
Money operator+(Money& m){
 return Money(amount + m.amount);
}

and so on for the rest of the operators.. and only after you do all these operations you can have separate functions which do formatting and so on. That's how you should have done it.
The exercise asks for the user to start with a string object, that's why there's an issue. I already thought of using a double private amount and going from there.

Is there anyway to alter the code, so I can still use cout, without stringstream messing everything up? I tried to remove "ss<<d" but then stringstream does not work properly.

Thanks,

Mike
In Money(double) you're printing the number again...you just have to save it into amount that's all. See the 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
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
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <sstream>
using namespace std;

class Money
{
public:
	Money(){}
	Money(string s):amount(s){}
	Money (double d)
	{
		stringstream ss;    
		ss.setf(ios::fixed);
		ss.setf(ios::showpoint);
		ss.precision(2);
		ss<<d;
		ss>>amount;

		//reset ss
		ss.clear();

		int location=amount.find_first_of('.');
		int distance;
		for(int i=0;i<amount.length();i++)
		{
			if(i==0)
				//putchar('$');
				ss<<"$";

			//putchar(amount[i]);
			ss<<amount[i];
			distance=location-i-1;//from end of array
			if(distance>0 && distance%3==0)
				//putchar(',');
				ss<<",";
		} 
		
		ss>>amount; //put the formatted number into amount
	}

	friend istream& operator>>(istream& in, Money& m);
	friend ostream& operator<<(ostream& out, Money& m);

	Money operator+(Money& m){
		return Money(double(Money(amount))+double(m));      
	}
	Money operator-(Money& m){
		return Money(double(Money(amount))-double(m));      
	}
	Money operator/(Money& m){
		return Money(double(Money(amount))/double(m));      
	}
	Money operator/(double d){
		return Money(double(Money(amount))/d);      
	}
	Money operator*(double d){
		return Money(double(Money(amount))*d);      
	}

	operator double()
	{
		cout.setf(ios::fixed);
		cout.setf(ios::showpoint);
		cout.precision(2);   
		int i, j, count=0;
		char sol[36];
		for(i=0,j=0;i<amount.length();i++)
		{
			if(isdigit(amount[i]) || amount[i]=='.')     
				sol[j++]=amount[i]; 
			if(amount[i]=='.' || amount[i]==',') 
				count++;
		}
		sol[amount.length()-count+1]='\0';        
		return (atof(sol));       
	}   

private:      
	string amount;     
};

int main()
{
	char choice;
	Money m1("$23,435.23"), m2("$5,245.11");
	do{
		//cin>>m1;
		//cin>>m2;
		//cout<<endl;
		cout<<"First amount is: "<<m1;
		cout<<"Second amount is: "<<m2<<endl<<endl;    
		double scalar=3.14;
		double one=m1;
		double two=m2;

		cout<<"Addition: ";
		Money addition=m1+m2;
		cout<<addition<<endl;
		cout<<endl;

		cout<<"Subtraction: ";
		Money subtraction=m1-m2;
		cout<<subtraction<<endl;
		cout<<endl;

		cout<<"Division: ";
		Money division=m1/m2;
		cout<<division<<endl;
		cout<<endl;

		cout<<"Amount one multiplied by "<<scalar<<" is: ";
		Money mult_scalar=m1*scalar;
		cout<<mult_scalar<<endl;
		cout<<endl;

		cout<<"Amount one divided by "<<scalar<<" is: ";
		Money div_scalar=m1/scalar;
		cout<<div_scalar<<endl;
		cout<<endl;

		double divi=m1/m2;
		cout<<"\nDouble m1/m2 is: "<<divi<<endl;

		double multi=m1*m2;
		cout<<"Double m1/m2 is: "<<multi<<endl;


		cout<<"Again (y/n): ";
		cin>>choice;    
	}while(choice=='y' || choice=='Y');
	cout<<"Have a nice day: ";
	exit(1);
	_getch();
	return 0;
}
ostream& operator<<(ostream& out, Money& m){
	out<<m.amount<<endl;        
	return out;         
}
istream& operator>>(istream& in, Money& m){
	cout<<"Enter an amount of money: ";
	getline(in,m.amount);        
	return in;        
}
Last edited on
Hi,

Sorry for the belated reply knuth, but it's been a hectic week. I see what you did with the code to make it work. The help is much appreciated!!

Thanks again,

Mike
Topic archived. No new replies allowed.