Inconsistency across platforms. Need help!

Helo all,

I am in an introductory programming class, and I am working on a C++ project.
The project involves reading in a list of integers from a text file, saving said list in an array, and then sorting that array into ascending order using the bubble sort methodology.

The problem I am running into is that my code so far compiles and executes fine on our classroom Windows environment, but when I go to run it on my MacBook, I have no output from the console, and it just hangs until I close the window. My assumption is that there is an inconsistency with the <fstream> library across the two platforms. It should be noted that I am using the latest version of Code::Blocks on both machines. Also, I am using the GCC compiler in OSX, and I can find out what compiler is being used in class this Friday (if the compiler is of relevance, which I think it may be).

Any help or suggestions would be greatly appreciated!

Regards,
Richard Knepp

EDIT: here is my current code in question
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

void swap (int&, int&);

int main()
{
    const int MAX_NUMS = 20;
    int numbers[MAX_NUMS];

    ifstream FILE;
    FILE.open("A7.1.txt");

    //USE FSTREAM TO READ INTEGERS FROM FILE
    for (int i = 0; !FILE.eof(); i++)
        FILE >> numbers[i];

    //PRINT THE INITIAL ARRAY
    cout << "The array as it was read:\n";
    for (int i = 0; i < MAX_NUMS; i++)
        cout << numbers[i] << " ";
    cout << endl;

    //SORT THE ARRAY IN ASCENDING ORDER
    for (int x = 0; x < (MAX_NUMS - 1); x++)
    {
        for ( int y = 0; y < (MAX_NUMS - x - 1); y++)
        {
            if (numbers[y] > numbers[y+1])
            {
                swap (numbers[y], numbers[y+1]);
            }
        }
    }

    //PRINT THE SORTED ARRAY
    cout << "The array after sorting:\n";
    for (int i = 0; i < MAX_NUMS; i++)
        cout << numbers[i] << " ";
    cout << endl;
}

void swap (int& n1, int& n2)
{
    int temp = n2;
    n2 = n1;
    n1 = temp;
}
Last edited on
If the file failed to open, or if the file read operation failed before you reached the end of the file, the EOF flag will never be set so the loop on line 18-19 will keep running forever.
Good point. I've actually thought of this before but couldn't think of anything that might be causing it. My text file name is correct, it's in the top level of the project folder, just where it was on Windows, and I'm using the same syntax, line for line. Any suggestions?
I have no idea if Code::Blocks sets the working directory the same way on all platforms. If you want to make sure you can put std::system("pwd"); at the start of main and see if it prints the project folder as you expect. Also note that filenames are case sensitive so make sure the file is not spelled a7.1.txt, A7.1.TXT, ...
Well I tried setting the executable working directory, thinking that you were on to something, to no avail. As it turns out, it was a known problem with Code::Blocks preventing the program from opening the file. I copied the text file into the debug build folder, and ran the executable from the command line, and voila! Thanks for your help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <cstdlib>
#include <fstream>

//using namespace std;

// void swap (int&, int&);

// we can use std::swap() in <algorithm>
#include <algorithm> // http://en.cppreference.com/w/cpp/algorithm/swap

int main()
{
    const int MAX_NUMS = 20;
    int numbers[MAX_NUMS];

    int nread = 0 ; // *** added: count of numbers that were actually read from file
                    // *** need to do this because the file may not have MAX_NUMS numbers

    // ifstream FILE;
    // FILE.open("A7.1.txt");
    std::ifstream FILE( "A7.1.txt" ) ; // constructor opens the file


    //USE FSTREAM TO READ INTEGERS FROM FILE
    // for (int i = 0; !FILE.eof(); i++)
        // FILE >> numbers[i];

    // idiomatic C++ input loop
    // continue with the loop only if
    //     a. there is space for more numbers in the array  and
    //     b. the input operation FILE >> numbers[nread] succeeded
    for( nread = 0 ; ( nread < MAX_NUMS ) &&  ( FILE >> numbers[nread] ) ; ++nread ) {}

    if( nread == 0 )
    {
        std::cout << "no numbers were read\n" ;
        return 0 ;
    }

    //PRINT THE INITIAL ARRAY
    std::cout << "The array as it was read:\n";
    //for (int i = 0; i < MAX_NUMS; i++)
    for( int i = 0; i < nread; ++i ) std::cout << numbers[i] << " ";
    std::cout << '\n' ; // endl;

    //SORT THE ARRAY IN ASCENDING ORDER
    for (int x = 0; x < nread ; x++)
    {
        for ( int y = 0; y < (nread-x); y++)
        {
            if (numbers[y] > numbers[y+1])
            {
                std::swap (numbers[y], numbers[y+1]);
            }
        }
    }

    //PRINT THE SORTED ARRAY
    std::cout << "The array after sorting:\n";
    for (int i = 0; i < nread; i++) std::cout << numbers[i] << " ";
    std::cout << '\n' ; // endl;
}

/*
void swap (int& n1, int& n2)
{
    int temp = n2;
    n2 = n1;
    n1 = temp;
}
*/
Topic archived. No new replies allowed.