online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
// This file provides a "first program" in C++. #include <iostream> // ^-- this is a "preprocessor directive", which tells the compiler to include // the contents of the system header iostream. It contains declarations // of the standard input/output streams (std::cin, std::cout & std::cerr), // which are used to read and write data from the terminal. #include <string> // ^-- this is another system header, which contains declarations of the // string type, which is used to represent strings of characters. // This is an example of a function definition, this function takes an integer // as input and returns a boolean (true or false) as output. bool isPrime(int n) { if (n < 2) { return false; } // Try dividing by all numbers from 2 to sqrt(n). for (int i = 2; i*i < n; ++i) { if (n % i == 0) { return false; } } // If we get here, we didn't find a factor, so n is prime. return true; } int main() { std::cout << "What is your name? (one word only please) "; // ^-- to print things out, we use std::cout (the output stream // representing the default output device, which is usually the // terminal) with the << operator, which is known as the "stream // insertion operator". The << operator is overloaded for many // different types, so we can use it to print out strings, ints, // doubles, etc. std::string name; // ^-- this declares a variable named "name" of type std::string. std::cin >> name; // ^-- to read things in, we use std::cin (the input stream representing // the default input device, which is usually the terminal) with the // >> operator, which is known as the "stream extraction operator". // The >> operator is overloaded for many different types, so we can // use it to read in strings, ints, doubles, etc. // Note that the line above reads a single word, if we wanted to read // the whole line, we could instead use: // std::getline(std::cin, name); std::cout << "Hello, " << name << "!" << std::endl; // ^-- the << operator can be used multiple times in a single statement, // so we can print out multiple things in a single line. This works // because the << operator returns the same stream that it was called // on, so we can chain multiple << operators together. // std::endl is a special value that represents the end of a line. std::cout << "What is your favorite number? "; int number; std::cin >> number; std::cout << "Your favorite number is " << number; // ^-- we've applied the same ideas to reading in an integer and printing // it out, notice that we didn't need to use std::endl here because // we're going to print out more stuff on the same line. if (isPrime(number)) { std::cout << ", and that's a prime number!" << std::endl; } else { std::cout << ". It's not a prime number, but that's okay!" << std::endl; } return 0; // ^-- main must return an integer, and 0 is the standard value to return // if the program exits successfully. Any other value indicates that // the program exited with an error. }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue