Thread: error C2065: undeclared identifier PLEASE HELP!

  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,336
    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,336
    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
    28,413
    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?
    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

  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
    28,413
    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;
            }
        }
    }
    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

  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,336
    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, 02: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