Implementing a loop

I have the conversion rates. I'm just having trouble implementing a loop with decision statements. Any help or advice would be greatly appreciated.

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
  /* Allows currency to   be   converted from various types to US dollars.
*/
#include <iostream>
#include <string>
using namespace std;


const int USD =  0;
const int EUR =  1;
const int GBP =  2;
const int INR =  3;
const int AUD =  4;
const int CAD =  5;
const int ZAR =  6;
const int NZD =  7;
const int JPY =  8;
//Outputs a  menu of currency types for user, returning a constant value
//   as defined above representing the selected currency.
int getUserSelection()
{
int selection;
cout <<   "Available currencies for conversion: "  << endl;
cout <<   "(1) Euros" << endl;
cout <<   "(2) Great Britain Pounds" <<   endl;
cout <<   "(3) Indian Rupees" <<   endl;
cout <<   "(4) Australian Dollars" <<   endl;
cout <<   "(5) Canadian Dollars" <<   endl;
cout <<   "(6) South African Rands" <<   endl;
cout <<   "(7) New Zealand Dollars" <<   endl;
cout <<   "(8) Japanese Zen" <<   endl;
cout <<   "Enter the number of the desired currency to convert:";
cin  >> selection;
return selection;
}
//Converts the numeric type to   a String representation.
//Use this to output a  String representation of the type.
string convertTypeToString(int type)
{
switch (type)
{

case USD:
return "US Dollars";
case EUR:
return "Euros";
case GBP:
return "Great Britain Pounds";
case INR:
return "Indian Rupees";
case AUD:
return "Australian Dollars";
case CAD:
return "Canadian Dollars";
case ZAR:
return
"South African Rands";
case NZD:
return
"New Zealand Dollars";
case JPY:
return
"Japanese Zen";
}
return "";
}
int main(int argc,char*argv[])
{
int userSelection = getUserSelection();
string selectionAsAString = convertTypeToString(userSelection);
cout << "You entered "  << selectionAsAString << endl;
cin.ignore();
cin.get();

double currencyAmount;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

cout << "Enter the amount of money to convert in " << selectionAsAString << " to US dollars:" << endl;
cin  >> currencyAmount;

}
for (int i = 0 ; i <=8 ; i++ )
{
    if (userSelection == EUR)
         conversionFactor = 1.08611;
}
Last edited on
I took the liberty of putting your code in a more common format. What is the exact problem that you have with the loop? And could it be related to the loop not being inside any function?

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
/* Allows currency to   be   converted from various types to US dollars.
Author: Kyle Smith for CSE 153 Fall, 2017
*/
#include <iostream>
#include <string>
using namespace std;


const int USD =  0;
const int EUR =  1;
const int GBP =  2;
const int INR =  3;
const int AUD =  4;
const int CAD =  5;
const int ZAR =  6;
const int NZD =  7;
const int JPY =  8;

//Outputs a  menu of currency types for user, returning a constant value
//   as defined above representing the selected currency.
int getUserSelection()
{
	int selection;
	cout <<   "Available currencies for conversion: "  << endl;
	cout <<   "(1) Euros" << endl;
	cout <<   "(2) Great Britain Pounds" <<   endl;
	cout <<   "(3) Indian Rupees" <<   endl;
	cout <<   "(4) Australian Dollars" <<   endl;
	cout <<   "(5) Canadian Dollars" <<   endl;
	cout <<   "(6) South African Rands" <<   endl;
	cout <<   "(7) New Zealand Dollars" <<   endl;
	cout <<   "(8) Japanese Zen" <<   endl;
	cout <<   "Enter the number of the desired currency to convert:";
	cin  >> selection;
	return selection;
}
//Converts the numeric type to   a String representation.
//Use this to output a  String representation of the type.
string convertTypeToString(int type)
{
	switch (type)
	{
		case USD: return "US Dollars";
		case EUR: return "Euros";
		case GBP: return "Great Britain Pounds";
		case INR: return "Indian Rupees";
		case AUD: return "Australian Dollars";
		case CAD: return "Canadian Dollars";
		case ZAR: return "South African Rands";
		case NZD: return "New Zealand Dollars";
		case JPY: return "Japanese Zen"; // Shouldn't this be Yen?
	}
	return "";
}

int main(int argc,char*argv[])
{
	int userSelection = getUserSelection();
	string selectionAsAString = convertTypeToString(userSelection);
	cout << "You entered "  << selectionAsAString << endl;
	cin.ignore();
	cin.get();

	double currencyAmount;
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	cout << "Enter the amount of money to convert in " << selectionAsAString << " to US dollars:" << endl;
	cin  >> currencyAmount;

}

for (int i = 0 ; i <=8 ; i++ )
{
	if (userSelection == EUR) conversionFactor = 1.08611;
}
Last edited on
Thank you for reformatting. The problem that I am having is putting in the correct syntax for the conversion factors of the currency entered in the beginning of the program to output the correct amount of US dollars. I wasn't sure if the way that I started doing that on line 74 was correct or not? If it is, I'm assuming that I can loop the statement for the other currencies listed in the beginning of the program along with their conversion factors?
Your for-loop is just a bit of code that will be executed multiple times. If you can do it once without the loop you can subsequently place the code between the brackets and add the loop to repeat it. So probably you should figure out how to make the correct decision first.

This should not be a problem since you have already done this in line 41-52. The only difference is that you can't call return this time so you have to call "break;" at the end of the implementation of each case.
As an alternative you could use an if-statement like you started in line 76, in that case i would recommend using "else if" for the other conditions (then your program will stop checking conditions as soon as it found one match, if you only use "if" for conditions it will check every condition, even if it has already found the correct one.

Kind regards, Nico
Thank you
I am getting an error in line 84 that states that i doesn't name a type when I try to build the program to check if I am writing the code properly. How would I go about fixing this problem?
I need to add a loop in the main program that will loop until the user indicates that they have no more currency to be converted. I'm guessing that will be a sentinel value. We haven't covered that so that is where I am stuck.
Your for-loop is not a function, therefor it has to be inside a function (probably inside the main function).
Initially (in the code you posted) it was not inside a function, because a function starts with the { directly after the function header and ends as soon as there are an equal number of opening and closing brackets. In the code you posted this happens in line 83.
Since your for-loop is in line 84, it is outside of the main function. Because of that your compiler doesn't properly recognize it. This usually happens if you don't indent your code, because then it becomes very hard to see which brackets belong together.

Have you fixed that yet? If so, could you post an update?
I will fix it and post here in a second
/* Allows currency to be converted from various types to US dollars.
*/
#include <iostream>
#include <string>
using namespace std;


const int USD = 0;
const int EUR = 1;
const int GBP = 2;
const int INR = 3;
const int AUD = 4;
const int CAD = 5;
const int ZAR = 6;
const int NZD = 7;
const int JPY = 8;
//Outputs a menu of currency types for user, returning a constant value
// as defined above representing the selected currency.
int getUserSelection()
{
int selection;
cout << "Available currencies for conversion: " << endl;
cout << "(1) Euros" << endl;
cout << "(2) Great Britain Pounds" << endl;
cout << "(3) Indian Rupees" << endl;
cout << "(4) Australian Dollars" << endl;
cout << "(5) Canadian Dollars" << endl;
cout << "(6) South African Rands" << endl;
cout << "(7) New Zealand Dollars" << endl;
cout << "(8) Japanese Yen" << endl;
cout << "Enter the number of the desired currency to convert:";
cin >> selection;
return selection;
}
//Converts the numeric type to a String representation.
//Use this to output a String representation of the type.
string convertTypeToString(int type)
{
switch (type)
{

case USD: return "US Dollars";
case EUR: return "Euros";
case GBP: return "Great Britain Pounds";
case INR: return "Indian Rupees";
case AUD: return "Australian Dollars";
case CAD: return "Canadian Dollars";
case ZAR: return "South African Rands";
case NZD: return "New Zealand Dollars";
case JPY: return "Japanese Yen";
}
return "";
}
int main(int argc,char*argv[])
{
int userSelection = getUserSelection();
string selectionAsAString = convertTypeToString(userSelection);
cout << "You entered " << selectionAsAString << endl;
cin.ignore();
cin.get();

double currencyAmount;
double usDollars;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

cout << "Enter the amount of money to convert in " << selectionAsAString << " to US dollars:" << endl;
cin >> currencyAmount;

for (int i = 1 ; i <= 8 ; i++)
{
if (userSelection == EUR)
int conversionFactor1 = 1.08611;

This is where I am getting stuck. I'm not able to output the correct conversion to US dollars.
Last edited on
I might recommend that if you're going to get help with your homework on the internet, you don't post your name and the class you're taking. A google search right now for your name and class gives this page, with you getting homework help, as the top result.

The amount of US dollars to output is the amount of currency multiplied by the conversion factor.
Last edited on
int conversionFactor1 = 1.08611;
You are putting a fraction into an integer variable.
Not a problem for the computer, but it won't do what you hope it will.
If you actually did the first part of the assignment, then I would expect you to not have this problem. You can do exactly the same here as you have done before, it would look something like this:

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
#include <iostream>
#include <string>
using namespace std;


const int USD = 0;
const int EUR = 1;
const int GBP = 2;
const int INR = 3;
const int AUD = 4;
const int CAD = 5;
const int ZAR = 6;
const int NZD = 7;
const int JPY = 8;
//Outputs a menu of currency types for user, returning a constant value
// as defined above representing the selected currency.
int getUserSelection()
{
    int selection;
    cout << "Available currencies for conversion: " << endl;
    cout << "(1) Euros" << endl;
    cout << "(2) Great Britain Pounds" << endl;
    cout << "(3) Indian Rupees" << endl;
    cout << "(4) Australian Dollars" << endl;
    cout << "(5) Canadian Dollars" << endl;
    cout << "(6) South African Rands" << endl;
    cout << "(7) New Zealand Dollars" << endl;
    cout << "(8) Japanese Yen" << endl;
    cout << "Enter the number of the desired currency to convert:";
    cin >> selection;
    return selection;
}
//Converts the numeric type to a String representation.
//Use this to output a String representation of the type.
string convertTypeToString(int type)
{
    switch (type)
    {
        case USD: return "US Dollars";
        case EUR: return "Euros";
        case GBP: return "Great Britain Pounds";
        case INR: return "Indian Rupees";
        case AUD: return "Australian Dollars";
        case CAD: return "Canadian Dollars";
        case ZAR: return "South African Rands";
        case NZD: return "New Zealand Dollars";
        case JPY: return "Japanese Yen";
    }
    return "";
}
int main(int argc,char*argv[])
{
    int userSelection = getUserSelection();
    string selectionAsAString = convertTypeToString(userSelection);
    cout << "You entered " << selectionAsAString << endl;
    cin.ignore();
    cin.get();

    double currencyAmount;
    double usDollars;
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    cout << "Enter the amount of money to convert to " << selectionAsAString << " in euro's:" << endl;
    cin >> currencyAmount;

    switch (userSelection)
    {
        case USD:   currencyAmount = currencyAmount*1.08611;
                    break;
        case EUR:   currencyAmount = currencyAmount;
                    break;
        case GBP:   currencyAmount = currencyAmount/3.333301;
                    break;
        case INR:   currencyAmount = currencyAmount*1500.12345;
                    break;
        case AUD:   currencyAmount = currencyAmount/2.0;
                    break;
        case CAD:   currencyAmount = currencyAmount/1.500;
                    break;
        case ZAR:   currencyAmount = currencyAmount*0.12345;
                    break;
        case NZD:   currencyAmount = currencyAmount/3.45;
                    break;
        case JPY:   currencyAmount = currencyAmount*1234.5;
                    break;
    }
    cout << "\nThat would be " << currencyAmount << " " << convertTypeToString(userSelection);
}


Once you have it working for a single case, you can start to add your loop.
Last edited on
Topic archived. No new replies allowed.