c++ - The #if elif directives not working as desired -
i trying test condition using if elif not getting desired results. please me in identifying doing wrong?
#include <iostream> using namespace std; #define a(t1)(t1+50) #define b(t2)(t2+100) int main() { union gm { int t1; int t2; }; gm c; cout <<"enter tokens earned in game 1: "<<endl; cin>>c.t1; int x=c.t1; cout <<"enter tokens earned in game 2: "<<endl; cin>>c.t2; int y=c.t2; int m= a(x)+100; int n= b(y)+50; int p; p=m+n; cout <<p<<endl; #if(p<500) cout <<"points earned less"<<endl; #elif (p>500 && p<1500) cout<<"you can play game 1 free"<<endl; #elif (p>1500 && p<2500) cout<<"you can play game 1 free"<<endl; #elif p>3000 cout<<"you can play game 1 , game free"<<endl; #endif return 0; }
when value of p exceeding 500, still showing message "points earned less".
please help.
you cannot use preprocessor macros depend on value of variable @ run time.
unless p
defined preprocessor macro,
#if(p<500)
is translated as:
#if(0<500)
since true, block of code compiled object code. explains behavior seeing.
you need use if-else
statements of c++, not preprocessor.
if (p<500) { cout <<"points earned less"<<endl; } else if (p>500 && p<1500) { cout<<"you can play game 1 free"<<endl; } else if (p>1500 && p<2500) { cout<<"you can play game 1 free"<<endl; } else if ( p>3000 ) { cout<<"you can play game 1 , game free"<<endl; } else { // else. }
Comments
Post a Comment