Thread: Divide fail

  1. #1
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134

    Divide fail

    Well ever other operator works instead of divide works. When I add multiply or subtract I get the correct results but when I divide I get some answer that aint numbers, works or even english.

    Code:
    	else if(check == '/')
    	{
    		printf("Enter the first number.\n");
    		scanf("%d", &num1);
    		fflush(stdin);
    		printf("Enter the second number.\n");
    		scanf("%d", &num2);
    		fflush(stdin);
    		
    		printf("The answer is: %f", num1 / num2);
    	}

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    num1 and num2 are both integers so the result of the division is an int too. "%f" expects a float
    Kurt

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,178
    SourceForge.net: Fflush - cpwiki

    It also might have something to do with your mixed use of integers (%d) and floats (%f).

    num1 / num2 will be done in integer arithmetic, and won't be magically converted to float before trying to print it.
    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.

  4. #4
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    so If I added a variable and saved it in it would be printed as a float?

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by BIGDENIRO View Post
    so If I added a variable and saved it in it would be printed as a float?
    Paste your full code so we can highlight the mistake for you. As mentioned above, it may be due to the fflush or the way you have declared your variables.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int a = 6, b = 5;
    
    	printf("%f", a/b);
    
    	getchar();
    	return 0;
    
    }
    I have declared a and b as int, but am trying to print a float. Do you have it something similar to the above?
    Last edited by bos1234; 09-29-2013 at 06:10 PM.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,656
    Try printf("%f", (float)a / (float)b);

  7. #7
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by rcgldr View Post
    Try printf("%f", (float)a / (float)b);
    You don't have to explicitly promote both a and b

  8. #8
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    Quote Originally Posted by bos1234 View Post
    Paste your full code so we can highlight the mistake for you. As mentioned above, it may be due to the fflush or the way you have declared your variables.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int a = 6, b = 5;
    
        printf("%f", a/b);
    
        getchar();
        return 0;
    
    }
    I have declared a and b as int, but am trying to print a float. Do you have it something similar to the above?
    look at the first post

  9. #9
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by BIGDENIRO View Post
    look at the first post
    And rc answered... try printf("%f\n", (float)a/b);

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,656
    Quote Originally Posted by rcgldr View Post
    Try printf("%f", (float)a / (float)b);
    Quote Originally Posted by SirPrattlepod View Post
    You don't have to explicitly promote both a and b
    True, but it might help the original poster understand the issue better rather than relying on promotion of "b" to a float because "a" was cast to a float.

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by rcgldr View Post
    True, but it might help the original poster understand the issue better rather than relying on promotion of "b" to a float because "a" was cast to a float.
    You're really just being explicit, since the compiler will certainly have to promote both whether you write it that way or not.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you Divide in C?
    By Nothing595 in forum C Programming
    Replies: 3
    Last Post: 10-02-2010, 05:32 PM
  2. fail to count digit of an integer (fail at 9)
    By azsquall in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2008, 09:42 AM
  3. divide by zero
    By George2 in forum C# Programming
    Replies: 17
    Last Post: 05-02-2008, 02:37 AM
  4. Divide by 3 in ISR
    By Roaring_Tiger in forum C Programming
    Replies: 40
    Last Post: 08-21-2004, 11:51 AM
  5. divide
    By Luigi in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2003, 07:38 PM