-
Loops
#include "stdafx.h"
#include <iostream.h>
int main(int argc, char* argv[])
{
// declare j as an integer
__int64 j;
// print the value of j incrementing by one until j exceeds 3
for (j = 3456958; j <= 92233720368547758; j= j+1)
{
cout<< j <<endl;
}
return 0;
Why won't this work?
Please help
-
You need to close main with }. And for what purpose do you need a loop like that?
-Prelude
-
correction
i forgot to type in the closing brace "}" but when i try to compile it the error message is on the cout line that<< operator is ambiguous i am using Microsoft Visual Studio 6.0 and my teacher doesn't know the answer.
-
I don't think that the << knows what to do (or how to properly overload) when it sees the __int64 variable. If you just declare j as a normal 32bit integer, it works just fine, which shows that the operator has a problem with the 64 one.
You probably need another way of outputting the 64-bit integer. Or maybe it's a compiler issue? I have no clue considering I've never dealt with 64-bit.
Good luck
-
Ah, I see. I guess I should compile more often to see what the real problem is. This is a bug in MSVC++, and it's a very obscure one at that. MS fixed it in VS.NET, but older versions will still have the problem. To fix it you have to redefine operator<< for ostream:
Code:
#include<iostream>
using namespace std;
#define COUTFIX
#ifdef COUTFIX
std :: ostream& operator<< ( std :: ostream& os, __int64 i )
{
char buffer[BUFSIZ];
sprintf ( buffer,"%I64d", i );
os<<buffer;
return os;
}
#endif
int main ( void ) {
__int64 j;
for ( j = 3456958; j <= 92233720368547758; j++ ) {
cout<< j <<endl;
}
return EXIT_SUCCESS;
}