Thread: Loops

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    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

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You need to close main with }. And for what purpose do you need a loop like that?

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    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.

  4. #4
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    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

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple thread for loops
    By lehe in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2009, 12:01 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM