Thread: cases in c++

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    37

    cases in c++

    how do I put in kg to pounds in one case i have one header file and 2 cpp files how can I make it work

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by sonicflare9 View Post
    how do I put in kg to pounds in one case i have one header file and 2 cpp files how can I make it work
    Your question makes no sense to me; you might wish to restate your question.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    like as an option

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I guess you'd do something like this.

    Code:
    enum WeightUnits {
       LBS_TO_KG,
       KG_TO_LBS
    };
    
    WeightUnits GetOptionFromUser(void);
    
    switch ( GetOptionFromUser() )
    {
          case LBS_TO_KG:
             ConvertKgToLbs();
             break;
          case KG_TO_LBS:
             ConvertLbsToKg();
             break;
    };

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    this is my functions header
    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 Function1();
        void Function2();
        void Function3();
        void Function4();
        void Function5();
        virtual int Menu() override;
    };
    
    #endif
    this is my functions cpp
    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::Function1()
    {
        cout << "Invoking Function 1..." << endl;
        double kg, lb;
        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';
    
    }
    
    void Functions::Function2()
    {
        cout << "Invoking Function 2..." << endl;
    }
    
    void Functions::Function3()
    {
        cout << "Invoking Function 3..." << endl;
    }
    
    void Functions::Function4()
    {
        cout << "Invoking Function 4..." << endl;
    }
    
    void Functions::Function5()
    {
        cout << "Invoking Function 5..." << 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;
    }

    this is sampleproject.cpp
    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->Function1();    
                double kg, lb;
                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';
                break;
            case 2:
                f->Function2();    break;
            case 3:
                f->Function3();    break;
            case 4:
                f->Function4();    break;
            case 5:
                f->Function5();    break;
            }
        } while (opt != 0);
    }
    i want 1 to be kilograms to pounds 2 to be celsius to kelvin 3 to be teaspoons to millilitres 4 to be multiplication table and 5 to be a squares table can you help
    Last edited by sonicflare9; 02-28-2018 at 10:45 AM.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Ignoring for a moment all the horrible style, over engineering, and "that's not doing what you think it's doing" problems, the specific big problem is functions.cpp lines 2 through 24, and sampleproject.cpp lines 18 through 22. In both places you are first reading in the pounds, then converting an uninitialized number of kg to pounds, then printing the kilograms. Doing that will not convert kilograms to pounds. A more reasonable set of steps would be to read in kilograms, then convert kilograms to pounds, then print pounds. Also, you don't need to do it in both places.

    There are two many other problems for me to list them all, so here's a basic one set you on the right track, hopefully: You should not have names that contain numbers. If you see yourself tempted to do that, instead create an array. Specifically, create an array of 5 things called "function" instead of Function1 through Function5. Make that work, and you'll have fixed several problems in your code.
    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.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    where should i put the array

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Rather than create an array, you should be renaming functions.

    Function1, Function2, ... these are meaningless function names that don't tell anyone anything about what they do, which I would argue is a lot more important. Some better names might be:

    Code:
    #ifndef FUNCTIONS_HPP
    #define FUNCTIONS_HPP
    
    #include <iostream>
    #include <string>
    
    enum Options {NORMAL_EXIT, KG_TO_LBS, CELSIUS_TO_KELVIN, TSP_TO_ML, M_TABLE, S_TABLE};
    
    class Functions
    {
    public:
        static double GetDouble(const std::string& prompt);
        static int GetInt(const std::string& prompt);
        static Options Menu(void);
        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);
    };
    
    #endif

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    thanks!!!

    i got this now

    Code:
     #include "stdafx.h"
    #include "Functions.h"
    #include <iostream>
    using namespace std;
    
    {
        cout << "Constructing base class..." << endl;
    }
    
    Functions::()
    {
        cout << "Constructing derived class..." << endl;
    }
    
    void Functions::()
    {
        cout << "Invoking Function 1..." << endl;
    }
    
    void Functions::()
    {
        cout << "Invoking Function 2..." << endl;
    }
    
    void Functions::()
    {
        cout << "Invoking Function 3..." << endl;
    }
    
    void Functions::()
    {
        cout << "Invoking Function 4..." << endl;
    }
    
    void Functions::()
    {
        cout << "Invoking Function 5..." << endl;
    }
    
    int Functions::()
    {
        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 Functions 
    {
    public:
        static double GetDouble(const std::string& prompt);
        static int GetInt(const std::string& prompt);
        static Options Menu(void);
        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);
    };
    
    #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->();    break;
            case 2:
                f->();    break;
            case 3:
                f->();    break;
            case 4:
                f->();    break;
            case 5:
                f->();    break;
            }
        } while (opt != 0);
    }
    Last edited by sonicflare9; 03-01-2018 at 10:57 AM.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by whiteflags View Post
    Rather than create an array, you should be renaming functions.

    Function1, Function2, ... these are meaningless function names that don't tell anyone anything about what they do, which I would argue is a lot more important. Some better names might be:
    I agree that the somewhere in the code, each function should have a better name. But in naming the functions function1 to function5, sonicflare9 properly observed that there is something common about all operations in a menu. This could be placed in an array of menu functions.

    I do agree that the list of functions you named should exist too.

    One thing in your code I don't see the point of is making them static functions to a class. This is C++ not Java; there's such a thing as free functions. You don't need a class "Functions". OTOH, a class "Function" or better named, "MenuItem" is not a bad idea.

    @ sonicflare9
    The array would go in main or in a class that's created in main.

    Your last code doesn't compile at all. Nor does it attempt to convert kg to pounds anywhere. The code before was (marginally) better.
    Last edited by King Mir; 03-01-2018 at 04:56 PM.
    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.

  11. #11
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    what should I do to make it work

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by sonicflare9 View Post
    what should I do to make it work
    Learn some c++! Read my first post in this thread, and wherever you're trying to learn c++ from again.

    You can't just cobble together a bunch of code snippets other people give you, you need to understand what is being done and why, and apply it.
    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.

  13. #13
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    im kinda new to this thing

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by sonicflare9 View Post
    im kinda new to this thing
    Well, to be honest, you only need roughly a handful of C++ lessons to write this program.
    • Loops (while, at the bare minimum)
    • Functions
    • Switch-case
    • I/O


    Another key problem is that the menu function seems to be doing something that I would expect all menus to do: plainly state the options.

    So maybe try drastically simplifying this design?

    I agree that the somewhere in the code, each function should have a better name. But in naming the functions function1 to function5, sonicflare9 properly observed that there is something common about all operations in a menu. This could be placed in an array of menu functions.
    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.

  15. #15
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    maybe would it work if I change the functions in the header one to different names? the class functions publics one? and then maybe i can change the sampleproject and functions cpp

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