954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with converter program

I am still a beginner, I recently started learning C++. However I am writing a program that converts currencies. For example Dollars to Euros. However when I have them pick the number of the currency they want to convert the number 2 doesnt work. Here it is:

#include <iostream>
using namespace std;

int main()
{
	int currency;
	float eurosdollars;
	float dollarseuros;
	
	cout<<"1.Dollars to Euros\n"<<endl;
	cout<<"2.Euros to Dollars\n"<<endl;
	
	cout<<"Please enter the number of the currency you want to convert: ";
	cin>>currency;
	
	if (currency==1)
	cout<<"Please enter the Dollars you would like to convert to Euros: ";
	cin>>eurosdollars;
	
	cout<<"You have entered "<<eurosdollars<<" dollars which is equal to "<<eurosdollars * 0.678518116<<" euros."<<endl;
	
	if (currency==2)
	cout<<"Please enter the Euros you would like convert to Dollars: ";
	cin>>dollarseuros;
	
	cout<<"You have entered "<<dollarseuros<<" Euros which is equal to "<<dollarseuros * 1.4738 <<" Dollars."<<endl;
	}

Right at the if (currency==2)
cout<<"Please enter the Euros you would like convert to Dollars: ";
cin>>dollarseuros; it never works when you type in 2 it doesn't go to that. It just stays the same even if you keep typing in 2.

np2100
Light Poster
25 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

I think you need to put braces around your if statements. Otherwise its always going to read in the eurosdollars variable. Its a good practice put braces around the body of the condition, even if its one line.

if (currency==1){
	cout<<"Please enter the Dollars you would like to convert to Euros: ";
	cin>>eurosdollars;
	
	cout<<"You have entered "<<eurosdollars<<" dollars which is equal to "<<eurosdollars * 0.678518116<<" euros."<<endl;
}
stilllearning
Posting Whiz
309 posts since Oct 2007
Reputation Points: 161
Solved Threads: 43
 

Using proper formatting helps solve problems like this...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Thank you I figured it out, I need blocks around my input.

np2100
Light Poster
25 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You