Thread: Multiple choice conditional

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    ‡ †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

  2. #2
    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.

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