Results 1 to 7 of 7

Thread: VS's integer division operator?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    VS's integer division operator?

    I'm writing a C++ app in Visual Studio 2005 and when I put:

    itotalpennies = dchange * 100;
    dtemp = itotalpennies/500;
    itemp = itotalpennies\500;

    it stops compiling with an error on that last line saying illegal escape character. Yeah they're the escape character in strings but I'm not using strings so what's up?
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: VS's integer division operator?

    What's up is that you're trying to write VB code and compile it with a C++ compiler. No wonder you don't like the language.

    In C++, / is the division operator, period. Integer division happens when all arguments are integer types. Floating point division happens when all arguments are floating point types. Any other kind of division might happen when the arguments are class instances that overload the operator.
    Code:
    int itotalpennies = whatever;
    int itemp = itotalpennies / 500; // Integer division
    double dtemp = itotalpennies / 500.0; // Floating point division
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: VS's integer division operator?

    our teacher specifically said that if you use the integer division operator it will take any data type and do integer division and the internet according to google agrees.
    If you were correct then why does the integer division operator exist at all in C++ if you get the same result using the regular division one by using integer values? So I think the integer division operator is for other data types than int...which brings me back to why doesn't it work?
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: VS's integer division operator?

    Um, CornedBee is correct. The character '\' is not an operator in C or C++.

    If you were to think about it as overloaded operators that you wrote, it would be:

    Code:
    int operator/(int lhs, int rhs)
    {
      // perform integer divison
    }
    
    double operator/(double lhs, double rhs);
    {
      // perform floating point division
    }
    When you use the division operator with integers, it does integer division. When you use it with floats or doubles, it does floating point division. There really isn't any simpler way to explain it. Try it yourself:



    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       double dx = 100.0, dy = 7.0;
       int ix = 100, iy = 7;
       cout << "integer division: " << ix << "/" << iy << "=" << (ix/iy) << endl;
       cout << "decimal division: " << dx << "/" << dy << "=" << (dx/dy) << endl;
      
       return 0;
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: VS's integer division operator?

    Quote Originally Posted by Desolator144
    our teacher specifically said that if you use the integer division operator
    There is no such thing. There's just the division operator.

    the internet according to google agrees.
    Show me.

    If you were correct then why does the integer division operator exist at all in C++
    It doesn't.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: VS's integer division operator?

    could the confusion possibly be in managed C++ (aka .NET C++, versus regular unmanaged traditional C++ syntax)??

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: VS's integer division operator?

    No. C++/CLI doesn't have a specific integer division operator either. This kind of nonsense only makes sense in weakly typed languages like VB.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width