Thread: Dividning in cpp

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    12

    Dividning in cpp

    How do i divide in c++. I ask because my program doesnt give me the right output. I use the dev compiler ver 5 beta.
    example:
    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    
    using namespace std;
    
    int main()
    {
         float a;
         int x, y;
         cout<<"enter two numbers:";
         cin>>x>>y;
         ofstream file("a.txt",ios::app);
         a = x/y;
         file<<a;
    }
    I get this:
    0
    Is this right?
    Last edited by 6arredja; 11-11-2004 at 02:18 PM.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    1) You need to use code tags. Place [/code] at the endof your code and [code] at the beginning. It'll put your code in a font better suited to source code, and preserve spacing.

    2) You're mixing old-style headers with new one. If you need to include a standard C header, you omit the .h, and precede the name with a c. For example, "#include <cstdlib>". And, franky, you don't need that haeder anyway.

    3) "wrong output" is not very descriptive at all. So that people can find the source of your problem faster, please include examples of actual output and what you were expecting.
    Last edited by sean; 11-11-2004 at 01:22 PM.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I assume the problem is that it only provides the integer part of the division (for example, 10 / 3 = 3.0, or 1 / 2 = 0.0). The reason is that you are dividing integers, and so an integer is returned as a result. Just change the type of x and y to be float. If they must be int, then you must cast one of them to float in the division expression.

    Quote Originally Posted by sean_mackrory
    2) You're mixing old-style headers with new one. If you need to include a standard C header, you omit the .h, and precede the name with a c. For example, "#include cstdlib".
    Technically, it's not that big of a deal since the headers used by the OP are all standard. Also, don't forget the <> in <cstdlib>.
    Last edited by jlou; 11-11-2004 at 02:16 PM.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    And be carefful when y is 0 so your program doesn't crash.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    12

    Thanks

    thanks for the help

  6. #6
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    integer division....you can easily cast one of the variables to a float or double type and that would work too
    nextus, the samurai warrior

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Grouped CPP files
    By bobbelPoP in forum C++ Programming
    Replies: 12
    Last Post: 07-16-2008, 01:48 AM
  2. need help converting c dll to cpp
    By *DEAD* in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 10:22 PM
  3. includein cpp file in a cpp file
    By sawer in forum C++ Programming
    Replies: 7
    Last Post: 06-15-2006, 12:34 PM
  4. Replies: 2
    Last Post: 04-09-2006, 07:20 PM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM