A
A
avion2018-09-19 19:05:11
C++ / C#
avion, 2018-09-19 19:05:11

Printing a message about the successful completion of a program in c++?

Hello, after executing the code on macos, a command appears about the successful completion of the function in the middle of the program. But this is not the case only on macos in windows.
PS
IDE: Xcode
Code:

#include <iostream>
#include <cstdlib>

using namespace std;

int sum(int n) {
    int sum = 0;
    while (n) {
        sum += n;
        n--;
    }
    return n + sum;
}

int main() {
    int n;
    
    cout << "Кол-ва нат чис: ";
    cin >> n;
    
    cout << sum(n);

    return 0;
}

Program output for n = 10:
Кол-ва нат чис: Program ended with exit code: 010
55

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2018-09-19
@Mercury13

The issue is buffering I/O streams. Of course, these details are arranged differently on Windows and OSX. Two modifiers, std::endl and std::flush, flush buffers.

cout << "Кол-ва нат чис: " << std::flush;
    cin >> n;
    
    cout << sum(n) << std::endl;

V
Victor Bomberow, 2018-09-22
@majstar_Zubr

Windows just won’t say the exit code, you need to pull it out, for example, like this:

@echo off
 
rem A batch file which captures the app's return value.

set arg1=%1
rem using for executable

set arg2=%2
rem using for argument #1

shift
shift
rem Abling to use bat with arguments

%1 %2

@if "%ERRORLEVEL%" == "0" goto success
 
:fail
  echo This application has failed!
  echo return value = %ERRORLEVEL%
  goto end
:success
  echo This application has succeeded!
  echo return value = %ERRORLEVEL%
  goto end
:end
echo All Done
exit /B %ERRORLEVEL%
/code>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question