If and/or else?

when do i use If and/or else?

Page 1 of 1

9 Replies - 2379 Views - Last Post: 04 September 2008 - 04:17 AM Rate Topic: -----

#1 smartin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-August 08

If and/or else?

Post icon  Posted 01 September 2008 - 08:04 AM

I am trying to write a simple program that will let you choose from 3 metric conversions.
I don't know what I am doing wrong. It will only let you choose the first option and then what ever is entered after first calculation continues to the next two calculations. I don't know what is missing. I am at "beginner" at best. Any suggestions would be appreciated.
#include <iostream>
using namespace std;

int main()
{
	//declare variables for calculation selections
	float calc_select, again;

	//define variables
	double feet, gallons, miles, meters, liters, kilometers;

	
	//Ask user to choose
	cout<< "\n\n What do you want to convert?\n"; 
	cout<< "\n\n Enter:\n"; 
	cout<<  " 1 for feet to meters,\n";
	cout<<  " 2 for gallons to liters,\n";
	cout<<  " 3 for miles to kilometers.\n";
	//store selection
	cin>> calc_select;
	//switch (calc_select);
	{
		//if calc_select == 1
		if (calc_select == 1)
			cout<< " Enter a number of feet to convert to meters.\n";
			cin>> feet;
			meters = (feet * 0.3048);
			cout  <<feet<< " feet equals "  <<meters<< " meters.\n";
			
		//if calc_select = 2
		if (calc_select == 2)
			cout<< " Enter a number of gallons to convert to liters.\n";
			cin>> gallons;
			liters = (gallons * 3.78541178);
			cout  <<gallons<< " gallons equals "  <<liters<< " liters.\n";
			
		//if calc_select = 3
		if (calc_select == 3)
			cout<< " Enter a number of miles to convert to meters.\n";
			cin>> miles;
			kilometers = (miles * 1.609344);
			cout <<miles<< " miles equals "  <<kilometers<< " kilometers.\n";
			
		//if calc_select = invalid
		
		
		
			//cout<< " Invalid selection!  Must enter a 1, 2, 3!\n";
			
		}
	

}



Is This A Good Question/Topic? 0
  • +

Replies To: If and/or else?

#2 Korupt   User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 185
  • Joined: 22-June 08

Re: If and/or else?

Posted 01 September 2008 - 08:22 AM

try this (also added a throughout loop with the start: label and the goto statement)

#include <iostream>

using namespace std;

int main()
{
    float calc_select, again;

    double feet, gallons, miles, meters, liters, kilometers;

    
start:
    cout << "What do you want to convert?\n";
    cout << "\n\n Enter:\n";
    cout <<  " 1 for feet to meters,\n";
    cout <<  " 2 for gallons to liters,\n";
    cout <<  " 3 for miles to kilometers.\n";
	cout <<  " Select now: ";
    cin>> calc_select;
	cin.ignore();
    if (calc_select == 1)
	{
		cout<< " Enter a number of feet to convert to meters: ";
        cin>> feet;
		cin.ignore();
        meters = (feet * 0.3048);
		cout << "\n";
        cout  <<feet<< " feet equals "  <<meters<< " meters.\n";
		goto start;
	}
            
    else if (calc_select == 2)
	{
		cout<< " Enter a number of gallons to convert to liters: ";
        cin>> gallons;
		cin.ignore();
        liters = (gallons * 3.78541178);
		cout << "\n";
        cout  <<gallons<< " gallons equals "  <<liters<< " liters.\n";
		goto start;
	}
            
    else if (calc_select == 3)
	{
		cout<< " Enter a number of miles to convert to meters: ";
        cin>> miles;
		cin.ignore();
        kilometers = (miles * 1.609344);
		cout << "\n";
        cout <<miles<< " miles equals "  <<kilometers<< " kilometers.\n";
		goto start;
	}

	else
	{
        cout<< " Invalid selection!  Must enter a 1, 2, 3!\n";
	}

	cin.get();
}

Was This Post Helpful? 1
  • +
  • -

#3 sensui   User is offline

  • D.I.C Head
  • member icon

Reputation: 30
  • View blog
  • Posts: 146
  • Joined: 24-August 08

Re: If and/or else?

Posted 01 September 2008 - 08:38 AM

If you want to use the switch instruction try the following code ( and read the comments ):
#include <iostream>
using namespace std;

int main() {
    //declare variables for calculation selections
    int calc_select; //this must be an int not a float if you want to work with switch

    //define variables
    double feet, gallons, miles, meters, liters, kilometers;


    //Ask user to choose
    cout<< "\n\n What do you want to convert?\n";
    cout<< "\n\n Enter:\n";
    cout<<  " 1 for feet to meters,\n";
    cout<<  " 2 for gallons to liters,\n";
    cout<<  " 3 for miles to kilometers.\n";
    //store selection
    cin >> calc_select;
    switch ( calc_select ) {
    case 1: //if calc_select == 1
        cout<< "Enter a number of feet to convert to meters.\n";
        cin>> feet;
        meters = (feet * 0.3048);
        cout  <<feet<< " feet equals "  <<meters<< " meters.\n";
        break; //you must use break if you don't want to execute the instructions
        //for case 2:

    case 2: //if calc_select == 2
        cout<< "Enter a number of gallons to convert to liters.\n";
        cin>> gallons;
        liters = (gallons * 3.78541178);
        cout  <<gallons<< " gallons equals "  <<liters<< " liters.\n";
        break;

    case 3: //calc_select == 3
        cout<< "Enter a number of miles to convert to meters.\n";
        cin>> miles;
        kilometers = ( miles * 1.609344 );
        cout << miles << " miles equals " << kilometers << " kilometers.\n";
        break;
    default: ////calc_select == invalid
        cout << "Invalid selection!  Must enter a 1, 2, 3!\n";

    };

    cin.get();
    return EXIT_SUCCESS;

}



I hope this will help you.
Was This Post Helpful? 1
  • +
  • -

#4 smartin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-August 08

Re: If and/or else?

Posted 03 September 2008 - 05:28 PM

Thanks for your help. That looks and works great! The instructor said we couldn't use "goto statements." Is there a way to loop it back to the main menu without the goto statement?

View PostKorupt, on 1 Sep, 2008 - 08:22 AM, said:

try this (also added a throughout loop with the start: label and the goto statement)

#include <iostream>

using namespace std;

int main()
{
    float calc_select, again;

    double feet, gallons, miles, meters, liters, kilometers;

    
start:
    cout << "What do you want to convert?\n";
    cout << "\n\n Enter:\n";
    cout <<  " 1 for feet to meters,\n";
    cout <<  " 2 for gallons to liters,\n";
    cout <<  " 3 for miles to kilometers.\n";
	cout <<  " Select now: ";
    cin>> calc_select;
	cin.ignore();
    if (calc_select == 1)
	{
		cout<< " Enter a number of feet to convert to meters: ";
        cin>> feet;
		cin.ignore();
        meters = (feet * 0.3048);
		cout << "\n";
        cout  <<feet<< " feet equals "  <<meters<< " meters.\n";
		goto start;
	}
            
    else if (calc_select == 2)
	{
		cout<< " Enter a number of gallons to convert to liters: ";
        cin>> gallons;
		cin.ignore();
        liters = (gallons * 3.78541178);
		cout << "\n";
        cout  <<gallons<< " gallons equals "  <<liters<< " liters.\n";
		goto start;
	}
            
    else if (calc_select == 3)
	{
		cout<< " Enter a number of miles to convert to meters: ";
        cin>> miles;
		cin.ignore();
        kilometers = (miles * 1.609344);
		cout << "\n";
        cout <<miles<< " miles equals "  <<kilometers<< " kilometers.\n";
		goto start;
	}

	else
	{
        cout<< " Invalid selection!  Must enter a 1, 2, 3!\n";
	}

	cin.get();
}



I initially had the "switch" and "break" in there but I couldn't get it to work. I tried it without, but it didn't work quite right either. Thank you for explaining the changes you made. How do I make this go back to the original menu and let the user make another selection?

View Postsensui, on 1 Sep, 2008 - 08:38 AM, said:

If you want to use the switch instruction try the following code ( and read the comments ):
#include <iostream>
using namespace std;

int main() {
    //declare variables for calculation selections
    int calc_select; //this must be an int not a float if you want to work with switch

    //define variables
    double feet, gallons, miles, meters, liters, kilometers;


    //Ask user to choose
    cout<< "\n\n What do you want to convert?\n";
    cout<< "\n\n Enter:\n";
    cout<<  " 1 for feet to meters,\n";
    cout<<  " 2 for gallons to liters,\n";
    cout<<  " 3 for miles to kilometers.\n";
    //store selection
    cin >> calc_select;
    switch ( calc_select ) {
    case 1: //if calc_select == 1
        cout<< "Enter a number of feet to convert to meters.\n";
        cin>> feet;
        meters = (feet * 0.3048);
        cout  <<feet<< " feet equals "  <<meters<< " meters.\n";
        break; //you must use break if you don't want to execute the instructions
        //for case 2:

    case 2: //if calc_select == 2
        cout<< "Enter a number of gallons to convert to liters.\n";
        cin>> gallons;
        liters = (gallons * 3.78541178);
        cout  <<gallons<< " gallons equals "  <<liters<< " liters.\n";
        break;

    case 3: //calc_select == 3
        cout<< "Enter a number of miles to convert to meters.\n";
        cin>> miles;
        kilometers = ( miles * 1.609344 );
        cout << miles << " miles equals " << kilometers << " kilometers.\n";
        break;
    default: ////calc_select == invalid
        cout << "Invalid selection!  Must enter a 1, 2, 3!\n";

    };

    cin.get();
    return EXIT_SUCCESS;

}



I hope this will help you.

Was This Post Helpful? 0
  • +
  • -

#5 smartin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-August 08

Re: If and/or else?

Posted 03 September 2008 - 05:36 PM

[I hope this is not a stupid question...but... what does "cin.ignore();" do in this program. I have never seen that or "cin.get();". Can you please explain what that does? Thanks so much!


quote name='sensui' date='1 Sep, 2008 - 08:38 AM' post='410821']
If you want to use the switch instruction try the following code ( and read the comments ):
#include <iostream>
using namespace std;

int main() {
    //declare variables for calculation selections
    int calc_select; //this must be an int not a float if you want to work with switch

    //define variables
    double feet, gallons, miles, meters, liters, kilometers;


    //Ask user to choose
    cout<< "\n\n What do you want to convert?\n";
    cout<< "\n\n Enter:\n";
    cout<<  " 1 for feet to meters,\n";
    cout<<  " 2 for gallons to liters,\n";
    cout<<  " 3 for miles to kilometers.\n";
    //store selection
    cin >> calc_select;
    switch ( calc_select ) {
    case 1: //if calc_select == 1
        cout<< "Enter a number of feet to convert to meters.\n";
        cin>> feet;
        meters = (feet * 0.3048);
        cout  <<feet<< " feet equals "  <<meters<< " meters.\n";
        break; //you must use break if you don't want to execute the instructions
        //for case 2:

    case 2: //if calc_select == 2
        cout<< "Enter a number of gallons to convert to liters.\n";
        cin>> gallons;
        liters = (gallons * 3.78541178);
        cout  <<gallons<< " gallons equals "  <<liters<< " liters.\n";
        break;

    case 3: //calc_select == 3
        cout<< "Enter a number of miles to convert to meters.\n";
        cin>> miles;
        kilometers = ( miles * 1.609344 );
        cout << miles << " miles equals " << kilometers << " kilometers.\n";
        break;
    default: ////calc_select == invalid
        cout << "Invalid selection!  Must enter a 1, 2, 3!\n";

    };

    cin.get();
    return EXIT_SUCCESS;

}



I hope this will help you.
[/quote]
Was This Post Helpful? 0
  • +
  • -

#6 MorphiusFaydal   User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: If and/or else?

Posted 03 September 2008 - 05:56 PM

cin.get() just waits for a key press. cin.ignore() ignores the first entry in the key buffer.
Was This Post Helpful? 1
  • +
  • -

#8 sensui   User is offline

  • D.I.C Head
  • member icon

Reputation: 30
  • View blog
  • Posts: 146
  • Joined: 24-August 08

Re: If and/or else?

Posted 04 September 2008 - 02:14 AM

You can put the menu and the switch statement in a do while loop like this:
do {
        //Ask user to choose
        cout<< "\n\n What do you want to convert?\n";
        cout<< "\n\n Enter:\n";
        cout<<  " 1 for feet to meters,\n";
        cout<<  " 2 for gallons to liters,\n";
        cout<<  " 3 for miles to kilometers.\n";
        //store selection
        cin >> calc_select;
        switch ( calc_select ) {
        case 1: //if calc_select == 1
            cout<< "Enter a number of feet to convert to meters.\n";
            cin>> feet;
            meters = (feet * 0.3048);
            cout  <<feet<< " feet equals "  <<meters<< " meters.\n";
            break; //you must use break if you don't want to execute the instructions
            //for case 2:

        case 2: //if calc_select == 2
            cout<< "Enter a number of gallons to convert to liters.\n";
            cin>> gallons;
            liters = (gallons * 3.78541178);
            cout  <<gallons<< " gallons equals "  <<liters<< " liters.\n";
            break;

        case 3: //calc_select == 3
            cout<< "Enter a number of miles to convert to meters.\n";
            cin>> miles;
            kilometers = ( miles * 1.609344 );
            cout << miles << " miles equals " << kilometers << " kilometers.\n";
            break;
        default: ////calc_select == invalid
            cout << "Invalid selection!  Must enter a 1, 2, 3!\n";

        };
    } while ( 1 );


Note that to end your application you must click the x button on top right of the window.
Or, instead of while( 1 ) you can use a sentinel value to stop the program like this:
int k = 0;
do {
  //...
  k++;
} while( k < 10 );


Was This Post Helpful? 1

#9 smartin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-August 08

Re: If and/or else?

Posted 04 September 2008 - 03:47 AM

Thank you so much for your suggestions!!! Does the sentinel value let the program run until the number of times you enter? 10 in the example?
The "do while" loop seems to be what I am looking for. Thanks again for your help!

View Postsensui, on 4 Sep, 2008 - 02:14 AM, said:

You can put the menu and the switch statement in a do while loop like this:
do {
        //Ask user to choose
        cout<< "\n\n What do you want to convert?\n";
        cout<< "\n\n Enter:\n";
        cout<<  " 1 for feet to meters,\n";
        cout<<  " 2 for gallons to liters,\n";
        cout<<  " 3 for miles to kilometers.\n";
        //store selection
        cin >> calc_select;
        switch ( calc_select ) {
        case 1: //if calc_select == 1
            cout<< "Enter a number of feet to convert to meters.\n";
            cin>> feet;
            meters = (feet * 0.3048);
            cout  <<feet<< " feet equals "  <<meters<< " meters.\n";
            break; //you must use break if you don't want to execute the instructions
            //for case 2:

        case 2: //if calc_select == 2
            cout<< "Enter a number of gallons to convert to liters.\n";
            cin>> gallons;
            liters = (gallons * 3.78541178);
            cout  <<gallons<< " gallons equals "  <<liters<< " liters.\n";
            break;

        case 3: //calc_select == 3
            cout<< "Enter a number of miles to convert to meters.\n";
            cin>> miles;
            kilometers = ( miles * 1.609344 );
            cout << miles << " miles equals " << kilometers << " kilometers.\n";
            break;
        default: ////calc_select == invalid
            cout << "Invalid selection!  Must enter a 1, 2, 3!\n";

        };
    } while ( 1 );


Note that to end your application you must click the x button on top right of the window.
Or, instead of while( 1 ) you can use a sentinel value to stop the program like this:
int k = 0;
do {
  //...
  k++;
} while( k < 10 );


Was This Post Helpful? 0
  • +
  • -

#10 RedSonja   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 172
  • Joined: 04-September 08

Re: If and/or else?

Posted 04 September 2008 - 03:57 AM

View Postsmartin, on 1 Sep, 2008 - 08:04 AM, said:

I am trying to write a simple program that will let you choose from 3 metric conversions.
I don't know what I am doing wrong. It will only let you choose the first option and then what ever is entered after first calculation continues to the next two calculations. I don't know what is missing. I am at "beginner" at best. Any suggestions would be appreciated.
#include <iostream>
using namespace std;

int main()
{
	//declare variables for calculation selections
	float calc_select, again;

	//define variables
	double feet, gallons, miles, meters, liters, kilometers;

	
	//Ask user to choose
	cout<< "\n\n What do you want to convert?\n"; 
	cout<< "\n\n Enter:\n"; 
	cout<<  " 1 for feet to meters,\n";
	cout<<  " 2 for gallons to liters,\n";
	cout<<  " 3 for miles to kilometers.\n";
	//store selection
	cin>> calc_select;
	//switch (calc_select);
	{
		//if calc_select == 1
		if (calc_select == 1)
			cout<< " Enter a number of feet to convert to meters.\n";
			cin>> feet;
			meters = (feet * 0.3048);
			cout  <<feet<< " feet equals "  <<meters<< " meters.\n";
			
		//if calc_select = 2
		if (calc_select == 2)
			cout<< " Enter a number of gallons to convert to liters.\n";
			cin>> gallons;
			liters = (gallons * 3.78541178);
			cout  <<gallons<< " gallons equals "  <<liters<< " liters.\n";
			
		//if calc_select = 3
		if (calc_select == 3)
			cout<< " Enter a number of miles to convert to meters.\n";
			cin>> miles;
			kilometers = (miles * 1.609344);
			cout <<miles<< " miles equals "  <<kilometers<< " kilometers.\n";
			
		//if calc_select = invalid
		
		
		
			//cout<< " Invalid selection!  Must enter a 1, 2, 3!\n";
			
		}
	

}



You forgot the curly brackets {}, so it just carried on into the next if. I would have used if else, and a final else to catch the cases when the user does something unexpected.
Was This Post Helpful? 0
  • +
  • -

#11 sensui   User is offline

  • D.I.C Head
  • member icon

Reputation: 30
  • View blog
  • Posts: 146
  • Joined: 24-August 08

Re: If and/or else?

Posted 04 September 2008 - 04:17 AM

View Postsmartin, on 4 Sep, 2008 - 03:47 AM, said:

Does the sentinel value let the program run until the number of times you enter? 10 in the example?

Yes! I'm glad I could help you.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1