Thread: Multiple choice conditional

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is that not what you said you wanted before?
    If not, then you need to specify exactly how you intend your program to work.
    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.

  2. #17
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Sorry. I'm starting to get confused now.

    If I choose to enter values, I go through the process of entering them and calculating the answer. Once I've done that I'm asked again to enter values. If I choose not to enter anything, I'm supposed to get the thank you message and no more questions, effectively ending the program.

    I'd just like to thank you for your patience with me. You've been a great help, and I'm learning alot from you.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Right.
    Well, look at your logic right now.
    It's currently:

    Code:
    While user wants to input values
    	Do you want to enter values? [Y/N]
    	Ask for values
    	Calculate
    Loop
    You probably want:
    Code:
    While user wants to input values
    	Ask for values
    	Calculate
    	Do you want to enter values? [Y/N]
    Loop
    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. #19
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    The instructions I have say I need to start with the question.

    Its late here, so I'll have to pick this up tomorrow. Thanks again for all your help, and take care.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are many ways to formulate logic. Another version might be:

    Code:
    While user wants to input values
        Do you want to enter values? [Y/N]
        If user wants to enter values
            Ask for values
            Calculate
    Loop
    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.

  6. #21
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Code:
    #include <iostream>
    using namespace std;
    
    //Global Values are bad and you shouldn't use them
    //I'm using them b/c it is easyer to help you understand what I've done.
    const double mol = 1.0;
    const double gas_const = 8.315;
    double pres1;
    double pres2;
    double vol;
    double temp;
    
    //I have moved your caculation code to its own function to help clarifity your main loop.
    
    void Calculate(){ //sorry for my spelling.
    //Please note that one tab has been placed to denote your inside a function.
    	cout << "\n Is the temperature in [K]elvin or [C]elsius?" << endl;
    	char choice2;
    	cin >> choice2;
    	switch (choice2){
    	//Please note that another tab has been added to denote the switch statement
    		case 'k':
    		case 'K':
    		//And another tab has been added to show you have entered a logic sequence.
    			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;
    		//As you exit the logic sequence you subtract a tab space.
    		break;
    		case 'c':
    		case 'C':
    		//Added another tab for the logic sequence
    			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;
    		//Deleted the tab as we exit this logic sequence.
    		break;
    	}
    }
    
    
    int main(){
        char choice1 = 0;  //Please remimber this line.
        while( choice1 != 'n'){
    		cout << "\n Do you wish to enter a temperature and volume? ([Y]es or [N]o)" << endl;
    		char choice1;  //Please look at line 53, this is why you can not exit your program.
    		cin >> choice1;
    		switch (choice1){
    			case 'y':
    			case 'Y':
    				Calculate();	
    			case 'n':
    			case 'N':;
    		}
    	}
        cout << "Thank you for using my ideal gas pressure calculator." << endl;
    }
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  7. #22
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Hi

    Sorry I didn't get back sooner. I've been really busy/distracted.

    I didn't manage to get the pressure calculation working until Nor's code, so thank you for that. I did however manage to get the main assessment done using Elysia's help, so thank you for that. I got 100% of the marks.

    You've both been a great help, and I've learned alot.

    Thank you very much.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well done. That's great.
    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.

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, 10: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