Rice Rice - 2 months ago 11
Linux Question

G++ Compiles, Executable Won't Run

I've been using a text editor and g++ to compile and run programs for a while, but for some reason, it stopped working today.

It will compile without errors but when I try to run the executable, it doesn't do anything. No errors, no output. Nothing. This is the code I'm trying to run.

#include <iostream>
#include <fstream>

int main(){
// Initializes variable.
int coordinatePair;
// Creates an object to use for the file.
std::ofstream fileReader;
// Initiates a for-loop to get the value of each variable.
for(int i = 0; i == 5; i++){
std::cout << "Please enter the x-coordinate of your coordinate pair. " << std::endl << "You have " << 5 - i << " pairs left to enter." << std::endl;
std::cin >> coordinatePair;
// Opens the file.
fileReader.open("points.txt");
// Writes the user's values to the file.
fileReader << coordinatePair << std::endl;
// Closes the file.
fileReader.close();
}

}


In the terminal, I created a directory to the file location...

cd ~/file location


And then I compiled it.

g++ points_out.cpp -o points_out


And I tried to run it.

./points_out


And nothing happens. No error messages, no output, no nothing. While the code isn't exactly efficient (I'm probably better off opening and closing the file outside of the for-loop.) it should still run. I tried putting in incorrect code to see what would happen, and it gave me the proper error codes. I also tried

g++ -W -Werror points_out.cpp


...and that didn't give me any error codes either. I tried making a new directory to a different .cpp file I had compiled and ran earlier this week and it worked just fine. For some reason, this one just will not run. And I'm confident that it is compiling because the executable is being created. I'm also confident that it is not running, because the text file is not being created.

I got this thing to compile successfully and run once and it created the text file when it did. This is the first time I've run into issues with g++. I've looked everywhere for a solution but most of the people who had this problem just weren't doing the

./points_out


part or had issues with MingW or needed to uninstall Avast. None of these are the issue I have so I'm at a loss.

Also, not sure if this will help, but running

g++ --version


gets me the following output:

g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Answer

The behavior is normal for a program doing nothing and completely successfully.

The C++ for-loop evaluates the loop condition before entering the loop body. When the loop body is completed, the optional post-loop expression is evaluated and the program returns to the conditional check.

So your code assigns the value 0 to i and then tests to see if i == 5 before allowing entry into the loop.

It's not entirely clear what you were intending, but you probably mean't

for (int i = 0; i < 5; i++)

We can demonstrate that quickly with this piece of code:

#include <iostream>

int main() {
    std::cout << "original loop:\n";
    for (int i = 0; i == 5; i++)
        std::cout << i << '\n';
    std::cout << "corrected loop:\n";
    for (int i = 0; i < 5; i++)
        std::cout << i << '\n';
}

Live demo: http://ideone.com/O0Ul2z

-- Addendum --

Most programmers would have thought to add something like a no-frills, no-conditions

std::cout << "hello world\n";

at the top of their program to verify that it wasn't running vs not working.