Thread: cases in c++

  1. #16
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    is this better

    Code:
     #ifndef _FUNCTIONS_H_
    #define _FUNCTIONS_H_
    
    class BaseFunction
    {
    private:
    
    protected:
    
    public:
        BaseFunction();
        virtual int Menu() = 0;
    };
    
    class Functions : public BaseFunction
    {
    private:
    
    public:
        Functions();
        void kg_to_lbs();
        void c_to_k();
        void tsp_to_ml();
        void m_table();
        void s_table();
        virtual int Menu() override;
    };
    Code:
     #include "stdafx.h"
    #include "Functions.h"
    #include <iostream>
    using namespace std;
    
    BaseFunction::BaseFunction()
    {
        cout << "Constructing base class..." << endl;
    }
    
    Functions::Functions()
    {
        cout << "Constructing derived class..." << endl;
    }
    
    void Functions::kg_to_lbs()
    {
        cout << "changing kilograms to pounds" << endl;
    }
    
    void Functions::c_to_k()
    {
        cout << "changing celsius to degrees" << endl;
    }
    
    void Functions::tsp_to_ml()
    {
        cout << "changing teaspoons to millilitres" << endl;
    }
    
    void Functions::m_table()
    {
        cout << "multiplying" << endl;
    }
    
    void Functions::s_table()
    {
        cout << "squaring" << endl;
    }
    
    
    
    int Functions::Menu()
    {
        int opt;
        do {
            for (int i = 1; i <= 5; i++)
                cout << "Function " << i << " [" << i << ']' << endl;
            cout << "Quit [0]\nEnter Option: ";
            cin >> opt;
        } while (opt < 0 || opt > 5);
        return opt;
    }
    Code:
     #include "stdafx.h"
    #include <iostream>
    #include "Functions.h"
    
    using namespace std;
    
    int main()
    {
        int opt;
        Functions *f = new Functions();
        do {
            opt = f->Menu();
            switch (opt)
            {
            case 1:
                f->kg_to_lbs();    break;
            case 2:
                f->c_to_k();    break;
            case 3:
                f->tsp_to_ml();    break;
            case 4:
                f->m_table();    break;
            case 5:
                f->s_table();    break;
            }
        } while (opt != 0);
    }

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    That's a lot better actually. But you are still only about half done! The menu function still hasn't been changed to plainly state the options the user has. Plus, you need to change the rest of the functions so that when they are called, they take the users input, do the math, and display the answer.

    The conversion functions should be easy because they are simple operations: for kg to pounds and teaspoons to mils, all you do is multiply a constant. For C to kelvin, all you do is add a constant.

    The multiplication and square tables are a bit different, but again, with a basic understanding of loops, you should be able to fill row and column with answers.
    Last edited by whiteflags; 03-02-2018 at 03:16 PM.

  3. #18
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by whiteflags View Post
    I'm really not sure what to say. I'm also tempted to call this approach over-engineered for a beginner.

    It feels like an array is getting preferential treatment just because the names were originally enumerated. A static class keeps everything as organized as an array would. The best thing that could be said for an array of function pointers in this program is that it supplants a switch. Personally, I find the switch to be clearer code.

    It's not the worst idea, I just don't think it's something people need to learn early on. At least I suspect that sonicflare9 hasn't been at this very long.
    I suggested changing to an array because of all the stylistic issues with the code, I felt that "Don't repeat yourself" was the most important one to point out. Also, I would expect not function pointers, but function objects, derived from BaseFunction. Sonicflage9's code is already half way to doing it this way. That's another reason I made my suggestion: where ever sonicflage9 got the idea to use "Basefunction" from, seemed to have an array of polymorphic objects in mind.

    But sonicflage9 has implemented it as a you suggested. Which is great. @sonicflage9, doing it this way, you don't need BaseFunction.

    On using static functions, I still disagree that it's a good idea. It does not organize things like an array, it organizes things like a namespace. But for that I suggest a namespace. Which, for sonicflage9's sake, looks like this:
    Code:
    namespace Functions {
        void kg_to_lbs();
        void c_to_k();
        void tsp_to_ml();
        void m_table();
        void s_table();
    };
    
    //use as 
    case 1:
                Functions::kg_to_lbs();    break;
    case 2:
                Functions::c_to_k();    break;
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #19
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    how do i fix the error uninitilized local variable kg used

  5. #20
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    Quote Originally Posted by sonicflare9 View Post
    how do i fix the error uninitilized local variable kg used
    If you're talking about your old code from page one, that question has already been answered; otherwise, post and example. Try to explain to yourself what your functions do step-by-step, as if you had to explain it to someone else; that's a good way to find mistakes.

  6. #21
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    I need help
    Code:
     #ifndef _FUNCTIONS_H_
    #define _FUNCTIONS_H_
    
    #include <iostream>
    #include <string>
    
    enum Options { NORMAL_EXIT, KG_TO_LBS, CELSIUS_TO_KELVIN, TSP_TO_ML, M_TABLE, S_TABLE };
    
    class game
    {
    private:
    
    protected:
    
    public:
        game();
        virtual int Menu() = 0;
    };
    
    class Functions
    {
    public:
        static double GetDouble(const std::string& prompt);
        static int GetInt(const std::string& prompt);
        static double ConvertCelsiusToKelvin(double c);
        static double ConvertKgToLbs(double kg);
        static double ConvertTspToMl(double tsp);
        static void MulTable(int first, int last);
        static void SquareTable(int first, int last, unsigned int step);
        virtual int Menu() override;
    };
    
    #endif
    Code:
     #include "stdafx.h"
    #include <iostream>
    #include "Functions.h"
    
    using namespace std;
    
    int main()
    {
        int opt;
        Functions *f = new Functions();
        do {
            opt = f->Menu();
            switch (opt)
            {
            case 1:
                f->ConvertKgToLbs();    break;
            case 2:
                f->ConvertCelsiusToKelvin();    break;
            case 3:
                f->ConvertTspToMl();    break;
            case 4:
                f->MulTable();    break;
            case 5:
                f->SquareTable();    break;
            }
        } while (opt != 0);
    }
    Code:
     #include "stdafx.h"
    #include "Functions.h"
    #include <iostream>
    using namespace std;
    
    
    double Functions::GetDouble(const std::string & prompt)
    {
        return 0.0;
    }
    
    int Functions::GetInt(const std::string & prompt)
    {
        return 0;
    }
    
    int Functions::Menu()
    {
        int opt;
        do {
            for (int i = 1; i <= 5; i++)
                cout << "Function " << i << " [" << i << ']' << endl;
            cout << "Quit [0]\nEnter Option: ";
            cin >> opt;
        } while (opt < 0 || opt > 5);
        return opt;
    }
    
    double Functions::ConvertCelsiusToKelvin(double c)
    {
        return 0.0;
    }
    
    double Functions::ConvertKgToLbs(double kg)
    {
        return 0.0;
    }
    
    double Functions::ConvertTspToMl(double tsp)
    {
        return 0.0;
    }
    
    void Functions::MulTable(int first, int last)
    {
    }
    
    void Functions::SquareTable(int first, int last, unsigned int step)
    {
    }

  7. #22
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    So you're back to square one? Start by implementing the functions. For instance ConvertCelsiusToKelvin should be trivial to figure out. Don't be a leech, get started!

  8. #23
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    how do i fix too few arguments in function call

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,560
    Quote Originally Posted by sonicflare9
    how do i fix too few arguments in function call
    • Call the function with sufficient arguments, or
    • Change the function definition to have the necessary number of parameters to match the call (if it turns out that you wrote the function with unused parameters)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    can i have an example

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,560
    Sure, here is a program with a negative example. Fix it:
    Code:
    #include <iostream>
    
    void foo(int n)
    {
        std::cout << n << std::endl;
    }
    
    int main()
    {
        foo(); // prints 123
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    #include <iostream>
    Code:
    void foo(int n)
    {
        std::cout << n << std::endl;
    }
    
    int main()
    {
        foo(123); // prints 123
    }
    so all i have to do is add numbers in the brackets?

    how can i make the 1-5 work
    Last edited by sonicflare9; 03-09-2018 at 02:44 PM.

  13. #28
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    Code:
     #include "stdafx.h"
    #include "Functions.h"
    #include <iostream>
    using namespace std;
    
    double kg;
    double lb;
    
    double Functions::GetDouble(const std::string & prompt)
    {
        return 0;
    }
    
    int Functions::GetInt(const std::string & prompt)
    {
        return 0;
    }
    
    double Functions::ConvertCelsiusToKelvin(double c)
    {
        return 0.0;
    }
    
    double Functions::ConvertKgToLbs(double kg)
    {
        return 0.0;
    }
    
    double Functions::ConvertTspToMl(double tsp)
    {
        return 0.0;
    }
    
    void Functions::MulTable(int first, int last)
    {
    }
    
    void Functions::SquareTable(int first, int last, unsigned int step)
    {
    }
    
    int Functions::Menu()
    {
        double kg, lb;
        kg = 50;
        cout << "this will maybe convert kilograms to pounds.\n";
        cout << "please enter weight in kilograms" ;
        cin >> lb;
        lb = kg * 2.2;
        cout << "The weight in kilograms is: " << kg << '\n';
        
        int opt;
        do {
            for (int i = 1; i <= 5; i++)
                cout << "Function " << i << " [" << i << ']' << endl;
            cout << "Quit [0]\nEnter Option: ";
            cin >> opt;
        } while (opt < 0 || opt > 5);
        return opt;
    }
    Code:
     #ifndef _FUNCTIONS_H_
    #define _FUNCTIONS_H_
    
    #include <iostream>
    #include <string>
    
    enum Options { NORMAL_EXIT, KG_TO_LBS, CELSIUS_TO_KELVIN, TSP_TO_ML, M_TABLE, S_TABLE };
    
    class game
    {
    private:
    
    protected:
    
    public:
        game();
        virtual int Menu() = 0;
    };
    
    class Functions
    {
    public:
        static double GetDouble(const std::string& prompt);
        static int GetInt(const std::string& prompt);
        static double ConvertCelsiusToKelvin(double c);
        static double ConvertKgToLbs(double kg);
        static double ConvertTspToMl(double tsp);
        static void MulTable(int first, int last);
        static void SquareTable(int first, int last, unsigned int step);
        virtual int Menu();
    };
    
    #endif
    Code:
     #include "stdafx.h"
    #include <iostream>
    #include "Functions.h"
    
    using namespace std;
    
    int main()
    {
        int opt;
        Functions *f = new Functions();
        do {
            opt = f->Menu();
            switch (opt)
            {
            case 1:
                f->ConvertKgToLbs(100);    break;
            case 2:
                f->ConvertCelsiusToKelvin(100);    break;
            case 3:
                f->ConvertTspToMl(100);    break;
            case 4:
                f->MulTable(12, 12);    break;
            case 5:
                f->SquareTable(1, 2, 3);    break;
            }
        } while (opt != 0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In what cases you HAVE TO use pointer?
    By meili100 in forum C++ Programming
    Replies: 7
    Last Post: 03-04-2008, 01:25 PM
  2. Test cases
    By desertstorm in forum C Programming
    Replies: 2
    Last Post: 05-13-2006, 12:06 AM
  3. cases
    By ZakkWylde969 in forum C++ Programming
    Replies: 2
    Last Post: 07-14-2003, 08:45 PM
  4. cases or ifs?
    By Yoshi in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2002, 07:58 AM
  5. Please help me with cases!!!
    By halifax in forum C Programming
    Replies: 1
    Last Post: 10-14-2001, 01:29 PM

Tags for this Thread