Adding values within a loop

I am writing a program that will calculate how much some selected foreign currency would cost in us dollars. I am having trouble getting started with the loop that will keep track and add all of it up once the user hits "0", or quits the program. Also, i am having trouble with restarting my program and inputting new values for the exchange rate, i.e. when the user wants to input a "9".

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
 #include "stdafx.h"
#include <iostream>
#include<cmath>

using namespace std;
using namespace System;
double eur2us();
double gbp2us();
double jpy2us();
double eur, gbp, jpy;
double us;


int main()
{
	double a;
	int choice;

	cout << "Welcome to my currency calculator!" << endl;
	while (choice != 0)
	{
		cout << "Please input the current exchange rate per US dollar of the following" << endl;
		cout << "EUR, GBP, JPY ";
		cin >> eur >> gbp >> jpy;
		do
		{
			cout << "Please input currency for valuation (1 = EUR 2 = GBP 3 = JPY 9 = NEW RATES \n0 = QUIT" << endl;
			cin >> choice;

			if (choice == 1)
			{
				a = eur2us();
				
			}
			else if (choice == 2)
			{
				a = gbp2us();
				
			}
			else if (choice = 3)
			{
				a = jpy2us();
				
			}
			else
			{
				cout << "Enter a valid key please" << endl;
			}
		} while (choice != 9);
	}
	system("pause");
    return 0;
}
double eur2us()
{
	double euros;
	

	cout<<"How many Euros are you buying?" << endl;
	cin >> euros;
	us = euros*eur;
	cout << "That will cost you "<<us<<" US dollars"<<endl;

	return us;
}
double gbp2us()
{
	double pounds;

	cout << "How many pounds of Sterling are you buying?" << endl;
	cin >> pounds;
	us = pounds*gbp;
	cout << "That will cost you "<<us<<" US dollars"<<endl;
	
	return us;
}
double jpy2us()
{
	double yen;
	cout << "How many Yen are you buying?" << endl;
	us = yen*jpy;
	cout << "That will cost you "<<us<<"US dollars"<<endl;

	return us;
}
Ok, I fixed some problems but am still having trouble with my looping techniques. How do I add, and for some reason my 0 to quit isn't working anymore?

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<cmath>

using namespace std;
using namespace System;
double eur2us();
double gbp2us();
double jpy2us();
double eur, gbp, jpy;



int main()
{
	double a1, a2, a3;
	int choice = 8;
	double total = 0;

	cout << "Welcome to my currency calculator!" << endl;
	do
	{
		cout << "Please input the current exchange rate per US dollar of the following" << endl;
		cout << "EUR, GBP, JPY ";
		cin >> eur >> gbp >> jpy;
		do
		{
			cout << "Please input currency for valuation (1 = EUR 2 = GBP 3 = JPY 9 = NEW RATES \n0 = QUIT" << endl;
			cin >> choice;

			if (choice == 1)
			{
				a1 = eur2us();
				
			}
			else if (choice == 2)
			{
				a2 = gbp2us();
				
			}
			else if (choice == 3)
			{
				a3 = jpy2us();
				
			}
			else if (choice == 0)
			{
				cout << "Sorry that you wish to quit the program" << endl;
			}
			else
			{
				cout << "Enter a valid key please" << endl;
			}
		} while (choice != 9);
	} while (choice != 0);
	total = a1 + a2 + a3;
	cout << "The total amount in US dollars is " << total<<endl;
	cout << "Thank You for using my currency converting program!" << endl;
	system("pause");
    return 0;
}
double eur2us()
{
	double euros;
	bool valid = false;
	
	while (!valid)
	{
		valid = true;
		cout << "How many Euros are you buying?" << endl;
		cin >> euros;
		if (cin.fail())
		{
			cin.clear();
			cin.ignore();
			cout << "Please enter a valid currency value "<<endl;
			valid = false;
		}
		else if (euros < 0)
		{
			cout<<"Please enter a positive monetary value" << endl;
			valid = false;
		}
	}
	double us1 = euros*eur;
	cout << "That will cost you "<<us1<<" US dollars"<<endl;

	return us1;
}
double gbp2us()
{
	double pounds;
	bool valid = false;

	while (!valid)
	{

		valid = true;
		cout << "How many pounds of Sterling are you buying?" << endl;
		cin >> pounds;
		if (cin.fail())
		{
			cin.clear();
			cin.ignore();
			cout << "Please enter a valid currency value" << endl;
			valid = false;
		}
		else if (pounds < 0)
		{
			cout << "Please enter a positive monetary value" << endl;
			valid = false;
		}
	}
	
	double us2 = pounds*gbp;
	cout << "That will cost you "<<us2<<" US dollars"<<endl;
	
	return us2;
}
double jpy2us()
{
	double yen;
	bool valid = false;

	while (!valid)
	{
		valid = true;
		cout << "How many Yen are you buying?" << endl;
		cin >> yen;
		if (cin.fail())
		{
			cin.clear();
			cin.ignore();
			cout << "Please enter a valid currency value" << endl;
			valid = false;
		}
		else if (yen < 0)
		{
			cout << "Please enter a positive monetary value" << endl;
			valid = false;
		}
	}
	double us3 = yen*jpy;
	cout << "That will cost you "<<us3<<"US dollars"<<endl;

	return us3;
}
I would delete the do/while loop. Specifically, line 21,line22, and line 55.

Take a look at line 54:
} while (choice != 9);

If you change it to:

} while (choice != 0);

Then it will close the program. I hope it helps.
Yes thanks a lot. I am still stuck on how to add up the, presumably 3, values for calculated us prices and output that at end of main() ?
Here is what I have so far, why doesn't this work?

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
using namespace std;
using namespace System;
void eur2us();
void gbp2us();
void jpy2us();
double eur, gbp, jpy;
double us1 = 0, us2 = 0, us3 = 0;


int main()
{
	
	int choice = 9;
	

	cout << "Welcome to my currency calculator!" << endl;
	while (choice == 9)
	{
		cout << "Please input the current exchange rate per US dollar of the following" << endl;
		cout << "EUR, GBP, JPY ";
		cin >> eur >> gbp >> jpy;
		do
		{
			cout << "Please input currency for valuation (1 = EUR 2 = GBP 3 = JPY 9 = NEW RATES \n0 = QUIT" << endl;
			cin >> choice;

			if (choice == 1)
			{
				eur2us();

			}
			else if (choice == 2)
			{
				gbp2us();

			}
			else if (choice == 3)
			{
				jpy2us();

			}
			else if (choice == 0)
			{
				cout << "Sorry that you wish to quit the program" << endl;
				break;
			}
			else
			{
				cout << "Enter a valid key please" << endl;
			}
		} while (choice != 9);

	}
	double total = us1 + us2 + us3;
	cout << "The total amount in US dollars is " << total<<endl;
	cout << "Thank You for using my currency converting program!" << endl;
	system("pause");
    return 0;
}
void eur2us()
{
	double euros;
	bool valid = false;
	
	while (!valid)
	{
		valid = true;
		cout << "How many Euros are you buying?" << endl;
		cin >> euros;
		if (cin.fail())
		{
			cin.clear();
			cin.ignore();
			cout << "Please enter a valid currency value "<<endl;
			valid = false;
		}
		else if (euros < 0)
		{
			cout<<"Please enter a positive monetary value" << endl;
			valid = false;
		}
	}
	double us1 = euros*eur;
	cout << "That will cost you "<<us1<<" US dollars"<<endl;

	return;
}
void gbp2us()
{
	double pounds;
	bool valid = false;

	while (!valid)
	{

		valid = true;
		cout << "How many pounds of Sterling are you buying?" << endl;
		cin >> pounds;
		if (cin.fail())
		{
			cin.clear();
			cin.ignore();
			cout << "Please enter a valid currency value" << endl;
			valid = false;
		}
		else if (pounds < 0)
		{
			cout << "Please enter a positive monetary value" << endl;
			valid = false;
		}
	}
	
	double us2 = pounds*gbp;
	cout << "That will cost you "<<us2<<" US dollars"<<endl;
	
	return ;
}
void jpy2us()
{
	double yen;
	bool valid = false;

	while (!valid)
	{
		valid = true;
		cout << "How many Yen are you buying?" << endl;
		cin >> yen;
		if (cin.fail())
		{
			cin.clear();
			cin.ignore();
			cout << "Please enter a valid currency value" << endl;
			valid = false;
		}
		else if (yen < 0)
		{
			cout << "Please enter a positive monetary value" << endl;
			valid = false;
		}
	}
	double us3 = yen*jpy;
	cout << "That will cost you "<<us3<<" US dollars"<<endl;

	return;
}
I am posting your code with my recommended changes. I added the comments on my recommendations:

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
#include "stdafx.h"
#include <iostream>
#include<cmath>

using namespace std;
using namespace System;
double eur2us();
double gbp2us();
double jpy2us();
double eur, gbp, jpy;



int main()
{
	double a1 = 0.0, a2 = 0.0, a3 = 0.0; // Initialize the variables.

	int choice = 8;
	double total = 0;

	cout << "Welcome to my currency calculator!" << endl;
	//do
	//{
		cout << "Please input the current exchange rate per US dollar of the following" << endl;
		cout << "EUR, GBP, JPY ";
		cin >> eur >> gbp >> jpy;
		do
		{
			cout << "Please input currency for valuation (1 = EUR 2 = GBP 3 = JPY 9 = NEW RATES \n0 = QUIT" << endl;
			cin >> choice;

			if (choice == 1)
			{
				a1 = eur2us();

			}
			else if (choice == 2)
			{
				a2 = gbp2us();

			}
			else if (choice == 3)
			{
				a3 = jpy2us();

			}
			else if (choice == 0)
			{
				cout << "Sorry that you wish to quit the program" << endl;
			}
			else
			{
				cout << "Enter a valid key please" << endl;
			}
                        
                        // Move it inside the while loop (next two lines).
			total = a1 + a2 + a3; 
			cout << "The total amount in US dollars is " << total << endl;

		} while (choice != 0);
	//} while (choice != 0);
	//total = a1 + a2 + a3; Old Location
	//cout << "The total amount in US dollars is " << total << endl; Old Location.
	cout << "Thank You for using my currency converting program!" << endl;
	system("pause");
	return 0;
}
double eur2us()
{
	double euros;
	bool valid = false;

	while (!valid)
	{
		valid = true;
		cout << "How many Euros are you buying?" << endl;
		cin >> euros;
		if (cin.fail())
		{
			cin.clear();
			cin.ignore();
			cout << "Please enter a valid currency value " << endl;
			valid = false;
		}
		else if (euros < 0)
		{
			cout << "Please enter a positive monetary value" << endl;
			valid = false;
		}
	}
	double us1 = euros*eur;
	cout << "That will cost you " << us1 << " US dollars" << endl;

	return us1;
}
double gbp2us()
{
	double pounds;
	bool valid = false;

	while (!valid)
	{

		valid = true;
		cout << "How many pounds of Sterling are you buying?" << endl;
		cin >> pounds;
		if (cin.fail())
		{
			cin.clear();
			cin.ignore();
			cout << "Please enter a valid currency value" << endl;
			valid = false;
		}
		else if (pounds < 0)
		{
			cout << "Please enter a positive monetary value" << endl;
			valid = false;
		}
	}

	double us2 = pounds*gbp;
	cout << "That will cost you " << us2 << " US dollars" << endl;

	return us2;
}
double jpy2us()
{
	double yen;
	bool valid = false;

	while (!valid)
	{
		valid = true;
		cout << "How many Yen are you buying?" << endl;
		cin >> yen;
		if (cin.fail())
		{
			cin.clear();
			cin.ignore();
			cout << "Please enter a valid currency value" << endl;
			valid = false;
		}
		else if (yen < 0)
		{
			cout << "Please enter a positive monetary value" << endl;
			valid = false;
		}
	}
	double us3 = yen*jpy;
	cout << "That will cost you " << us3 << "US dollars" << endl;

	return us3;
}


I hope it helps.
Last edited on
Nevermind, I got it on my own. thanks for the help though!
Good deal!
Topic archived. No new replies allowed.