Thread: Switch statement

  1. #16
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Quote Originally Posted by vart View Post
    and why exactly do you need declare reslut as pointer to double and allocate it dynamically?
    I suppose i don't need to, just a spur-of-the-moment kind of thing.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I noticed a rather unfortunate choice of variable names:
    Code:
    double _Num1;
    double _Num2;
    double _Num3;
    The C++ Standard states that "each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercase
    letter is reserved to the implementation for any use". As such, you should use a different variable naming convention. It seems to me that something as straightforward as num1, num2 and num3 will do.

    It also seems that these three variables were not really used at all. Likewise, it looks like much of your class actually functions as a namespace with the member variables left largely unused except for result. After all, although you use Num1 and Num2 in various member functions, you have parameters of the same name, thus those parameters are used and the member variable names are hidden.
    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

  3. #18
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    I have went over my code and realised that it was a bit of a mess, thanks to the people on this bored for helping me realise this, so I have reformatted(I think that's the correct word) my code and come across a few errors, I have moved....

    Code:
    double num1;
    double num2;
    double num3;
    double Num1;
    double Num2;
    double Num3;
    ....to the constructor and removed the other declarations from the program, but I don't see why i get these errors:

    Code:
    main.cpp          28     error: 'num1' undeclared(first use this function)
    I thought that whenever an object is made that the constructor is called. So why are my variables not declared?

    Am I missing something here?
    Last edited by beene; 07-01-2007 at 02:35 AM.

  4. #19
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I believe variables declared inside a constructor are local to that constructor, just like a regular function. You need variables that have class-wide scope to access them anywhere inside the class.

  5. #20
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Right, is there another way to tidy up my code without making the variables global?

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes: declare local and member variables as needed and keep them and their scopes straight.
    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

  7. #22
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Thanks a bunch.

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, 03: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