Switch statement

This is a discussion on Switch statement within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by vart and why exactly do you need declare reslut as pointer to double and allocate it dynamically? ...

  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
    22,269
    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.
    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. #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 03:35 AM.

  4. #19
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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,893
    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.

Page 2 of 2 FirstFirst 12
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, 04: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, 03:17 PM
  4. char switch statement
    By jmarsh56 in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2006, 06:04 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 03:47 PM

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