error C2065: undeclared identifier PLEASE HELP!

This is a discussion on error C2065: undeclared identifier PLEASE HELP! within the C++ Programming forums, part of the General Programming Boards category; ok so im trying to learn switch cases so i tried to write this program to see if i understood ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    38

    error C2065: undeclared identifier PLEASE HELP!

    ok so im trying to learn switch cases so i tried to write this program to see if i understood how to write them and i wrote both my variables the same exact way and its only giving me an undeclared variable for 1 of them. here's my code, please help.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    	int a;
    	int b (b = 1);
    	int c (c = 2);
    
    	for (a = 0; a < 3; a++) {
    		switch (a) {
    			case b:
    				{
    					cout<< "wow this is really confusing\n";
    				}
    				break;
    			case c:
    				{
    					cout<< "now it's a little easier\n";
    				}
    				break;
    			default:
    				{
    					cout<< "X-P\n";
    				}
    				break;
    		}
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I think you mean
    Code:
    int b(2);
    and similarly for c. Also I don't believe you can use variables in case statements, only constants.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    thank you so should that part of the code look like

    Code:
    int a;
    int b(1);
    int c(2);
    i tried compiling that and it said "case expression not constant." for both b and c.

    whats the difference between variables and constants? sorry im a beginner haha

  4. #4

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    ya i know that i meant whats the difference in writing them in c++

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    A constant is something like 6. Or 11. Or 253. Something that is constant according to the English sense of the term.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    yeah so how would i write it because when i write it like this it says "case expression not constant."

    Code:
    int a;
    int b(1);
    int c(2);

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by dyelax
    yeah so how would i write it because when i write it like this it says "case expression not constant."
    What are you trying to do?
    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
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    this is the whole code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    	int a;
    	int b(1);
    	int c(2);
    
    	for (a = 0; a < 3; a++) {
    		switch (a) {
    			case b:
    				{
    					cout<< "wow this is really confusing\n";
    				}
    				break;
    			case c:
    				{
    					cout<< "now it's a little easier\n";
    				}
    				break;
    			default:
    				{
    					cout<< "X-P\n";
    				}
    				break;
    		}
    	}
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    What your compiler is trying to tell you is that the expression used in a case must be a constant, i.e., you should write:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        for (int a = 0; a < 3; a++) {
            switch (a) {
            case 1:
                {
                    cout<< "wow this is really confusing\n";
                }
                break;
            case 2:
                {
                    cout<< "now it's a little easier\n";
                }
                break;
            default:
                {
                    cout<< "X-P\n";
                }
                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

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    ooooohhhhhh. so the case has to be a number?

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    thank you! it runs now! I'm sorta confused why you dont have to have "int a;" at the beginning before the loop

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You do have to have int a before the loop.(*) And since you have it, everybody's happy.

    (*)You are allowed to have it inside the loop itself, as in
    Code:
    for (int a = 0; a < 3; ++a)

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    all right thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

Tags for this Thread


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