Type INT

is there a way to compare a variable to an int without having to type if(i = 1 || i == 2 ... etc?
A switch case works:
1
2
3
4
5
6
7
switch(Variable)
{
   case 1: //Code
   case 2: //Same Code
   case 3: //You Get The Idea
//...
}

Last edited on
True but the problem with the switch statement, which i tried earlier, is that the algorithm i attempting to use does not function properly with it.
Instead of writing if(i == 1 || i == 2 || i == 3) you can write if (1 <= i && i <= 3).
Last edited on
I hope this isn't homework :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
int i = 100;
int x=0;
while (x<=999)
{
     if (x==i)
     {
     cout << x << " is equal to " << i << endl;
     break;
     }
     else
     {
     cout << x << " is not equal to " << i << endl;         
     x++;

     }
}

   return(0);
}


To make it run faster, comment out the "is not equal to" line.

Or maybe this is what you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
int i = 100;
int x = 99;
     if (x!=i)
     {
     cout << x << " is not equal to " << i << endl;         
     }
     else
     {
     cout << x << " is equal to " << i << endl;         
     }

   return(0);
}
Last edited on
Topic archived. No new replies allowed.