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?
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
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?
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;
}
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.
Quote:
the internet according to google agrees.
Show me.
Quote:
If you were correct then why does the integer division operator exist at all in C++
It doesn't.
Re: VS's integer division operator?
could the confusion possibly be in managed C++ (aka .NET C++, versus regular unmanaged traditional C++ syntax)??
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.