Multiple choice conditional

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-16-2011
SpudCake
Multiple choice conditional
Hello

I'm trying to create a calculator that takes a keyboard-entered variable, followed by a choice of variable, a value for that variable, then a calculation, using one of two expressions. The expression used depends on the chosen variable.

It should read something like this:

Please enter a mass
5
Will you enter [S]peed or [M]omentum?
s
Please enter the speed
10
A mas of 5kg with speed of 10m/s has kinetic energy of 250J.

The program I have so far works until the calculation, where I get a runtime error. If I chose to enter a speed, it says the variable representing momentum is being used without being initialised. If I chose momentum, the speed variable gets the same error. I've tried multiple variations, but it keeps happening.

Code:

```#include <iostream> using namespace std; int main () {     //variable declarations and initialisation     double en_k1;     double en_k2;     double mass;     double vel;     double mom;     //prompt and read mass     cout << "Please enter the mass (kg):" << endl;     cin >> mass;     //prompt for choice of speed or momentum     cout << "Are you going to enter a [S]peed or a [M]omentum?" << endl;     char choice;     cin >> choice;     switch (choice){         case 's':         case 'S':             cout << "Please enter the speed (m/s)." << endl;             cin >> vel;             break;         case 'm':         case 'M':             cout << "Please enter the momentum (kgm/s)" << endl;             cin >> mom;                break;         default:                        cout << "You entered a mass of " << mass << "kg, but your choice of speed or momentum was not a valid choice." << endl;     }         //conditional read and calculate energy     if (vel){         en_k1 = 0.5 * mass * (vel * vel);     }     else if (mom){         en_k2 = mom / (2 * mass);     }             //print result     cout << "A mass of " << mass << "kg with a speed of " << vel << "m/s has a kinetic energy of " << en_k1 << "J." << endl;     cout << "A mass of " << mass << "kg with a momentum of " << mom << "kgm/s has a kinetic energy of " << en_k2 << "J." << endl;                 //spit if invalid input     //sign off politely     cout << "Thank you for using the kinetic energy calculator." << endl; }            //end of main()```
I don't understand what is wrong with it, and I'm not sure if I'm linking the expressions to the statements correctly..

Could anyone offer any help?

Thanks.
• 10-16-2011
jimblumberg
In the following snippet:
Code:

```     if (vel){         en_k1 = 0.5 * mass * (vel * vel);     }     else if (mom){         en_k2 = mom / (2 * mass);     }```
The only time you initialize, assign a value to, the variables en_k1 and en_k2 is inside these if statements. Therefore if you try to use both of these variables one will not be initialized. You should only print out the value that you calculated, not the other.

Also because vel and mom are floating point variables using them in these if statements can cause problems, you are trying to turn a floating point number into a bool value. You probably should do the calculations and printout inside your switch statement.

Jim
• 10-16-2011
Elysia
The problem is that you think

if (vel)

will determine if the user has inputted any value into vel, which is wrong.
You cannot determine if a variable has been assigned any value or not. Well, to be precise, there is no built-in method for doing it.
You need to either revise your logic, or to find a way to detect if a value has been assigned to a variable or not. Examples of how to do this is to, say, assign a non-valid default value to those variables. Then you know that if the user has entered a value, then those variables contain a valid value; otherwise they don't.
• 10-16-2011
SpudCake
Thanks for the replies.

I've got the calculator working now, by doing the calculations within the switch. It works exactly as it should, but I'm not sure whether those sentences after the // are compulsory or for guidance. Even though it works, it may still be wrong. I'll submit it and see what happens.

Now I have to work out how to loop it, so I may be back.

Thank you very much for your help. I wish I got this kind of detail on my course.
• 10-16-2011
SpudCake
Yep, I'm back.

I need to loop the calculator now, asking whether or not to carry out another calculation after each one, with the loop ending only when the user wants it to.

For example:

Please enter the mass
8
[S]peed or [M]omentum?
s
Please enter the speed
4
A mass of 8 at a speed of 4 has ...
Do you wish to perform another calculation ([Y]es or [N]o)?
y
Please enter the mass
2
[S]peed or [M]omentum?
m
Please enter the momentum
9
A mass of 2 with momentum of 9 has ...
Do you wish to perform another calculation ([Y]es or [N]o)?
n

I think it needs to loop if the answer is y or Y, but I don't have any idea how to loop that. I only know how to loop things that have been initialised.

Any ideas? :D
• 10-16-2011
Elysia
Can you start with a simple example?
Formalize code from this:

Ask the user if (s)he wants to quit. The input shall be 'y' if the user wants to quit.
If the user does not want to quit, then repeat this process.
• 10-16-2011
SpudCake
Something like this?

Code:

```#include <iostream> using namespace std; int main () {     int y;     int n;     int choice;     cout << "Do you wish to quit ([y]es or [n]o)?" << endl;     cin >> choice;     for( choice = y;){     } }```
Maybe I don't understand how to loop at all. :frown:
• 10-16-2011
Elysia
It's a step in the right direction, but not quite right.
Let's read the code and see what it does.

Ask user: "Do you wish to quit ([y]es or [n]o)?"
Loop until forever

Why does it loop forever? Because you haven't put any condition to stop it from looping.
Going back to my example:

Ask the user if (s)he wants to quit. The input shall be 'y' if the user wants to quit.
If the user does not want to quit, then repeat this process.

From the last, we see that if the user does not type 'y', then we should repeat this process.
One can translate this into a logical sentence: "Repeat the process until the user enters 'y'," or "Repeat the process while the user enters 'y'."
If you can't grasp it, try taking a look at flowcharts. They're great beginner tools.
• 10-16-2011
SpudCake
I've got this:

Code:

``` #include <iostream> using namespace std; int main () {     char choice = 0;     while(choice != 'y'){     cout << "Do you wish to quit? Type 'y' to quit" << endl;     cin >> choice;         } }            //end of main()```
If I type anything but y, it repeats the question. If I type y, it tells me to press any key to continue, which is what I get at the end of all my programs.
• 10-16-2011
Elysia
みごとだ (well done). You've implemented a basic loop.
Now, can you apply this knowledge to your calculator?
• 10-16-2011
SpudCake
I've almost got it working in a different calculator. It follows a similar trend.

Code:

```#include <iostream> using namespace std; int main(){     const double mol = 1.0;     const double gas_const = 8.315;     double pres1;     double pres2;     double vol;     double temp;     char choice1 = 0;     while( choice1 != 'y'){     cout << "\n Do you wish to enter a temperature and volume? ([Y]es or [N]o)" << endl;     char choice1;     cin >> choice1;     switch (choice1){         case 'y':         case 'Y':             cout << "\n Is the temperature in [K]elvin or [C]elsius?" << endl;             char choice2;             cin >> choice2;             switch (choice2){                 case 'k':                 case 'K':                     cout << "\n Please enter the temperature (K)." << endl;                     cin >> temp;                     cout << "\n Please enter the volume (m^3)." << endl;                     cin >> vol;                     pres1 = (mol * gas_const * temp) / vol;                 cout << "\n One mole of gas at " << temp << "K, in a volume of " << vol << "m^3, has a pressure of " << pres1 << "J." << endl;                 break;                 case 'c':                 case 'C':                     cout << "\n Please enter the temperature (C)." << endl;                     cin >> temp;                     cout << "\n Please enter the volume (m^3)." << endl;                     cin >> vol;                     pres2 = (mol * gas_const * (temp + 273)) / vol;                 cout << "\n One mole of gas at " << temp << "C, in a volume of " << vol << "m^3, has a pressure of " << pres2 << "J." << endl;             }                 break;         case 'n':         case 'N':; }     }             cout << "Thank you for using my ideal gas pressure calculator." << endl; }```
If I enter y or Y, I go through the loop of entering variables and choosing units. If I enter n or N, it repeats the first question. I haven't worked out how to break the loop and get the final comment if I enter n or N.
• 10-16-2011
Elysia
What do you want it to do?
Also, I think now is a good idea to figure out how to do proper indentation.
• 10-16-2011
SpudCake
Its supposed to keep going through the process of choosing variables and calculating until I say I don't want to enter anything.

Do you wish to enter a temperature and volume ([Y]es or [N]o)?
y
Is the temperature in [K]elvin or [C]elsius?
c
Please enter a temperature (C)
6
Please enter a volume (m^3)
5
Gas at temp with vol has ...
Do you wish to enter a temperature and volume ([Y]es or [N]o)?

If I choose to enter something, I go through the process to the calculation, where I'm asked again. If I choose not to enter anything, I'm asked again straight away.

So there is a standard for indentation? I wasn't aware of that, as you can tell.
• 10-16-2011
Elysia
What does the loop say? Loop while choice != 'y'.
There is no standard to style, but there are some well known ones. You'll probably find lots of info by googling a little (google indentation).
• 10-16-2011
SpudCake
Ah. I've made a couple of changes, and it behaves differently, but still doesn't end.

Code:

```#include <iostream> using namespace std; int main(){     const double mol = 1.0;     const double gas_const = 8.315;     double pres1;     double pres2;     double vol;     double temp;     char choice1 = 0;     while( choice1 != 'n'){     cout << "\n Do you wish to enter a temperature and volume? ([Y]es or [N]o)" << endl;     char choice1;     cin >> choice1;     switch (choice1){         case 'y':         case 'Y':             cout << "\n Is the temperature in [K]elvin or [C]elsius?" << endl;             char choice2;             cin >> choice2;             switch (choice2){                 case 'k':                 case 'K':                     cout << "\n Please enter the temperature (K)." << endl;                     cin >> temp;                     cout << "\n Please enter the volume (m^3)." << endl;                     cin >> vol;                     pres1 = (mol * gas_const * temp) / vol;                 cout << "\n One mole of gas at " << temp << "K, in a volume of " << vol << "m^3, has a pressure of " << pres1 << "J." << endl;                 break;                 case 'c':                 case 'C':                     cout << "\n Please enter the temperature (C)." << endl;                     cin >> temp;                     cout << "\n Please enter the volume (m^3)." << endl;                     cin >> vol;                     pres2 = (mol * gas_const * (temp + 273)) / vol;                 cout << "\n One mole of gas at " << temp << "C, in a volume of " << vol << "m^3, has a pressure of " << pres2 << "J." << endl;                 break; }         case 'n':         case 'N':; }     }             cout << "Thank you for using my ideal gas pressure calculator." << endl; }```
Now I get the ending message if I choose not to enter anything, but I then get asked if I want to enter something.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last