GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Mar-2010, 17:10
xcajun_ xcajun_ is offline
New Member
 
Join Date: Mar 2010
Posts: 1
xcajun_ is on a distinguished road
Post

problem with if statements


this is soposed to be an all in one convertion program it runs just dosent do what i want it to. The problem is thats is going in order from 1 to 2 to 3. But i want it to jump to what number you type not for it to just run from begging to end. how do i get the if statements to work correctly? This is not homework for school im learing c++ on my free time.


CPP / C++ / C Code:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    
double convertion;
double answer;
double feet;
double inches;
double meters;
double yards;
double miles;



       cout << "This is an all in one converter!" << endl; 
       cout << "Press 1 for feet into inches!" << endl;
       cout << "Press 2 for inches into feet!" << endl;
       cout << "Press 3 for meter into feet!" << endl;
       cout << "Press 4 for feet into meter!" << endl;
       cout << "Press 5 for feet into miles!" << endl;
       cout << "Press 6 for miles into feet!" << endl;
       cout << "Press 7 for meters into miles!" << endl;
       cout << "Press 8 for miles into meters!" << endl;
       cout << "Press 9 for inches into meters!" << endl;
       cout << "Press 10 for meters into inches!" << endl;
    
       cin >> answer;

         if (answer == 1);  
       cout << "Please enter the amount of feet you wish to convert!" << endl;
       cin >> answer;       
       convertion = answer * 12;       
       cout << "this is " << convertion << " inches" << endl;
       
       
        if (answer == 2);  
cout << "Please enter the amount of inches you wish to convert!" << endl;
       cin >> answer;          
       convertion = answer / 12;
       cout << "this is " << convertion << " feet" << endl;
     
    
       if (answer == 3);         
 cout << "Please enter the amount of meters you wish to convert!" << endl;
   cin >> answer;      
      convertion = answer * 3.280;      
      cout << "This is " << convertion << " feet";
      
       cin.get();
       cin.get();
}
  #2  
Old 11-Mar-2010, 17:31
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 354
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: problem with if statements


You have semicolons after all your if statements, which means that they won't actually have any effect. Those semicolons all need to go:

CPP / C++ / C Code:
if (answer == 1)

That does not fix your problem, though. Now if you enter something other than 1, the line immediately after the if statement will not be executed, but everything after that will be. Curly braces will solve that problem:

CPP / C++ / C Code:
	if (answer == 1)
	{
		cout << "Please enter the number of feet you wish to convert!" << endl;
		cin >> answer;       
		conversion = answer * 12;       
		cout << "this is " << conversion << " inches" << endl;
	}

Finally, you only want one to do one conversion, so you should use if-else statements:

CPP / C++ / C Code:
	if (answer == 1)
	{
		cout << "Please enter the number of feet you wish to convert!" << endl;
		cin >> answer;       
		conversion = answer * 12;       
		cout << "this is " << conversion << " inches" << endl;
	}
	else if (answer == 2)
	{
		cout << "Please enter the number of inches you wish to convert!" << endl;
		cin >> answer;          
		conversion = answer / 12;
		cout << "this is " << conversion << " feet" << endl;
	}
        else if (answer == 3)
	{
		cout << "Please enter the number of meters you wish to convert!" << endl;
		cin >> answer;      
		conversion = answer * 3.280;      
		cout << "This is " << conversion << " feet";
	} 

Some other tips:

You may want to consider using a switch statement because they were designed for this kind of thing.

It's not a great idea to use the same variable to store the menu selection result and measurement to convert. You should probably make a different variable (an int) for the menu selection.

If you wanted to consider a larger set of conversions, manually coding every possible conversion would not be a good idea. It would be better to pick a unit for internally storing distances, convert the input units to the internal units, and then convert the internal units to the output units. For example, if you choose to use feet internally, you might store two constants inchesToFeet and milesToFeet. Then the conversion from inches to miles would be would be:

CPP / C++ / C Code:
const double inchesToFeet = 12.0;
const double milesToFeet = 1.0/5280.0;
//...
conversion = answer*inchesToFeet/milesToFeet;
__________________
www.blake-foster.com
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache Web Server newbie problem niss3 Apache Web Server Forum 1 13-Apr-2009 18:38
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 07:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:04.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.