Thread: if else problem

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    if else problem

    When im doing an if and then an else, and i want the else to be a "do nothing". How do i do that and can i have an example?
    thanks

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    51
    what im trying to do is get the program to print the numbers in ascending order. How do i get the program to go to the swap part from main? Im not really understanding how to do things separatly from the main. Heres my really bad code:

    Code:
               
       void swap(int x, int y){
               
          int z = x;
          x = y;
          y = z;
       }
               
       int main(){
               
       
          int a = 45;
          int b = 2;
       
          if (a < b){
             printf("%d %d\n", a, b);
          }
       
          if (a > b){
             swap(a, b);
             printf("%d %d\n", a, b);
          }
       
       
       
       }

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you need to pass pointers instead.
    Code:
               
       void swap(int *x, int *y){
               
          int z = *x;
          *x = *y;
          *y = z;
       }
               
       int main(){
               
       
          int a = 45;
          int b = 2;
       
          if (a < b){
             printf("%d %d\n", a, b);
          }
       
          if (a > b){
             swap(&a, &b);
             printf("%d %d\n", a, b);
          }
       
       
       
       }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    You could just try this...

    Code:
    void swap(int *x, int *y)
    {        
      int z = *x;
      *x = *y;
      *y = z;
    }
               
    int main()
    {
      int a = 45;
      int b = 2;
        
      if (a > b)
      {
        swap(&a, &b);
      }
       
      printf("%d %d\n", a, b);     
    }

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    51
    This works ok. How do i make it so that you can input different arguments instead of having them set when i run the program.. like i want to be able to enter values when i execute it if you know what i mean?

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    main can be declared in two ways...

    int main( void )
    or
    int main( int argc, char *argv[] )

    If you use the second declaration, argc relates to the number of parameters passed at runtime, and argv is an array of strings representing the parameters themselves

    e.g. from the command prompt I could type

    myprogram 45 2

    argc = 3
    argv[0] = "myprogram"
    argv[1] = "45"
    argv[2] = "2"

    The parameters are expressed as strings, so you would have to use atoi to convert them to integers (and probably something else to check that they are actually numbers).

    Alternatively you could prompt the user from within the program to choose two numbers...

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by sworc66
    This works ok. How do i make it so that you can input different arguments instead of having them set when i run the program.. like i want to be able to enter values when i execute it if you know what i mean?
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    May be i might not got the point correctly. Question is
    like i want to be able to enter values when i execute it
    why not use scanf and compare, if condition is true swap??
    Saravanan.T.S.
    Beginner.

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    51
    THanks everyone, its been great help. In regards to main() i just need a simple explination as to how i write functions outside of the main and then refer to them in the main. At the moment i really dont understand exactly how to get away from writing everything in the main().

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by sworc66
    THanks everyone, its been great help. In regards to main() i just need a simple explination as to how i write functions outside of the main and then refer to them in the main. At the moment i really dont understand exactly how to get away from writing everything in the main().
    There are loads of examples on here. Maybe start with the tutorial, and then ask a specific question when you're stuck.

    Code:
    #include <stdio.h>
    
    void printme(int i)
    {
      printf ("value: %d\n", i);
    }
    
    int main(void)
    {
      printme(10);
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    51
    I can only seem to find c++ tutes on this site... anyways... here is the start of what ive done. Can you show me using my code how to refer to functions outside of main() because i want to add more. Just need another example. Thanks.

    Code:
    #include <stdio.h>
    
               
       int main(){
               
          
       }
    
               
       void one(){
               
          int a;
       
          scanf("%d", &a);
          switch(a){
             case 0: printf("zero \n");
                break;
             case 1: printf("one \n");
                break;
             case 2: printf("two \n");
                break;
             case 3: printf("three \n");
                break;
             case 4: printf("four \n");
                break;
             case 5: printf("five \n");
                break;
             case 6: printf("six \n");
                break;
             case 7: printf("seven \n");
                break;
             case 8: printf("eight \n");
                break;
             case 9: printf("nine \n");
                break;
             case 10: printf("ten \n");
                break;
             case 11: printf("eleven \n");
                break;
             case 12: printf("twelve \n");
                break;
             case 13: printf("thirteen \n");
                break;
             case 14: printf("fourteen \n");
                break;
             case 15: printf("fifteen \n");
                break;
             case 16: printf("sixteen \n");
                break;
             case 17: printf("seventeen \n");
                break;
             case 18: printf("eighteen \n");
                break;
             case 19: printf("ninteen \n");
                break;
             default: printf("error \n");
                break;
          }
       }

  12. #12
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Code:
    void inline swap(int *x, int *y)
    {
      *x = *x ^ *y;
      *y = *y ^ *x;
      *x = *x ^ *y;
    }
    Probably the best swap function.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Just need another example.
    Code:
    #include <stdio.h>
    
    void foo(size_t x); /* prototype if function definition is after function call */
    
    int main(void)
    {
       size_t i;
       for ( i = 0; i < 7; ++i )
       {
          foo(i); /* function call */
       }
       return 0;
    }
    
    void foo(size_t x) /* function definition */
    {
       const char *number[] = { "zero", "one", "two", "three", "you get the idea" };
       puts( x < sizeof(number)/sizeof(*number) ? number[x] : "huh?" );
    }
    
    /* my output
    zero
    one
    two
    three
    you get the idea
    huh?
    huh?
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Aug 2002
    Posts
    51
    i get this error:

    bash-2.02$ gcc -o ass ass.c
    ass.c:47: warning: type mismatch with previous implicit declaration
    ass.c:43: warning: previous implicit declaration of `one'
    ass.c:47: warning: `one' was previously implicitly declared to return `int'

    Also what i want to be able to do next with the two() function is for the user to be able to type in a number in word format ie thirteen and the prog outputs the number in integer format. What i want to know how to do is do scanf in the main() and then depending on whether a number or a word was entered in to go to the correct function.

    Code:
    #include <stdio.h>
    
               
       int main(void){
               
          one();
          return 0;
       }
    
               
       void one(){
               
          int a;
       
          scanf("%d", &a);
          switch(a){
             case 0: printf("zero \n");
                break;
             case 1: printf("one \n");
                break;
             case 2: printf("two \n");
                break;
             case 3: printf("three \n");
                break;
             case 4: printf("four \n");
                break;
             case 5: printf("five \n");
                break;
             case 6: printf("six \n");
                break;
             case 7: printf("seven \n");
                break;
             case 8: printf("eight \n");
                break;
             case 9: printf("nine \n");
                break;
             case 10: printf("ten \n");
                break;
             case 11: printf("eleven \n");
                break;
             case 12: printf("twelve \n");
                break;
             case 13: printf("thirteen \n");
                break;
             case 14: printf("fourteen \n");
                break;
             case 15: printf("fifteen \n");
                break;
             case 16: printf("sixteen \n");
                break;
             case 17: printf("seventeen \n");
                break;
             case 18: printf("eighteen \n");
                break;
             case 19: printf("ninteen \n");
                break;
             default: printf("error \n");
                break;
          }
       }
    
               
       void two(){
               
       
       
       
       }

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    bash-2.02$ gcc -o ass ass.c
    ass.c:47: warning: type mismatch with previous implicit declaration
    ass.c:43: warning: previous implicit declaration of `one'
    ass.c:47: warning: `one' was previously implicitly declared to return `int'
    If you used prototypes for your functions you wouldn't have this problem.

    >then depending on whether a number or a word was entered in to go to the correct function.
    I personally wouldn't trust the user to enter a string accurate enough to do this conversion, but you could do something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        char buff[BUFSIZ];
        int  num;
    
        printf("Enter a number or the word equivalent (10 or ten): ");
        fflush(stdout);
    
        if (fgets(buff, sizeof buff, stdin) != NULL) {
            char *pos;
    
            num = (int)strtol(buff, &pos, 0);
    
            if (pos == buff)
                puts("You entered a string");
            else
                puts("You entered a number");
        }
    
        return 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM