Switch statement

This is a discussion on Switch statement within the C++ Programming forums, part of the General Programming Boards category; I'm having problems with my switch statement, i try to call a member function of my calculator class inside of ...

  1. #1
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321

    Switch statement

    I'm having problems with my switch statement, i try to call a member function of my calculator class inside of my switch statement and i keep getting errors.

    Code:
    main.cpp     18  error: expected primary-expression before "double"
    I have 5 of the same errors, here is the switch statement:

    Code:
    switch (choice)
    {
        case '1':
        NewCalc.Multiply(double, double);
        break;
        case '2':
        NewCalc.Multiply(double, double, double);
        break;
    }
    Obviously it's not finished yet but i don't want to carry on if it's got errors.
    Can someone help me out?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Perhaps you have forgotten that double is a keyword used to declare double precision floating point variables?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Ok, but when i get rid of the doubles it keeps saying that the function cannot be overloaded.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    What did you try in getting rid of the doubles? It looks like all you are trying to do is call one of two functions depending on the case block entered.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Yup, that's it, but i can't get it to work, obviously, i have no idea why my compiler won't let me use keywords in switch statements.

    This is confusing me a bit now

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Yup, that's it, but i can't get it to work, obviously, i have no idea why my compiler won't let me use keywords in switch statements.
    Are you trying to define the functions? It is simply invalid syntax. To call a function that takes arguments, you have to pass something, be it a literal, a temporary or a variable.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    I have declared and defined my functions in other files, i am simply trying to use my function

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    In that case, I would expect to see something like:
    Code:
    double num = 1.2;
    switch (choice)
    {
        case '1':
        NewCalc.Multiply(3.4, 5.6);
        break;
        case '2':
        NewCalc.Multiply(num, num, num);
        break;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    I tried that, but i get the error:

    Code:
    Num1 undefined (first use this function)
    It is really anoying!!!

    Oh yeah... An the numbers are what the user imputs.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Now it looks like you are using a variable before defining it.

    Hate to say this, but this kind of newbie problems seem rather uncharacteristic of you... perhaps you should get some rest?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    And/or post the latest code, including the surrounding context.

  12. #12
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Ok, i think i get what i've done wrong here, but i'll fix it tomorrow, my mam is just about to steel my computer for the next hour or so, and i'm tired, i've been working on this project all night, i don't know how proffesional programmers do this stuff 24/7.

    Quote Originally Posted by laserlight View Post
    Hate to say this, but this kind of newbie problems seem rather uncharacteristic of you... perhaps you should get some rest?
    Thanks, i'll take that as a compliment

  13. #13
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by beene View Post
    i don't know how proffesional programmers do this stuff 24/7.
    two words, COFFEE and DELIVERY
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  14. #14
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Hi everyone, i have just came home and fixed it, it was easy.
    Thanks for all of your help though, i really appreciate it, it was such a nooby problem, i probably was to tired yesterday, anyway, here is the code, all i have to do is add a do while loop and there you have it, my OOP Calculator...

    main.cpp
    Code:
    #include <iostream>
    #include "Functions.h"
    
    int main()
    {
        int choice;
        double _Num1;
        double _Num2;
        double _Num3;
        std::cout << "Welcome to my prototype Calculator with the basic functions" << std::endl;
        std::cout << "of Addition, Subtraction, Multiplication and Division" << std::endl;
        std::cout << "\nThis Calculator has the ability to Add, Subtract, Multiply" << std::endl;
        std::cout << "and Divide 3 numbers as well as two" << std::endl;
        std::cout <<"\nWhat would you like to do?" << std::endl;
        calculator NewCalc;
        NewCalc.Menu();
        std::cin >> choice;
        switch (choice)
        {
            case 1:
            NewCalc.Multiply(_Num1, _Num2);
            break;
            case 2:
            NewCalc.Multiply(_Num1, _Num2, _Num3);
            break;
            case 3:
            NewCalc.Divide(_Num1, _Num2);
            break;
            case 4:
            NewCalc.Divide(_Num1, _Num2, _Num3);
            break;
            case 5:
            NewCalc.Add(_Num1, _Num2);
            break;
            case 6:
            NewCalc.Add(_Num1, _Num2, _Num3);
            break;
            case 7:
            NewCalc.Subtract(_Num1, _Num2);
            break;
            case 8:
            NewCalc.Subtract(_Num1, _Num2, _Num3);
            break;
        }
        return 0;
    }
    Funtions.cpp
    Code:
    #include <iostream>
    #include "Functions.h"
    
    calculator::calculator()
    {
        result = new double;
        _Num1 = Num1;
        _Num2 = Num2;
        _Num3 = Num3;
    }
    
    calculator::~calculator()
    {
        delete result;
    }
    
    void calculator::Menu()
    {
        std::cout << "Multiply 2 numbers (1)" << std::endl;
        std::cout << "Multiply 3 numbers (2)" << std::endl;
        std::cout << "Divide 2 numbers   (3)" << std::endl;
        std::cout << "Divide 3 numbers   (4)" << std::endl;
        std::cout << "Add 2 numbers      (5)" << std::endl;
        std::cout << "Add 3 numbers      (6)" << std::endl;
        std::cout << "Subtract 2 numbers (7)" << std::endl;
        std::cout << "Subtract 3 numbers (8)" << std::endl;
    }
    
    double calculator::Add(double Num1, double Num2)
    {
        std::cout << "Type in the 2 numbers that you wanted to Add together..." << std::endl;
        std::cin >> Num1;
        std::cin.ignore();
        std::cout << "\n...And now the next" << std::endl;
        std::cin >> Num2;
        std::cin.ignore();
        *result = Num1 + Num2;
        std::cout << "\nResult: " << *result << std::endl;
    }
    
    double calculator::Add(double Num1, double Num2, double Num3)
    {
        std::cout << "Type in the 3 numbers that you want to Add together..." << std::endl;
        std::cin >> Num1;
        std::cin.ignore();
        std::cout << "\n...And now the next" << std::endl;
        std::cin >> Num2;
        std::cin.ignore();
        std::cout << "\n...And now the last" << std::endl;
        std::cin >> Num3;
        std::cin.ignore();
        *result = Num1 + Num2 + Num3;
        std::cout << "\nResult: " << *result << std::endl;
    }
    
    double calculator::Subtract(double Num1, double Num2)
    {
        std::cout << "Type in the 2 numbers that you want to Subtract from eachother..." << std::endl;
        std::cin >> Num1;
        std::cin.ignore();
        std::cout << "\n...And now the next" << std::endl;
        std::cin >> Num2;
        std::cin.ignore();
        *result = Num1 - Num2;
        std::cout << "\nResult: " << *result << std::endl;
    }
    
    double calculator::Subtract(double Num1, double Num2, double Num3)
    {
        std::cout << "Type in the 3 numbers that you want to Subtract from eachother..." << std::endl;
        std::cin >> Num1;
        std::cin.ignore();
        std::cout << "\n...And now the next" << std::endl;
        std::cin >> Num2;
        std::cin.ignore();
        std::cout << "\n...And now the last" << std::endl;
        std::cin >> Num3;
        std::cin.ignore();
        *result = Num1 - Num2 - Num3;
        std::cout << "\nResult: " << *result << std::endl;
    }
    
    double calculator::Multiply(double Num1, double Num2)
    {
        std::cout << "Type in the 2 numbers that you want to Multiply together..." << std::endl;
        std::cin >> Num1;
        std::cin.ignore();
        std::cout << "\n...And now the next" << std::endl;
        std::cin >> Num2;
        std::cin.ignore();
        *result = Num1 * Num2;
        std::cout << "\nResult: " << *result << std::endl;
    }
    
    double calculator::Multiply(double Num1, double Num2, double Num3)
    {
        std::cout << "Type in the 3 numbers that you want to Multiply together..." << std::endl;
        std::cin >> Num1;
        std::cin.ignore();
        std::cout << "\n...And now the next" << std::endl;
        std::cin >> Num2;
        std::cin.ignore();
        std::cout << "\n...And now the last" << std::endl;
        std::cin >> Num3;
        std::cin.ignore();
        *result = Num1 * Num2 * Num3;
        std::cout << "\nResult: " << *result << std::endl;
    }
    
    double calculator::Divide(double Num1, double Num2)
    {
        std::cout << "Type in the 2 numbers that you want to Divide..." << std::endl;
        std::cin >> Num1;
        std::cin.ignore();
        std::cout << "\n...And now the next" << std::endl;
        std::cin >> Num2;
        std::cin.ignore();
        *result = Num1 / Num2;
        std::cout << "\nResult: " << *result << std::endl;
    }
    
    double calculator::Divide(double Num1, double Num2, double Num3)
    {
        std::cout << "Type in the 3 numbers that you want to Divide..." << std::endl;
        std::cin >> Num1;
        std::cin.ignore();
        std::cout << "\n...And now the next" << std::endl;
        std::cin >> Num2;
        std::cin.ignore();
        std::cout << "\n...And now the last" << std::endl;
        std::cin >> Num3;
        std::cin.ignore();
        *result = Num1 / Num2 / Num3;
        std::cout << "\nResult: " << *result << std::endl;
    }
    Funtions.h
    Code:
    class calculator
    {
        public:
        calculator();
        ~calculator();
    
        void Menu();
    
        double Multiply(double Num1, double Num2);
        double Multiply(double Num1, double Num2, double Num3);
    
        double Divide(double Num1, double Num2);
        double Divide(double Num1, double Num2, double Num3);
    
        double Add(double Num1, double Num2);
        double Add(double Num1, double Num2, double Num3);
    
        double Subtract(double Num1, double Num2);
        double Subtract(double Num1, double Num2, double Num3);
    
        protected:
        double _Num1;
        double _Num2;
        double _Num3;
        double Num1;
        double Num2;
        double Num3;
        double *result;
    };
    Critisicm welcome, i rushed through this so if i could make it better i would appreciate that help also, and thanks again.

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    calculator::calculator()
    {
        result = new double;
        _Num1 = Num1;
        _Num2 = Num2;
        _Num3 = Num3;
    }
    and why exactly do you need declare reslut as pointer to double and allocate it dynamically?

    and why do you initialize _NumX with not initialized vaiables NumX?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  4. char switch statement
    By jmarsh56 in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2006, 05:04 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21