C++ mathematics????

This is a discussion on C++ mathematics???? within the C++ Programming forums, part of the General Programming Boards category; Code: int a; int b; double c; //in the course of the program I got a=492 and also b=492// c=a/b; ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    C++ mathematics????

    Code:
    int a;
    int b;
    double c;
    //in the course of the program I got a=492 and also b=492//
    c=a/b;
    And for c I get 0. How could it be??????

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Apparently you don't have 492 for both a and b. Have you tried writing both values to the console just before the calculation?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Code:
    int main()
    {
    	int a = 492;
    	int b = 492;
    	double c=a/b;
    	std::cout << c << std::endl;
    	return 0;
    }
    Looks a lot like 1 to me. However, notice that you're performing integer division. To do floating point division, you'll need something like this:
    Code:
    int main()
    {
    	int a = 36;
    	int b = 492;
    	double c=(float)a/b;
    	std::cout << c << std::endl;
    	return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    If you're assigning to a double, cast to a double. And use static_cast:
    double c = static_cast<double>(a)/b;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    did you try restarting your computer? that's usually what I do
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insert node in a linked list.
    By antonis in forum C Programming
    Replies: 2
    Last Post: 10-22-2005, 02:30 PM
  2. mathematics <exponential>
    By xddxogm3 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 11-17-2004, 11:48 PM
  3. Mathematics Odd/Even Functions
    By xddxogm3 in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-18-2004, 05:35 PM
  4. rotation mathematics....
    By EvBladeRunnervE in forum Game Programming
    Replies: 11
    Last Post: 12-10-2003, 02:13 PM
  5. Highschool mathematics
    By Panopticon in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 06-09-2003, 12:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21