Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. #define how to use with is code ?

#define how to use with is code ?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    krish_kumar
    wrote on last edited by
    #1

    int main() { printf("Enter 0 or 1 :"); scanf("%d",&i_choice); if ( i_choice ==0 ) for( ;i_count_1 < i_number;) {------- ------ } if (choice == 1) for( ;i <= i_number;) {------- ------ } Here I raed a user input ( 0 or 1) So as per i/p I've to change the condition in my for loop. if choice ==1 the condition must be (i <= i_number) if choice == 0 the condition must be (i_count_1 < i_number) I don't know how to use #define with this program. anybody can help me.... Krish

    M 1 Reply Last reply
    0
    • K krish_kumar

      int main() { printf("Enter 0 or 1 :"); scanf("%d",&i_choice); if ( i_choice ==0 ) for( ;i_count_1 < i_number;) {------- ------ } if (choice == 1) for( ;i <= i_number;) {------- ------ } Here I raed a user input ( 0 or 1) So as per i/p I've to change the condition in my for loop. if choice ==1 the condition must be (i <= i_number) if choice == 0 the condition must be (i_count_1 < i_number) I don't know how to use #define with this program. anybody can help me.... Krish

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      You don't need #define in your code sample, but you need a switch(i_choice).

      Maxwell Chen

      K 1 Reply Last reply
      0
      • M Maxwell Chen

        You don't need #define in your code sample, but you need a switch(i_choice).

        Maxwell Chen

        K Offline
        K Offline
        krish_kumar
        wrote on last edited by
        #3

        here I wanna reduce the code size.... swith(i_choice) { case 0 : for (.. ; condition 1 ; ..) { } break; case 1 : for (.. ; condition 2 ; ..) { } break; } So the code will come twise ; except the conditions in for loop all other statements are same for case 1 and 0. That's why I tried for a #define Macro. ???? with regards Krish

        M D 2 Replies Last reply
        0
        • K krish_kumar

          here I wanna reduce the code size.... swith(i_choice) { case 0 : for (.. ; condition 1 ; ..) { } break; case 1 : for (.. ; condition 2 ; ..) { } break; } So the code will come twise ; except the conditions in for loop all other statements are same for case 1 and 0. That's why I tried for a #define Macro. ???? with regards Krish

          M Offline
          M Offline
          Maxwell Chen
          wrote on last edited by
          #4

          krish_kumar wrote:

          case 0 : for (.. ; condition 1 ; ..)

          Then could you show the complete code (I mean those ... portions)? I will figure out a solution for you.

          Maxwell Chen

          1 Reply Last reply
          0
          • K krish_kumar

            here I wanna reduce the code size.... swith(i_choice) { case 0 : for (.. ; condition 1 ; ..) { } break; case 1 : for (.. ; condition 2 ; ..) { } break; } So the code will come twise ; except the conditions in for loop all other statements are same for case 1 and 0. That's why I tried for a #define Macro. ???? with regards Krish

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            krish_kumar wrote:

            That's why I tried for a #define Macro. ????

            Which would be totally wrong. The #define directive is handled by the preprocessor before the compile phase. You don't know the value of i_choice until the code is running. See the problem?

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            K 1 Reply Last reply
            0
            • D David Crow

              krish_kumar wrote:

              That's why I tried for a #define Macro. ????

              Which would be totally wrong. The #define directive is handled by the preprocessor before the compile phase. You don't know the value of i_choice until the code is running. See the problem?

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              K Offline
              K Offline
              krish_kumar
              wrote on last edited by
              #6

              Okkk... I realize I can't use a #define here.... But my code is repeating twice ...I wanna avoid that.... Except condition the other statements are same for both... Here is my code printf("\n U wanna find prime nos up to N(press 1) or N prime nos (press 0 "); scanf("%d",&i_choice); switch(i_choice) { case 0 : for (i = 1, i_count = 0; i_count_1 < i_number; i++ ) { for (j = 1,i_count = 0; j <= i ; j++) { if ((i % j) == 0) ++i_count; } if (i_count <= 2) { printf("\t %d \n",i); i_count_1++; } } break; case 1 : for (i = 1, i_count = 0 ; i <= i_number; i++ ) { for (j = 1, i_count = 0; j <= i ; j++) { if( (i % j)==0 ) ++i_count; } if (i_count <= 2) { printf("\t %d \n",i); i_count_1++; } } } }

              M D 2 Replies Last reply
              0
              • K krish_kumar

                Okkk... I realize I can't use a #define here.... But my code is repeating twice ...I wanna avoid that.... Except condition the other statements are same for both... Here is my code printf("\n U wanna find prime nos up to N(press 1) or N prime nos (press 0 "); scanf("%d",&i_choice); switch(i_choice) { case 0 : for (i = 1, i_count = 0; i_count_1 < i_number; i++ ) { for (j = 1,i_count = 0; j <= i ; j++) { if ((i % j) == 0) ++i_count; } if (i_count <= 2) { printf("\t %d \n",i); i_count_1++; } } break; case 1 : for (i = 1, i_count = 0 ; i <= i_number; i++ ) { for (j = 1, i_count = 0; j <= i ; j++) { if( (i % j)==0 ) ++i_count; } if (i_count <= 2) { printf("\t %d \n",i); i_count_1++; } } } }

                M Offline
                M Offline
                Maxwell Chen
                wrote on last edited by
                #7

                I would suggest you to re-design your code architecture. See the sample code below.

                #include <iostream>

                int GetNextPrime(int iStartNum)
                {
                int i, j;
                int i_count;
                for(i = iStartNum; ; i++) {
                for(j = 1, i_count = 0; j <= i ; j++) {
                if( (i % j) == 0 )
                ++i_count;
                }
                if(i_count <= 2) {
                return i;
                }
                }
                return 0;
                }

                void main()
                {
                int i_choice;
                int i_count;
                int i_count_1 = 0;
                int i_number = 10;
                int i, j;
                int iValNow;
                int count;

                printf("Find prime nos up to %d (press 1), or %d prime nos (press 0) ", i\_number, i\_number);
                scanf("%d", &i\_choice);
                
                
                // ---------------------------------------------------------
                printf("\\n My version: \\n");
                switch(i\_choice)
                {
                case 0 :	// N count.
                	for(i = 0, iValNow = i, count = 0; count < i\_number; i++, count++) {
                		iValNow = GetNextPrime(iValNow +1);
                		printf("\\t %d \\n", iValNow);			
                	}
                	break;
                case 1 :	// up to N.
                	for(iValNow = 0; ; ) {
                		iValNow = GetNextPrime(iValNow +1);
                		if(iValNow > i\_number) {
                			break;
                		}
                		printf("\\t %d \\n", iValNow);
                	}
                	break;
                }
                
                // ---------------------------------------------------------
                printf("\\n Your version: \\n");
                
                switch(i\_choice)
                {
                case 0 :	// N count.
                	for(i = 1, i\_count = 0; i\_count\_1 < i\_number; i++ ) {
                		for(j = 1,i\_count = 0; j <= i ; j++) {
                			if( (i % j) == 0 )
                				++i\_count;
                		}
                		if(i\_count <= 2) {
                			printf("\\t %d \\n",i);
                			i\_count\_1++;
                		}
                	}
                	break;
                
                case 1 :	// up to N.
                	for(i = 1, i\_count = 0 ; i <= i\_number; i++ ) {
                		for(j = 1, i\_count = 0; j <= i ; j++) {
                			if( (i % j) == 0 )
                				++i\_count;
                		}
                		if(i\_count <= 2) {
                			printf("\\t %d \\n",i);
                			i\_count\_1++;
                		}
                	}
                }
                

                }

                Maxwell Chen

                1 Reply Last reply
                0
                • K krish_kumar

                  Okkk... I realize I can't use a #define here.... But my code is repeating twice ...I wanna avoid that.... Except condition the other statements are same for both... Here is my code printf("\n U wanna find prime nos up to N(press 1) or N prime nos (press 0 "); scanf("%d",&i_choice); switch(i_choice) { case 0 : for (i = 1, i_count = 0; i_count_1 < i_number; i++ ) { for (j = 1,i_count = 0; j <= i ; j++) { if ((i % j) == 0) ++i_count; } if (i_count <= 2) { printf("\t %d \n",i); i_count_1++; } } break; case 1 : for (i = 1, i_count = 0 ; i <= i_number; i++ ) { for (j = 1, i_count = 0; j <= i ; j++) { if( (i % j)==0 ) ++i_count; } if (i_count <= 2) { printf("\t %d \n",i); i_count_1++; } } } }

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  krish_kumar wrote:

                  But my code is repeating twice ...I wanna avoid that....

                  No problem. Just put the common code in a function.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups