Thread: modulus will not work

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    41

    Angry modulus will not work

    I understand the entire thing about modulus gives you the remainder, yada yada. THis is what I complied, and this is what I get.
    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
      float num1, num2, mod;
    
      printf("Please type in a number: ");
      scanf("%f%",&num1);
      printf("Please type in a number: ");
      scanf("%f",&num2);
      double fmod(double num1, double num2);
    
     printf("%f divided by %f is remainder %f\n", num1, num2, mod);
    
      return 0;
    }
    I type cc -lm -o mod2A.out mod2A.c
    Code:
     Please type in a number: 9
    Please type in a number: 7
    9.000000 divided by 7.000000 is remainder 0.000000
    that isn't the remainder, where did i go wrong????

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Well, simple enough, you never set the variable mod to anything. I think you are trying to use the modulus operator inside the first scanf() call which is very weird. You would need to take both first numbers in input and only then assign the result of the modulus operator to the variable mod. However, you can only use the modulus operator on integers.

    By the way, what in hell are you trying to do with that fmod() declaration ?

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Get the return value of the function fmod and store in mod. and print mod. more over you are suppose to send only the variables to the function not the variable type along with it as an argument to the function fmod. have a look at the modified code below, it will make you more sense

    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
      float num1, num2, mod;
    
      printf("Please type in a number: ");
      scanf("%f%",&num1);
      printf("Please type in a number: ");
      scanf("%f",&num2);
      
      mod = fmod(num1,num2);
    
     printf("%.2f divided by %.2f is remainder %.2f\n", num1, num2, mod);
     
    
     getchar();
      return 0;
    }
    
    /*my output
    Please type in a number: 9
    7
    Please type in a number: 9.00 divided by 7.00 is remainder 2.00
    
    */
    ssharish2005
    Last edited by ssharish2005; 09-28-2006 at 03:33 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by Desolation
    Well, simple enough, you never set the variable mod to anything. I think you are trying to use the modulus operator inside the first scanf() call which is very weird. You would need to take both first numbers in input and only then assign the result of the modulus operator to the variable mod. However, you can only use the modulus operator on integers.

    By the way, what in hell are you trying to do with that fmod() declaration ?
    Assignment for school

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by ssharish2005
    Get the return value of the function fmod and store in mod. and print mod. more over you are suppose to send only the variables to the function not the variable type along with it as an argument to the function fmod. have a look at the modified code below, it will make you more sense

    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
      float num1, num2, mod;
    
      printf("Please type in a number: ");
      scanf("%f%",&num1);
      printf("Please type in a number: ");
      scanf("%f",&num2);
      
      mod = fmod(num1,num2);
    
     printf("%.2f divided by %.2f is remainder %.2f\n", num1, num2, mod);
     
    
     getchar();
      return 0;
    }
    
    /*my output
    Please type in a number: 9
    7
    Please type in a number: 9.00 divided by 7.00 is remainder 2.00
    
    */
    ssharish2005
    Thank you so much, it does make sense. I did leave out the getchar part, becuase that wasn't in the book for the code we had to rewrite, and it still worked. I did the mod before, but I kept thinking i needed the double num1, because that's what it said everywhere else to do it. I never tried the
    Code:
    mod = fmod(num1,num2) i kept just trying things like mod = num1 / num2;
    and so on.

  6. #6
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    You might want to consider not using "mod" as a variable name too, as there is a function in the maths library with the same name and it might get a tad confusing.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well if we're going to be concerned with libraries, "mod" isn't an ANSI standard function, nor is it a reserved keyword, so all bets are off.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM