Trouble with my currency conversion program

I am learning C++ at home because my school doesn't offer C++ classes. I am extremely new to C++, so please don't mock me for simple mistakes. That aside, I am writing a program for practice that converts an amount in a currency chosen by the user and converts it to U.S. dollars. My code looks fine to me but I get the following error message when I run it:


CurrencyConverter.cpp:5:1: error: expected unqualified-id before ‘{’ token
CurrencyConverter.cpp:12:1: error: expected unqualified-id before ‘{’ token
CurrencyConverter.cpp:19:1: error: expected unqualified-id before ‘{’ token
CurrencyConverter.cpp: In function ‘int main()’:
CurrencyConverter.cpp:31:13: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
CurrencyConverter.cpp:31:18: error: ‘then’ was not declared in this scope
CurrencyConverter.cpp:31:23: error: expected ‘;’ before ‘Peso’
CurrencyConverter.cpp:32:18: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
CurrencyConverter.cpp:32:23: error: ‘then’ was not declared in this scope
CurrencyConverter.cpp:32:28: error: expected ‘;’ before ‘Dollar’
CurrencyConverter.cpp:33:18: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
CurrencyConverter.cpp:33:23: error: ‘then’ was not declared in this scope
CurrencyConverter.cpp:33:28: error: expected ‘;’ before ‘Euro’
CurrencyConverter.cpp:35:1: error: expected ‘;’ before ‘return’



I am normally able to fix my programs by looking at the error messages, but this time I couldn't figure out what's wrong with my code. Any help would be greatly appreciated. Here is my program:

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
//This program takes a currency of the user's choice and converts an amount in that currency to U.S. dollars
#include <iostream>
using namespace std;

void Peso();
{
float solve1;
solve1=choice*0.080462;
cout <<solve1" U.S. Dollars." <<endl;
}

void Dollar();
{
float solve2;
solve2=choice*0.9796;
cout <<solve2" U.S. Dollars." <<endl;
}

void Euro();
{
float solve3;
solve3=choice*1.3040;
cout <<solve3" U.S. Dollars." <<endl;
}

int main()
{
char choice;
cout <<"What currency are you converting to U.S Dollars? Enter the corresponding letter of the currency you're converting.(a,b,or c)" <<endl;
cout <<"a)Mexican Peso; b)Canadian Dollar; c)European Euro" <<endl;
cin >>choice;
if (choice=="a") then Peso();
else if (choice=="b") then Dollar();
else if (choice=="c") then Euro();
else cout <<"Not a valid choice. Please enter one of the following: a,b, or c."
return 0;
}


Again, I greatly appreciate any help I get.

Thanks for your time,
Austin
Last edited on
A few things:

-Declare variable BEFORE the first use of them. So declare char choice, right near the top:
1
2
3
using namespace std;

char choice 


-Functions do not have a semi-colon after the braces unless you are only declaring them.
1
2
3
4
5
6
7
8
9
10
11
void function(); //Correct 

void function() //Correct
{
//stuff
}

void function(); //Incorrect
{
//stuff
}


-When using cout, you must seperate variables and strings.
1
2
3
cout <<solve2" U.S. Dollars." <<endl; //Incorrect

cout << solve2 << " U.S. Dollars." << endl; //Correct 


-If comparing characters, put them in single quotes.
1
2
3
if (choice == "a") // Incorrect. 

if (choice == 'a') // Correct. 


-'then' does not mean anything. To call a function do this or this:
1
2
3
4
5
6
7
8
9
if (condition)
{
functioncall(); 
}

OR

if (condition)
functioncall(); 


Note that the second way ONLY works if you put one line of code. For multiple lines of code, braces must be present.

Hope that helps you fix your problems. :)

Edit: What is the code supposed to do? It doesn't really do anything at the moment.

And I just saw a missing semi-colon at line 35. ;)
Last edited on
Thanks a ton! The program actually runs now, but doesn't do what I want it to. (I caught the missing semi-colon on line 35 when I was fixing the code using what you told me, thanks! ;D)

Now my new problem is what you said. It doesn't really do anything. I was a complete moron and forgot to ask the number the user wants to convert, store it in a variable, and then do the multiplication. I'll go fix that right now. Thanks a lot for your time! I really appreciate it! :D

Edit: Everything is fixed! The program runs perfectly and does what I want it to do! Thank you so much! Here is the finished code in case anyone wants to see it:

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
//This program takes a currency of the user's choice and converts an amount in that currency to U.S. dollars
#include <iostream>
using namespace std;
char choice;

void Peso()
{
float solve1;
float amount1;
cout <<"Enter the amount in Pesos you want that you want to convert to U.S. Dollars." <<endl;
cin >>amount1;
solve1=amount1*0.080462;
cout <<solve1 <<" U.S. Dollars." <<endl;
}

void Dollar()
{
float solve2;
float amount2;
cout <<"Enter the amount in Canadian Dollars that you want to convert to U.S. Dollars." <<endl;
cin >>amount2;
solve2=amount2*0.9796;
cout <<solve2 <<" U.S. Dollars." <<endl;
}

void Euro()
{
float solve3;
float amount3;
cout <<"Enter the amount in Euros that you want to convert to U.S. Dollars." <<endl;
cin >>amount3;
solve3=choice*1.3040;
cout <<solve3 <<" U.S. Dollars." <<endl;
}

int main()
{
cout <<"What currency are you converting to U.S Dollars? Enter the corresponding letter of the currency you're converting.(a,b,or c)" <<endl;
cout <<"a)Mexican Peso b)Canadian Dollar c)European Euro" <<endl;
cin >>choice;
if (choice=='a') 
{
Peso();
}
else if (choice=='b')
{
Dollar();
}
else if (choice=='c')
{
Euro();
}
else cout <<"Not a valid choice. Please enter one of the following: a,b, or c." <<endl;
return 0;
}

Last edited on
Umm, just a suggestion.. Indentation goes a long way to make your code more readable and clean... Its a fairly good habit as well...
You're right Caprico, I probably should start indenting.
Topic archived. No new replies allowed.