1

I have a simple program in Xcode 8.3

#include <iostream>
using namespace std;

void reverseString(int);


int main(int argc, const char * argv[]) {

    reverseString(123456);
    return 0;
}

void reverseString(int numString){
    
    if(numString < 10){
        cout<<numString;
    }else{
        cout<<numString % 10;
        reverseString(numString/10);
    }
}

The code just prints out a number in its reverse order.

When I run the program in Xcode I get

Program ended with exit code: 0

Rewrite the program with a print statement in the main function like so:

int main(int argc, const char * argv[]) {

cout<<"Hello";
reverseString(123456);
    return 0;
}

I get the same output

Program ended with exit code: 0

If I add \n to the send of the print statement cout<<"Hello\n";

I get:

Hello

Program ended with exit code: 0

What is going on here? I recently updated to Xcode 8.3, could that be causing the issue? If so, how can I fix this?

Note: I am creating my project by selecting file -> new project -> MacOS -> Command Line Program

Also note: Programs I've created in the past still run properly.

1 Answer 1

2

cout won't print anything untill you send a "\n", so at some point in your code you need to do cout << "\n";

Sign up to request clarification or add additional context in comments.

5 Comments

Is there a reason why that is? Thats specific to Xcode correct? Other IDEs will print the statement correctly without the cout<<"\n"
I don't believe it it specific to Xcode. The purpose is to allow you to build up a string for output to the standard output terminal from multiple lines of code, with the line only being emitted once it is terminated with \n (or std::endl )
You could always call cout.flush() to force the output, out.
But other IDEs print the statement correctly, and so does terminal on Mac. Do they just input it themselves somehow?
It is interesting that it works on other IDEs (compilers, whatever) but it remains true that to guarantee the output you either need to use the end-of-line terminator, or flush the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.