is possible using switch statement for guessing game?

I am trying to create a guess game by using switch statement.
But when I wrote the case
It shows that the case value is not a constant expression.

Here is my [code]
#include <iostream>
#include <ctime>
#include<cstdlib>
#include<cstdio>

using namespace std;
int main() {
int uguess=0;
int cpick;

srand((unsigned int)time(0));
cpick=rand()%100+1;
cin>>cpick;

cout<<"please enter a number range 1 to 100."<<endl;
cin>>uguess;

switch(uguess)
{
case (uguess<cpick):
cout<<"too low, tried again"<<endl;
break;
case (cpick<uguess):
cout<<"too high,tired"<<endl;
break;
You should check the switch statement documentation again, check out the examples: http://en.cppreference.com/w/cpp/language/switch

Switch statements don't work the same as if statements, you cant test for a range of number. You can only check for specific numbers. In your case, you should just use if statements.

if guess is greater then num output to high
else if guess is less then num output low
else output you guessed the number

Note switch statements can be used to test other data types then just integers.

Please also use code tags next time, they're the <> on the right side of the submission box.
Topic archived. No new replies allowed.