Skip to content

C++ ​

Version ​

Running C++ on C++ v17

Supported languages ​

C++

Testing framework ​

catch2

Special reminders and implementation details ​

cpp
#include <catch2/catch_test_macros.hpp>
#include <iostream>
#include "app.hpp"
using namespace std;

TEST_CASE("Should return Hello World!") {
    REQUIRE(getHelloWorld() == "Hello, World!");
}

Included libraries ​

Reading input (stdin) ​

Programs that read values use standard input. Run opens an interactive terminal: the program starts, prints its prompt, and you type the answer and press Enter, as in a real terminal. The tests of an output-test challenge feed the input themselves.

cpp
#include <iostream>
#include <string>

int main() {
    std::string name;
    std::getline(std::cin, name);
    std::cout << "Hello, " << name << "!" << std::endl;
    return 0;
}

std::getline reads one line without its newline; std::cin >> n reads a number. At end-of-file the stream's fail bit is set and the variable stays empty. Do not print a prompt before reading in output-test challenges: it would become part of the compared output.

How to debug ​

Debugging C++ code is crucial for every developer. One of the simplest yet effective ways to debug is by using std::cout statements.

Steps to Debug Using std::cout Statements ​

  1. Identify the Problem Area: Locate the section of your code where you suspect the problem might be.

  2. Insert std::cout Statements: Add std::cout statements before and after the suspected problematic code to print out variable values and flow control.

  3. Check the Results Output of your tests

  4. Analyze the Output: Look at the values printed on the results console to understand what's going wrong in your code. Adjust your code based on the findings and repeat as necessary.

Example ​

Here’s an example of how to use std::cout for debugging:

C++ Code ​

cpp
#include <iostream>

int calculate_sum(int a, int b) {
    std::cout << "calculate_sum called with arguments: " << a << ", " << b << std::endl; // Log function call
    int sum = a + b;
    std::cout << "Sum after addition: " << sum << std::endl; // Log sum calculation
    return sum;
}