Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,299 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,450 people online right now. Registration is fast and FREE... Join Now!




If and/or else?

 
Reply to this topicStart new topic

If and/or else?, when do i use If and/or else?

smartin
1 Sep, 2008 - 07:04 AM
Post #1

New D.I.C Head
*

Joined: 30 Aug, 2008
Posts: 18


My Contributions
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.
CODE

#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";
            
        }
    

}

User is offlineProfile CardPM
+Quote Post

Korupt
RE: If And/or Else?
1 Sep, 2008 - 07:22 AM
Post #2

D.I.C Head
Group Icon

Joined: 22 Jun, 2008
Posts: 58



Thanked: 1 times
Dream Kudos: 25
My Contributions
try this (also added a throughout loop with the start: label and the goto statement)

cpp
#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();
}

User is offlineProfile CardPM
+Quote Post

sensui
RE: If And/or Else?
1 Sep, 2008 - 07:38 AM
Post #3

D.I.C Head
Group Icon

Joined: 24 Aug, 2008
Posts: 132



Thanked: 20 times
Dream Kudos: 50
My Contributions
If you want to use the switch instruction try the following code ( and read the comments ):
cpp
#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.
User is offlineProfile CardPM
+Quote Post

smartin
RE: If And/or Else?
3 Sep, 2008 - 04:28 PM
Post #4

New D.I.C Head
*

Joined: 30 Aug, 2008
Posts: 18


My Contributions
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?

QUOTE(Korupt @ 1 Sep, 2008 - 08:22 AM) *

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

cpp
#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?

QUOTE(sensui @ 1 Sep, 2008 - 08:38 AM) *

If you want to use the switch instruction try the following code ( and read the comments ):
cpp
#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.


User is offlineProfile CardPM
+Quote Post

smartin
RE: If And/or Else?
3 Sep, 2008 - 04:36 PM
Post #5

New D.I.C Head
*

Joined: 30 Aug, 2008
Posts: 18


My Contributions
[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 ):
cpp
#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]

User is offlineProfile CardPM
+Quote Post

MorphiusFaydal
RE: If And/or Else?
3 Sep, 2008 - 04:56 PM
Post #6

D.I.C Lover
Group Icon

Joined: 12 May, 2005
Posts: 1,104



Thanked: 8 times
Expert In: Hardware, Networking

My Contributions
cin.get() just waits for a key press. cin.ignore() ignores the first entry in the key buffer.
User is offlineProfile CardPM
+Quote Post

sensui
RE: If And/or Else?
4 Sep, 2008 - 01:14 AM
Post #7

D.I.C Head
Group Icon

Joined: 24 Aug, 2008
Posts: 132



Thanked: 20 times
Dream Kudos: 50
My Contributions
You can put the menu and the switch statement in a do while loop like this:
cpp
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:
cpp
int k = 0;
do {
//...
k++;
} while( k < 10 );

User is offlineProfile CardPM
+Quote Post

smartin
RE: If And/or Else?
4 Sep, 2008 - 02:47 AM
Post #8

New D.I.C Head
*

Joined: 30 Aug, 2008
Posts: 18


My Contributions
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!

QUOTE(sensui @ 4 Sep, 2008 - 02:14 AM) *

You can put the menu and the switch statement in a do while loop like this:
cpp
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:
cpp
int k = 0;
do {
//...
k++;
} while( k < 10 );



User is offlineProfile CardPM
+Quote Post

RedSonja
RE: If And/or Else?
4 Sep, 2008 - 02:57 AM
Post #9

D.I.C Head
Group Icon

Joined: 4 Sep, 2008
Posts: 170



Thanked: 3 times
Dream Kudos: 25
My Contributions
QUOTE(smartin @ 1 Sep, 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.
CODE

#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.
User is offlineProfile CardPM
+Quote Post

sensui
RE: If And/or Else?
4 Sep, 2008 - 03:17 AM
Post #10

D.I.C Head
Group Icon

Joined: 24 Aug, 2008
Posts: 132



Thanked: 20 times
Dream Kudos: 50
My Contributions
QUOTE(smartin @ 4 Sep, 2008 - 03:47 AM) *

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.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 06:11AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month