C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-12-2009, 08:32 AM   #1
Registered User
 
Join Date: Oct 2009
Posts: 16
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;
		}
	}
}
dyelax is offline   Reply With Quote
Old 10-12-2009, 08:49 AM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 10-12-2009, 09:21 AM   #3
Registered User
 
Join Date: Oct 2009
Posts: 16
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
dyelax is offline   Reply With Quote
Old 10-12-2009, 09:27 AM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Variable Definition | Definition of Variable at Dictionary.com

Constant Definition | Definition of Constant at Dictionary.com
tabstop is offline   Reply With Quote
Old 10-12-2009, 09:47 AM   #5
Registered User
 
Join Date: Oct 2009
Posts: 16
ya i know that i meant whats the difference in writing them in c++
dyelax is offline   Reply With Quote
Old 10-12-2009, 09:51 AM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
A constant is something like 6. Or 11. Or 253. Something that is constant according to the English sense of the term.
tabstop is offline   Reply With Quote
Old 10-12-2009, 09:56 AM   #7
Registered User
 
Join Date: Oct 2009
Posts: 16
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);
dyelax is offline   Reply With Quote
Old 10-12-2009, 09:58 AM   #8
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,356
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 10-12-2009, 10:01 AM   #9
Registered User
 
Join Date: Oct 2009
Posts: 16
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;
		}
	}
}
dyelax is offline   Reply With Quote
Old 10-12-2009, 10:09 AM   #10
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,356
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 10-12-2009, 10:16 AM   #11
Registered User
 
Join Date: Oct 2009
Posts: 16
ooooohhhhhh. so the case has to be a number?
dyelax is offline   Reply With Quote
Old 10-12-2009, 10:24 AM   #12
Registered User
 
Join Date: Oct 2009
Posts: 16
thank you! it runs now! I'm sorta confused why you dont have to have "int a;" at the beginning before the loop
dyelax is offline   Reply With Quote
Old 10-12-2009, 10:26 AM   #13
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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)
tabstop is offline   Reply With Quote
Old 10-12-2009, 11:50 AM   #14
Registered User
 
Join Date: Oct 2009
Posts: 16
all right thank you
dyelax is offline   Reply With Quote
Reply

Tags
c++, error c2065, undeclared identifier

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:37 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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