problem with running this code

Discussion in 'Programmer's Corner' started by TAKYMOUNIR, Oct 13, 2012.

  1. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    Code ( (Unknown Language)):
    1. [CODE=rich][/CODE]
    include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    int main()
    {
    int x,y,w,z,f1,f2;
    printf("enter four numbers");
    scanf("%d%d%d%d",x,y,w,z);
    f1=x-y-w-z;
    f2=x-y-(w+z);
    printf("numbers f1 is %d and f2 is %d",f1,f2);
    return 0;
    }
     
    Last edited: Oct 15, 2012
  2. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    You´ve been here a while, so you might have noticed we got here what is called code tag, which lets you post the code right here.
    Code ( (Unknown Language)):
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <math.h>
    4.  
    5. int main()
    6. {
    7.    int x,y,w,z,f1,f2;
    8.    printf("enter four numbers");
    9.    scanf("%d%d%d%d",x,y,w,z);
    10.    f1=x-y-w-z;
    11.    f2=x-y-(w+z);
    12.    printf("numbers f1 is %d and  f2 is %d",f1,f2);
    13.  
    14.     return 0;
    15. }
    16.  
    Look here, see where you problem is? http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
    That should help you with the first one, the second is that you should have spaces between the %d in the scanf function, else it won't recognize the rest of the numbers.
     
    TAKYMOUNIR likes this.
  3. panic mode

    Senior Member

    Oct 10, 2011
    1,321
    304
    in addition to comment by kubeek, your variables need & in front of each variable in SCANF instruction. this is one of the classic pitfalls of programming in c.
     
    TAKYMOUNIR likes this.
  4. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    panic mode likes this.
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019
    Not to mention not giving proper error messages.

    I doubt you would be getting the message "that the program stop working".

    I think panic mode has it. Scanf needs a pointer to a variable not the variable itself. & get the address of the variable i.e. it's pointer.
     
    panic mode and TAKYMOUNIR like this.
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019
    Had a brain freeze for a minute. Yikes I didn't even get a chance to correct my statement.
     
    panic mode and TAKYMOUNIR like this.
  7. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    :) NP (no problem). I'm not too bright myself with C++, even if I try to improve (reading a lot of websites and also a few paper books).
     
    TAKYMOUNIR likes this.
  8. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    Code ( (Unknown Language)):
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <math.h>
    4. int main()
    5. {
    6.    int x,y,w,z,f1,f2;
    7.    printf("enter four numbers");
    8.    scanf("%d%d%d%d",x,y,w,z);
    9.    f1=x-y-w-z;
    10.    f2=x-y-(w+z);
    11.    printf("numbers f1 is %d and  f2 is %d",f1,f2);
    12.     return 0;
    13. }
     
  9. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    You still need to make it
    Code ( (Unknown Language)):
    1.  
    2.    scanf("%d%d%d%d", &x, &y, &w, &z);
    3.  
    Personally I think spaces between the list elements makes it easier to read, but you don't have to.
     
    TAKYMOUNIR likes this.
Loading...