Thread: can't find why this code ain't working!

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    25

    Question can't find why this code ain't working!

    Hi,
    This is a program finds out the date for Eastern. I get these two errors when compiling:
    -invalid operands to binary % (line 14 and 16)
    -int format,double arg<arg 2> (line 19)
    I don't know how to fix it. Does anyone have a tips on this?
    Code:
    #include<stdio.h>
    #include<stdbool.h>
    #include<math.h>
    int main()
    {
      int year;
      float A,B,C,S,D,T,E,date;
      printf("Choose a year between 1900 to 2099");
      year=getchar();
      A= year % 19;
      B= year %4;
      C= year % 7;
      S=19*A+ 24;
      D=S % 30;
      T=2*B+4*C+6*D+5;
      E=T % 7;
      date=22 + D + E;
      if(date==32){
        printf("The eastern's date is:%d",date);
      }
      else if(date!=32){
        date=D+E-9;
        if(date==26){
          printf("The eastern is April 19th.");
        }
        if(date==25&&A==16&&D==28){
          printf("The eastern is April 18th.");
        }
    
    
      }
    
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    -invalid operands to binary &#37; (line 14 and 16)
    You can't perform modulo arithmetic on a float (why are they floats anyway)

    -int format,double arg<arg 2> (line 19)
    Read the manual page to find out what formats correspond to what types.

    Your attempt to read a year needs work.

    Can you think of better variable names for some of those single letter identifiers?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    25
    Tnx for the help.
    I should have used int instead of float.(right?) cause now I don't get that error longer.
    And I used scanf to read "year" instead of getchar. But now I get this error :
    "format argument is not a pointer."
    what does it mean?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Basically, you have to pass the variable's memory address to scanf() using the & operator. So the call will be:
    Code:
    scanf("&#37;d", &year);

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Let me clarify that a bit more. Each variable is stored in memory and referenced using a memory address. When you call a function, you actually pass it's value, not the variable itself. So the function can't modify that variable.

    A pointer points to a variable's memory address. It's operator is '&'. So when you want to pass the memory address of a variable to a function, you'll have to precede it with '&'.

    Another way to do it is using a pointer variable. This is a variable which stores the memory address of another variable. A pointer variable is declared using
    Code:
     type *name
    where type is a type of variable and name is the name of the pointer variable.
    You then must assign the pointer variable the address of the original variable. It will look like this:
    Code:
    ptr_variable = &variable
    Now you can call scanf and pass it the pointer variable without the & operator:
    Code:
    scanf("&#37;d", ptr_variable);
    Hope I did a good job explaining it. If anyone can fix this up anymore than I can, please feel free to comment.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    25
    ooops! I was being careless forgetting "&" in scanf.
    but tnx to your explanations I know now why I should use it =)
    but there's another problem too. When the program asks abt the year, It just reads it and jumps out of the program.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    25
    tnx for d link, very helpful cause I get that problem alot but this time it is not the command window which disappears, it's the program that ends it self ;like if I have a break or sth in the program which I don't have. It doesn't go to any of loops at all =(

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The above link is pretty much what you want. It describes it very well. The program runs fine, but closes directly because your program ends.
    Have another look in there and look in the FAQ for ways to wait for a keypress before closing the app.

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
        }
      }
      getchar();
      return 0;
    }
    include this at the end of code. One to read a char from the stdin. So that it will wait until a key is press and the main should return a value.

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM
  2. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  3. Sphere code not working
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 10-03-2004, 07:29 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 08:50 PM