Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple switch not working

Status
Not open for further replies.

random260

Programmer
Mar 11, 2002
116
0
0
US
I have a simpleprogram where the switch statement isn't working for some reason. Like this:

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int level = 5;

void getlevel();
void dosomething();

int main() {

getlevel();
dosomething();
switch(level) {
case 0:
cout<<"Level is 0"<<endl;
break;
case 1:
cout<<"Level is 1"<<endl;
}
return 0;
}

void getlevel() {
cin>>level;
}

void dosomething() {
cout<<"Did something"<<endl;
}

The switch doesn't work - it's like level doesn't equal anything. If I do this

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int level = 5;

void getlevel();
void dosomething();

int main() {

getlevel();
dosomething();
cout<<level<<endl;
switch(level) {
case 0:
cout<<"Level is 0"<<endl;
break;
case 1:
cout<<"Level is 1"<<endl;
}
return 0;
}

void getlevel() {
cin>>level;
}

void dosomething() {
cout<<"Did something"<<endl;
}

and enter 1 for the input, the cout prints 1 - it KNOWS the level I input (it is a global variable after all) but the switch doesn't recognize it. If I do this

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int level = 5;

void getlevel();
void dosomething();

int main() {

getlevel();
dosomething();
level = 1;
switch(level) {
case 0:
cout<<"Level is 0"<<endl;
break;
case 1:
cout<<"Level is 1"<<endl;
}
return 0;
}

void getlevel() {
cin>>level;
}

void dosomething() {
cout<<"Did something"<<endl;
}

Then the switch works for the value of 1. It's like the act of calling the function dosomething() invalidates the switch, even though the value is still stored in the variable.

Anybody got any ideas why this is doing this?
 
Your original code works just fine for me. I typed "1" without the quotes and hit Enter. It output:

Did something
Level is 1

Not sure why it isn't working for you. (I'm using MSVC++ 6.0 with a newer Dinkumware STL implementation).
 
i don't see anything wrong with your switch case but if it is giving you problems try stepping through it with the debugger. This will allow you to visually see where and what is going on with your code. ;)
 
Change your code to have teh following:


switch(level)
{
case 0:
cout<<"Level is 0"<<endl;
break;
case 1:
cout<<"Level is 1"<<endl;
break;
default:
cout "defualt: Level is << level << endl;
}


This way you will find out the value of level
 
Looks like it is a console app... Press the exclamation point to execute it instead of F5. That will leave the console up long enough for you to see the output.

Matt
 
Or just add a cin.get() or a System(pause) before the return from main...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top