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?
#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?