Thread: do while loop

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Smile do while loop

    I am trying to run this program just for an exercise. It runs but completely ignores void computation. It goes directly to the line "system ("PAUSE");"
    What could I be missing?
    Thanks..

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	char ans, Y;
    	do
    		{
    		void computation();
    		}
    	while (ans = Y);
    	//wait until user is ready before terminating program to allow the
    	//user to see the program results
    	system ("PAUSE");
    	return 0; 	
    }
    
    void computation()
    {
    	//enter the temperature in celsius
    	int celsius;
    	cout <<"Enter the temperature in celsius: ";
    	cin >> celsius;
    	
    	//calculate conversion factor for celsius to fahrenheit
    	int factor;
    	factor = 212 - 32;
    	
    	//use conversion factor to convert celsius into fahrenheit value
    	int fahrenheit;
    	fahrenheit = factor * celsius/100 + 32;
    
    	//output the results (followed by an Newline)
    	cout <<"Fahrenheit value is: ";
    	cout <<fahrenheit <<endl;
    
    	//choice to input another
    	char ans;
    	cout <<"Do you want to try another one? [Y/N]: ";
    	cin >> ans;
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    1. ans and Y are uninitialized variables.
    2. You are creating a second function computation() inside your do...while loop.
    3. You are using the assignment operator '=' instead of the comparison operator '=='.
    4. Read #1.
    5. system() is evil.
    6. You are creating a new variable called ans in computation() which has nothing to do with the ans variable inside main(). You either need to return its value from computation() or to separate this part of the function from computation().

    Generally, it's good practice to separate IO with computation. Here you are mixing user input, computation and text input. Even worse, you have code related to your do...while loop in your computation() function. A computation function should compute, nothing less and nothing more than that.

  3. #3
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    Code:
    /*   cstdio & cstdlib are not necessary for this program    */
    
    #include <iostream>
    using namespace std;
    
    
    
    
    char computation();    //  Prototype your functions
    
    
    int main()
    {
        
        char ans;
    
    
        do
        {
    
            ans = computation();    //  make computation() return a CHAR
            
            cin.ignore();    //  clear anything leftover in the stream buffer
    
    
        }while((ans != 'N') && (ans != 'n'));    // This is usually more concise than what you had
     
     
     
        
        /*  Use these two lines instead of 
            system("pause");
        */
        cout << "Press [Enter] to continue...";
        cin.ignore(256, '\n');
    
    
    
    
        return 0;
    
    
    }
    
    
    
    
    /*  computation() now returns a CHAR
    */
    char computation()
    {
        
        /*   Declare ALL variables first     */
        char ans;
        
        
        
        
        /*   Work out the conversion here    */
    
    
    
    
        /*  This will protect your program from 
            unwanted/unexpected input
        */
        cout <<"Do you want to try another one? [Y/N]: ";
        do
        {
            
            cin >> ans;
        
        
        }while((ans != 'Y') && (ans != 'y') && (ans != 'N') && (ans != 'n'));
        
        
        return ans;    //  return the char for the do/while loop in main
        
        
    }

  4. #4
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    I find class structures best for these type of programs..well,you need to only compute one function,so its not required

  5. #5
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    While we're at it, it's not good practice to expose the entire std namespace either with:
    Code:
    using namespace std;
    a better alternative is:
    Code:
    using std::cout;
    using std::cin;
    
    /* and if you're using it...
    */
    using std::endl;
    or of course this is always an option as well:
    Code:
    std::cout << "Hello World!" << std::endl;
    
    std::cin >> someVariable;

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by SVXX View Post
    I find class structures best for these type of programs..well,you need to only compute one function,so its not required
    A class to convert Fahrenheit to Celcius? Pointless. A function is perfectly fine.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by sh3rpa View Post
    While we're at it, it's not good practice to expose the entire std namespace either with:
    Code:
    using namespace std;
    a better alternative is:
    Code:
    using std::cout;
    using std::cin;
    
    /* and if you're using it...
    */
    using std::endl;
    or of course this is always an option as well:
    Code:
    std::cout << "Hello World!" << std::endl;
    
    std::cin >> someVariable;
    The latter option is the best, not the second. I always type that extra std:: and never ever use a using declaration unless my code becomes bloated because there are too many std:: 's. Generally, that's a sign that my code is not well designed, though.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Desolation View Post
    The latter option is the best, not the second. I always type that extra std:: and never ever use a using declaration unless my code becomes bloated because there are too many std:: 's. Generally, that's a sign that my code is not well designed, though.
    Using lots of standard language features is "not well designed?" I think you should probably strive for the opposite of what you just said. "Is there a way I can solve this with a standard container/algorithm/function?"

    Leveraging the standard library to maximum effect is necessarily going to pepper your code with a lot of "std::"

    I almost always end up "using boost::bind" because I do a lot of heavily nested algorithmic programming. bind() within bind() within for_each(), etc... It looks horrific until you get rid of all the excess "std::" and "boost::".

    Of course, I only use a "using" declaration inside a function definition, never at module scope. Do it where it makes things cleaner.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    903
    I didn't mean that using the standard library was bad. I always use it and always try to find new algorithms / containers in the standard library so I don't have to implement them myself. What I meant is that if there are like 6-7 std:: 's in a single line, odds are that there is a much simpler solution.

  10. #10
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    Well in either case for such a small and trivial program like this, it's splitting hairs to try and defend one way or the other.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Smile

    Thank you guys for all the input. I am new in C++ and am still familiarizing myself with all the functions and commands, let alone the libraries. But thank you all, I'm learning. I will consider everyone's input until I get this program right.
    Thanks!!

  12. #12
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    here's some overkill, but it might give you something to ponder...
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    using std::cin;
    
    
    class Temp
    {
        public:
    
            void setT(float t){ temperature = t; }
            
            float getC(){ return ( (5.0/9.0) * (temperature - 32.0) ); }
            float getF(){ return ( (9.0/5.0) * temperature + 32.0 ); }
            
        private:
            
            float temperature;
    };
    
    
    int main()
    {   
        
        Temp t;
        char choice;
        float c, f;
        
        
        do
        {
            
            cout << "\nEnter [1] to convert C to F, [2] to convert F to C," << endl
                 << "or [3] to quit: ";     
    
    
            do
            {
                cin >> choice;
            
            }while((choice != '1') && (choice != '2') && (choice != '3'));
            
            
            if(choice == '1')
            {
                cout << "\nEnter degrees in C: ";
                
                cin >> c;
                
                t.setT(c);
                
                cout << c << "C = " << t.getF() << "F" << endl;
            }
            
            
            
            
            if(choice == '2')
            {
                cout << "\nEnter degrees in F: ";
                cin >> f;
                
                t.setT(f);
                
                cout << f << "F = " << t.getC() << "C" << endl;
            }
            
            
        }while(choice != '3');
        
        
        cin.ignore();
        cout << "\nPress [Enter] to continue...";
        cin.ignore(256, '\n');
        
        
        return 0;
    
    
    }

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    sh3rpa, your class has a problem: try this driver
    Code:
    int main()
    {
        Temp t;
        float temp = 60.3;
        t.setT(temp);
        std::cout << "Temperature: " << temp << '\n'
                  << " in Celsius: " << t.getC() << '\n'
                  << " in Fahrenheit: " << t.getF() << '\n'
                  << "Huh?\n";
        return 0;
    }
    As you can see, it is entirely unclear what the value inside the class represents: is it temperature in Fahrenheits or Celsius degrees or something else? For this reason the inner state of this class makes no sense and it just requires more typing to get a simple conversion done than with
    Code:
    float toCelsius(float Fahrenheit);
    float toFahrenheit(float Celsius);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    the value of "temperature" in class "Temp" is controlled through what's written in main.

    There are many ways of creating this a class like this, my example was for illustrative purposes only.

    Notice that "temperature" takes on ONLY the OPPOSITE value (C or F) in main.

    My point was not to create an exhaustive concordance to a celsius->fahrenhiet and vice versa class but to get *alyeska* thinking.

    Thank you anyway.


    I hardly think a celsius->fahrenheit & fahrenheit->celsius program is worthy of test-driven development, but if you'd like to waste your time proving therwise, be my guest.
    Last edited by sh3rpa; 10-03-2007 at 05:04 PM. Reason: blah

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But you'll get him thinking about the wrong things. There are times when objects are a good approach. There are times when they are a bad approach. This case belongs to the latter category.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM