C++ Basic Math

email me

Compiled in Visual Studio 2017.

#include "stdafx.h"
#include<iostream>
#include<string>

using namespace std;

// METHOD 1
void messages (void)
{
	// initialize strings
	string xString1;

	// output message
	xString1 = "Doing basic math. \n\nLet's try some math in the console.\n";
	cout << xString1 << endl;

}

// METHOD 2
void addition (void)
{

	// initialize variables
	int xInt1, xInt2, xResult;

	xInt1 = 9;
	xInt2 = 4;

	// do some addition
	xResult = xInt1 + xInt2;		

	// do some casting
	std::string castInt1 = std::to_string(xInt1);
	std::string castInt2 = std::to_string(xInt2);

	std::string output = "\nAddition: ";
	output += castInt1 + " + " + castInt2 + " = " + std::to_string(xResult);

	cout << output << endl;

}

// METHOD 3
void subtraction(void)
{

	// initialize variables
	int xInt1, xInt2, xResult;

	xInt1 = 9;
	xInt2 = 4;

	// do some subtraction
	xResult = xInt1 - xInt2;

	// do some casting
	std::string castInt1 = std::to_string(xInt1);
	std::string castInt2 = std::to_string(xInt2);

	std::string output = "\nSubtraction: ";
	output += castInt1 + " - " + castInt2 + " = " + std::to_string(xResult);

	cout << output << endl;

}

// METHOD 4
void multiplication(void)
{

	// initialize variables
	int xInt1, xInt2, xResult;

	xInt1 = 9;
	xInt2 = 4;

	// do some multiplication
	xResult = xInt1 * xInt2;

	// do some casting
	std::string castInt1 = std::to_string(xInt1);
	std::string castInt2 = std::to_string(xInt2);

	std::string output = "\nMultiplication: ";
	output += castInt1 + " x " + castInt2 + " = " + std::to_string(xResult);

	cout << output << endl;

}

// METHOD 5
void wait(void)
{
	string xString2;

	// wait for keypress
	xString2 = "\n\nPress any key to continue...";
	cout << xString2 << endl;
	cin.get();

}

// ENTRY POINT - PROGRAM GOVERNOR
int main()

{
	// call messages
	messages();

	// call addition
	addition();

	// call subtraction
	subtraction();

	// call multiplication
	multiplication();

	// call wait
	wait();

}

Notes

std::to_string

string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
Convert numerical value to string

Returns a string with the representation of val.

The format used is the same that printf would print for the corresponding type:

type of valprintf
equivalent
description
int"%d"Decimal-base representation of val.
The representations of negative values are preceded with a minus sign (-).
long"%ld
long long"%lld
unsigned"%u"Decimal-base representation of val.
unsigned long"%lu
unsigned long long"%llu
float"%f"As many digits are written as needed to represent the integral part, followed by the decimal-point character and six decimal digits.
inf (or infinity) is used to represent infinity.
nan (followed by an optional sequence of characters) to represent NaNs (Not-a-Number).
The representations of negative values are preceded with a minus sign (-).
double"%f
long double