Multiple choice conditional

This is a discussion on Multiple choice conditional within the C++ Programming forums, part of the General Programming Boards category; Hello I'm trying to create a calculator that takes a keyboard-entered variable, followed by a choice of variable, a value ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    16

    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.
    Last edited by SpudCake; 10-16-2011 at 09:50 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    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

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    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.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    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?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    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.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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)?"
    Read user choice
    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.
    Last edited by Elysia; 10-16-2011 at 02:46 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    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. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    みごとだ (well done). You've implemented a basic loop.
    Now, can you apply this knowledge to your calculator?
    Last edited by Elysia; 10-16-2011 at 03:04 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    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.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    What do you want it to do?
    Also, I think now is a good idea to figure out how to do proper indentation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    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.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple-Choice Exam Program
    By micmagicfly in forum C++ Programming
    Replies: 7
    Last Post: 03-26-2010, 01:17 PM
  2. Multiple choice question.
    By ungalnanban in forum C Programming
    Replies: 2
    Last Post: 02-18-2010, 09:40 AM
  3. multiple choice
    By iLike in forum C Programming
    Replies: 6
    Last Post: 10-30-2009, 03:53 PM
  4. Multiple Choice!!!
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-08-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21