|
-
Feb 28th, 2007, 04:54 PM
#1
Thread Starter
Hyperactive Member
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 
-
Feb 28th, 2007, 05:05 PM
#2
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.
-
Feb 28th, 2007, 05:19 PM
#3
Thread Starter
Hyperactive Member
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 
-
Feb 28th, 2007, 06:02 PM
#4
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.
-
Mar 1st, 2007, 07:25 AM
#5
Re: VS's integer division operator?
 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.
-
Mar 9th, 2007, 02:29 PM
#6
Re: VS's integer division operator?
could the confusion possibly be in managed C++ (aka .NET C++, versus regular unmanaged traditional C++ syntax)??
-
Mar 9th, 2007, 06:37 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|