Thread: Diff compiler == diff program????

  1. #1
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    Diff compiler == diff program????

    This is REALLY starting to bug me. Really.

    The folling code, under:

    gpp, compiles and removes the tenth integer form a space delimited list of integers in file.txt

    Dev-C++, does nothing at all

    Turbo, deletes the contents of the file and replaces it with meaningless data.

    WHYYYY?????

    Is this not ANSI compilant? Anyone????

    Code:
    /*
       Kth Integer remover
    
    This program reads all integers up to and including the Kth, but no more, from a text file called file.txt. It then eliminates the Kth integer. No other numbers are read or otherwise modified.
    
    Prerequisite: A space delimited integer file called file.txt exists in the cwd.
    
    Note: It may be helpful to, when making the file, make one line of numbers, then another identical line below it. This allows you to better see the removal of the number.
    */
    
    
    #include <iostream.h>
    #include <fstream.h>
    #include <math.h>
    
    
    //This integer is to be removed from the file, bye bye!
    #define Kth_integer 10
    
       int lenof(int);
    
       int main () {
          bool stop = false;
          int b = 0;
          int inums[Kth_integer];
          ifstream ifile("file.txt", ios::in);
          for (int b = 1; b <= Kth_integer; ++b)  {
             ifile >> inums[b];
          }
          ifile.close();
          ofstream ofile("file.txt", ios::in);
          for (int b = 1; b < Kth_integer; ++b)  {
             ofile << inums[b];
             ofile << ' ';
          }
          for (int x = 0; x < lenof(inums[Kth_integer]); ++x) {
             ofile << ' ';
          }
          ofile.close();
          return 0;
       }
    
       int lenof (int a) {
          int b = 1;
          while (a/pow(10, b) >= 1) {
             ++b;
          }
          return b;
       }

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    for starters, bool isn't a standard data type. it's compiler dependant, i think. can anyone confirm this? i remember djgpp didn't accept bools. (or hex notation, for that matter)

  3. #3
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Oh, well that ought not to even matter, the boolean sentry value was not used in the program. I'll see if removing it has any effect.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    bool is standard C++.

    This is bad, your off by one..

    int inums[Kth_integer];

    for (int b = 1; b <= Kth_integer; ++b) {
    ifile >> inums[b];
    }

    Should start at 0 and go < Kth_integer.

    I don't know if this has any effect, but why do you pass in the ios::in flag to an ostream?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Passing an ios::in to an ostream leaves the ostream's put iterator at the very beginning of the file, allowing to to be overwritten, rather than appended to or truncated.

    As for skipping the array's zero, I just did that for notational convenience, inums[1] is the first number, and inums[Kth_integer] is the Kth number, rather than being one lower.

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You can't change the fundamentals of the langauge..

    const int k = 10;
    int a[k];
    a[k]; // invalid, greatest valid index is k-1
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  7. #7
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    ACH!!!

    Thats probably it!!!

    Thanks, I'll try that with k+1.

  8. #8
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Nope. That as written compiles fine on gpp, and works. If I make is int inums[Kth_integer + 1]; it still compiles and runs fine on gpp, but neither does anything on Dev-C++.

    Any borland/msvc users care to take it for a spin? Maybe its just Dev-C++ and Turbo.

  9. #9
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    im not sure if this will work but try adding ios::app to the open mode, this shouldn't be needed ut its worth a try.

    im not up on C++ streams so this is just a guess on a buggy implementation.

    ::edit:: try also ios::binary the mode can be done differently than text mode on some compilers!
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  10. #10
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    no-one, opening the ofstream with the append flag would place the put pointer at the end of the file, so instead of overwriting Kth digit with blanks, it would append all the numbers up to k to the end. This, unfortunately, would not be effective.

    Thanks for the time, though!

  11. #11
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    What I just can't get is how DevC++ and gpp can treat what I thin ought to be somehwta low level implementation-independant code differently.

    Can anyone pick out something from that prog that is implementation-dependant? Prelude?

  12. #12
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    um seek your way back...is what im saying, i thought you said that turbo fraps the file contents with jibberish, appending hopefully prevents this.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  13. #13
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Seeking, perhaps, might be a solution. but then, why does it work under gpp????????

  14. #14
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    stick with what works i guess...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  15. #15
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Problem being that I am trying to prove to someone that this can, in fact, be done. She uses turbo. My little proggie was not that impressive due to the different output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 09:44 PM
  2. Tic Tac Toe Program Question
    By Unknowntoyou000 in forum C++ Programming
    Replies: 24
    Last Post: 04-10-2008, 08:55 PM
  3. Looking for feedback on program segment
    By avron in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 04:38 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. swapping elements in a 2D array
    By axon in forum C++ Programming
    Replies: 9
    Last Post: 03-10-2003, 02:18 PM