Miscalculations, but I'm not sure where...

Hey,
Last edited on
No need to use a do while loop. The condition while(opt != 0) will always be true and the loop will keep running. Also line 41, 47, 53, you forgot to use ' ' to encapsulate the character. You cannot output to the console a variable from a function that is not in the scope of main() so you need to assign a variable to hold the value you are returning from your function. You also forgot to put a ' ; ' after your while statement. Also some of your conversion rates are incorrect. For your menu option I would suggest using a switch statement, it simplifies things greatly and you can use the switch statement with characters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
switch(curFrn)
{
    case 'E':
    case 'e':
    {
        //place code here
    }
    break;
    case 'P':
    case 'p':
    {
        //place code here
    }
    break
    default:
    {
        //place default code if none of your options are chosen
    }
}


This is your code cleaned up a bit. Your miscalculations are in your conversion. The rate you have is from USD to "Euro, Pound, Yen".

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

//function declarations
		
int toUSD(double amountFrn, char curFrn);
	
void menu();
	
int main()
{
	double amountFrn;
	char curFrn;
	int menuOpt;
	double addSum = 0;
	double amountSum = 0; //added variable to store return from function
	
		
	cout << "Welcome to the currency exchange calculator!" << endl;
	
	
	

	menu();
	cin >> menuOpt;
		if (menuOpt == 1){
				cout << "What amount would you like to convert? > " ;
				cin >> amountFrn;
					//invalid negative integer entry code
						if ( amountFrn < 0 ) 
							{
								cout << "Invalid integer, no negative integers. PLease restart." << endl;
								return 0;
							}
				cout << "Which currency would you like to convert to USD?" << endl;
				cout << "P - Pound       E - Euro        Y - Yen\n> " ; 
				cin >> curFrn;
				
				if (curFrn == 'P' || curFrn == 'p') //encapsulated the characters
						{
							cout << amountFrn << " Pounds converts to " ;
							amountSum = toUSD(amountFrn, curFrn);
							cout << amountSum << " US Dollars. " << endl;
						}
				else if ( curFrn == 'E' || curFrn == 'e' )
						{
							cout << amountFrn << " Euros converts to " ;
							amountSum = toUSD(amountFrn, curFrn);
							cout << amountSum << " US Dollars. " << endl;
						}
				else if (curFrn == 'Y' || curFrn == 'y')
						{
							cout << amountFrn << " Yen converts to " ;
							amountSum = toUSD(amountFrn, curFrn);
							cout << amountSum << " US Dollars. " << endl;
						}
		}else if (menuOpt == 2){
				cout << "Enter type of first currency    P - Pound       E - Euro        Y - Yen\n>" ;
				cin >> curFrn;
				cout << "Enter amount of first currency \n>" ;
				cin >> amountFrn;
					//invalid negative integer entry code
					if ( amountFrn < 0 ) 
							{
								cout << "Invalid integer, no negative integers. PLease restart." << endl;
								return 0;
							}
				toUSD(amountFrn, curFrn);
				addSum += amountSum;
				cout << "Enter type of second currency    P - Pound       E - Euro        Y - Yen\n>" ;
				cin >> curFrn;
				cout << "Enter amount of second currency \n>" ;
				cin >> amountFrn;
					//invalid negative integer entry code
					if ( amountFrn < 0 ) 
							{
								cout << "Invalid integer, no negative integers. PLease restart." << endl;
								return 0;
							}
				toUSD(amountFrn, curFrn);
				addSum += amountSum;
				cout << "This would equal " << addSum << " US Dollars " << endl;
		} else if (menuOpt == 0) {
			cout << "Goodbye!" << endl;
		} else {
			cout << "Invalid Option. Please try again." << endl;
		} //removed the loop
	return 0;
}




//funciton definitions

int toUSD(double amountFrn, char curFrn)//recheck conversion rates
{
	const double Pound = 0.77;
	const double Euro = 0.88;
	const double Yen = 109.74;
	double amountSum;
	
	if (curFrn == 'P' || curFrn == 'p') {
		amountSum =  amountFrn * Pound;
	}else if (curFrn == 'E' || curFrn == 'e'){
		amountSum = amountFrn * Euro;
	}else if (curFrn == 'Y' || curFrn == 'y'){
		amountSum = amountFrn * Yen;
	}
	return amountSum;
}

void menu()
{
	cout << "Please select one of the following options: " << endl;
	cout << "1 - Convert foreign Amount to USD" << endl;
	cout << "2 - Add foreign amounts" << endl;
	cout << "0 - Quit" << endl;
	cout << "-----------------------------------" << endl;
	cout << "> ";
}
Last edited on
everything runs quite well in fact

It doesn't even compile. And your indentation is insane. Instead of tabs, use 4 spaces, and be consistent.
Last edited on
@dutch I though the same when I tried to run it the first time, then had the tab that was running the code crash when the do while loop ran.
dutch wrote:
Instead of tabs, use 4 spaces, and be consistent.

There is nothing wrong with tabs if used properly.
> There is nothing wrong with tabs if used properly.
Only that at some point, some text editor, printer, forum, text processor will screw it up in some way. Your nice presentation is suddenly reduced to dog food.

Even more so if you decided in your editor that a tab was something other than 8 spaces. A fact that is very hard to communicate to other tools which might process the text for display.

If you want a real cluster-fxxk, mix spaces and tabs for indentation.

For a "looks the same everywhere" experience, just use spaces. Every usable code editor has smart indent features to prevent repetitive strain injury.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// spaces only
void foo ( ) {
    for ( int i = 0 ; i < 10 ; i++ ) {
        cout << "i=" << i << endl;
        cout << "hello\n";
    }
}
// tabs only
void bar ( ) {
	for ( int i = 0 ; i < 10 ; i++ ) {
		cout << "i=" << i << endl;
		cout << "hello\n";
	}
}

// silly mix of spaces and tabs
// looked OK in my editor (tab = 4 spaces), YMMV
void baz ( ) {
    for ( int i = 0 ; i < 10 ; i++ ) {
		cout << "i=" << i << endl;
	    cout << "hello\n";
    }
}

https://ibb.co/DtbSHJn
@nicholasjb1996 do you think thatll fix my miscalculating in the variable reuslts?


Also, indention is not my focus, as everything runs fine regardless of how i indented things. I have seen probably a hundred different styles of how people space their code, from what I see in replies. Its just being nitpicky, not genuinely finding a solution to my issue lol.
@nicholasjb1996 i think i see what youre saying now somehwat, i need to divide by those numbers in my toUSD? instead of multiplying?
I believe thats what im understdning.
Also, I initially had coded the program with a switch statement but had simlpy resorted to constantly running the menu code and i eventually had to ^C out of it.



P.S. sorry for horrible spelling. im outside and my hand are cold lol.
@ alldaynodae The issue I see with your code is your conversion. I am unsure whether you know which currency you are converting to. When your code runs it prompts the user to enter an amount of foreign currency. Then it prompts the user to enter the country which this currency origins, eg: 50---->E---->€. When you hit enter, your code now converts from Euros to USD but the calculation is incorrect because the conversion rate you have is USD to Euros. If I may, I would suggest you use the switch case and simplify your menu options and you can also implement switch case for the conversion. Also the algorithm you used is a bit mixed, you should first ask the user which currency they would like to convert to, then ask them to enter the amount they would like to convert. (I think it seems more logical this way, its just my opinion). If you are having issue with the switch statement, I would suggest looking up a tutorial video. Also from reading your code the first option says to convert foreign amount to USD. Google Euro to USD, Pound to USD, Yen to USD. Those are your conversion values. Simply replace your values in your conversion function and the conversion should be accurate. To answer your question, you do not need to divide, the multiplication by the correct conversion rate will give you the correct answer.
Alright, I fixed what was happening.
Last edited on
Topic archived. No new replies allowed.