1

I am new to both C++ and TextMate, and I'm running into a problem. When I run my extremely basic "programs" in TextMate using C++, it completely ignores the statement "cout >>" and just either ignores it, or inputs the maximum data value for the basic type I used.

What is going on, and how can I stop it?

The source code is as follows:

#include <iostream>
int stonetolb(int);
int main() {
    using namespace std;
    int stone;
    cout << "enter the weight in stone";
    cin >> stone;
    int pounds = stonetolb(stone);
    cout << stone << "stone = ";
    cout << pounds <<" pounds.";
    return 0;
}

int stonetolb(int sts) {
    return 14 * sts;
}

… and the output, regardless of whether or not I even try to put in a number is:

enter the weight in stone0stone = 0 pounds.

8
  • What if you try the very same code in XCode? And have you tried to compile via command line (e.g. "g++ test.cpp")?
    – gd1
    Nov 6, 2011 at 14:09
  • I don't think TextMate is supposed to do that (i.e. wait for your input). The better solution would be to open the compiled program in a Terminal.
    – slhck
    Nov 6, 2011 at 14:15
  • ok... i tried to compile via command line, and i get this file called a.out that i cant open, but nothing else happens... Nov 6, 2011 at 14:26
  • To run the that file. Try ./a.out in the command line.
    – whirlwin
    Nov 6, 2011 at 14:32
  • 1
    NEVER, NEVER start programming with an IDE or anything like that. It makes you loose 5-6 months in doing rubbish. Go basic text editor and a terminal. Start w/ the basics.
    – gd1
    Nov 6, 2011 at 18:10

1 Answer 1

4

You need to run the compiled file from a Terminal. Open Applications » Utilities » Terminal.app, use cd to get to the directory you wrote the file in, and compile it.

In the easiest case, this is:

g++ test.cpp

Then, run the program with:

./a.out

Note that this works for single files only, not whole projects. But given the scope of your problem, that goes too far. You will eventually learn about advanced compiling and "makefiles" soon.


Why is that, even?

The problem is that TextMate does not wait for any input. This is because it can't read input without any additional library that needs to interact with the system. This TextMate blog entry about interactive input explains more:

Prior to r1509 TextMate had a very neat feature: you could run (⌘R) a piece of C, Ruby, Python, Shell Script, etc. and if the code (directly or indirectly) read from its standard input then a dialog would be shown prompting the user for input.

For systems prior to Snow Leopard (10.6), a fix was possible. TextMate 2 is supposed to be able to do that again, but who knows when it'll come out.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .