![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 16
| error C2065: undeclared identifier 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 | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| I think you mean Code: int b(2); |
| tabstop is offline | |
| | #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); whats the difference between variables and constants? sorry im a beginner haha |
| dyelax is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop is offline | |
| | #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 | |
| | #6 |
| and the Hat of Guessing 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 | |
| | #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 | |
| | #8 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,356
| Quote:
__________________ 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 | |
| | #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 | |
| | #10 |
| C++ Witch 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 | |
| | #11 |
| Registered User Join Date: Oct 2009
Posts: 16
| ooooohhhhhh. so the case has to be a number? |
| dyelax is offline | |
| | #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 | |
| | #13 |
| and the Hat of Guessing 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 | |
| | #14 |
| Registered User Join Date: Oct 2009
Posts: 16
| all right thank you |
| dyelax is offline | |
![]() |
| Tags |
| c++, error c2065, undeclared identifier |
| Thread Tools | |
| Display Modes | |
|
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 |