pennie and dollar counter

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 3 Exercise 11
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 3 Exercise 11

#include "std_lib_facilities.h"

int main()
{
	int oneCentCoins = 0;
	int fiveCentCoins = 0;
	int tenCentCoins = 0;
	int twfiveCentCoins = 0;
	int ffCentCoins = 0;
	int dollars = 0;
	double total = 0;

	cout << "How many 1-Cent-Coins do you have?\n";
	cin >> oneCentCoins;
	cout << "How many 5-Cent-Coins do you have?\n";
	cin >> fiveCentCoins;
	cout << "How many 10-Cent-Coins do you have?\n";
	cin >> tenCentCoins;
	cout << "How many 25-Cent-Coins do you have?\n";
	cin >> twfiveCentCoins;
	cout << "How many 50-Cent-Coins do you have?\n";
	cin >> ffCentCoins;
	cout << "How many 1-Dollar-Coins do you have?\n";
	cin >> dollars;

	total = oneCentCoins*0.01 + fiveCentCoins*0.05 + tenCentCoins*0.10 + twfiveCentCoins*0.25 + ffCentCoins*0.50 + dollars*1.00;

	if (oneCentCoins < 2) cout << "You have " << oneCentCoins << " pennie\n";
	else cout << "You have " << oneCentCoins << " pennies\n";
	if (fiveCentCoins < 2) cout << "You have " << oneCentCoins << " nickle\n";
	else cout << "You have " << fiveCentCoins << " nickles\n";
	if (tenCentCoins < 2) cout << "You have " << oneCentCoins << " dime\n";
	else cout << "You have " << tenCentCoins << " dimes\n";
	if (twfiveCentCoins < 2) cout << "You have " << oneCentCoins << " quarter\n";
	else cout << "You have " << twfiveCentCoins << " quarters\n";
	if (ffCentCoins < 2) cout << "You have " << oneCentCoins << " half dollar\n";
	else cout << "You have " << ffCentCoins << " half dollars\n";
	if (dollars < 2) cout << "You have " << oneCentCoins << " dollar\n";
	else cout << "You have " << dollars << " dollars\n";

	cout << "the value of all your coins is " << total << " Dollar\n";
	keep_window_open();
}
Output:
How many 1-Cent-Coins do you have?
230
How many 5-Cent-Coins do you have?
34
How many 10-Cent-Coins do you have?
12
How many 25-Cent-Coins do you have?
23
How many 50-Cent-Coins do you have?
31
How many 1-Dollar-Coins do you have?
8
You have 230 pennies
You have 34 nickles
You have 12 dimes
You have 23 quarters
You have 31 half dollars
You have 8 dollars
the value of all your coins is 34.45 Dollar
Please enter a character to exit

operator and two values

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 3 Exercise 10
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 3 Exercise 10

#include "std_lib_facilities.h"

int main()
{
	string operation;
	double val1 = 0;
	double val2 = 0;
	double solution = 0;
	cout << "enter an operator and two values divided by space: ";

	while (cin >> operation >> val1 >> val2)
	{
		if (operation == "+") solution = val1 + val2;
		else if (operation == "-") solution = val1 - val2;
		else if (operation == "*") solution = val1 * val2;
		else if (operation == "/") solution = val1 / val2;
		cout << solution;
	}
	keep_window_open();
}
Output:
enter an operator and two values divided by space: - 36 27
9

spelled out number to integer

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 3 Exercise 9
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 3 Exercise 9

#include "std_lib_facilities.h"

int main()
{
	cout << "enter a spelled out integer to be converted to the number: ";
	int toBeSpelled = -1;
	string inputNumber;

	while (cin >> inputNumber)
	{
		if (inputNumber == "zero") toBeSpelled = 0;
		else if (inputNumber == "one") toBeSpelled = 1;
		else if (inputNumber == "two") toBeSpelled = 2;
		else if (inputNumber == "three") toBeSpelled = 3;
		else if (inputNumber == "four") toBeSpelled = 4;
		else if (inputNumber == "Quit" || inputNumber == "quit") return 0;
		if (toBeSpelled != -1)
			cout << "you entered number " << toBeSpelled << '\n';
		else
			cout << "I dont know that number\n";
		cout << "Please enter another number or \"Quit\"\n";
		toBeSpelled = -1;
	}
	return 0;

	keep_window_open();
}
Output:
enter a spelled out integer to be converted to the number: four
you entered number 4
Please enter another number or "Quit"

odd or even number testing

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 3 Exercise 8
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 3 Exercise 8

#include "std_lib_facilities.h"

int main()
{
	cout << "enter an integer to test if it is odd or even: ";
	int valueToTest;
	cin >> valueToTest;

	if (valueToTest % 2 == 1)
	{
		cout << valueToTest << " is an odd number\n";
	}
	else
	{
		cout << valueToTest << " is an even number\n";
	}
		
	keep_window_open();
}
Output 1:
enter an integer to test if it is odd or even: 5
5 is an odd number
Please enter a character to exit
Output 2:
enter an integer to test if it is odd or even: 4
4 is an even number
Please enter a character to exit

sorting strings

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 3 Exercise 7
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 3 Exercise 7

#include "std_lib_facilities.h"

int main()
{
	string word1 = " ", word2 = " ", word3 = " ";
	string smallest = " ", medium = " ", largest = " ";
	cout << "enter word 1, word 2, word 3 devided by space: ";
	cin >> word1 >> word2 >> word3;

	cout << "you entered three words: " << "word1: " << word1 << ", word2: " << word2 << ", word3: " << word3 << "\n";

	if (word1 <= word2 && word1 <= word3)
	{
		smallest = word1;
		if (word2 <= word3)
		{
			medium = word2;
			largest = word3;
		}
		else
		{
			medium = word3;
			largest = word2;
		}
	}
		
	if(word2 <= word1 && word2 <= word3)
	{
		smallest = word2;
		if (word1 <= word3)
		{
			medium = word1;
			largest = word3;
		}
		else
		{
			medium = word3;
			largest = word1;
		}

	}

	if(word3 <= word2 && word3 <= word1)
	{
		smallest = word3;
		if (word1 <= word3)
		{
			medium = word1;
			largest = word2;
		}
		else
		{
			medium = word2;
			largest = word1;
		}
	}

	cout << smallest << ", " << medium << ", " << largest << "\n";

	keep_window_open();
}
Output:
enter word 1, word 2, word 3 devided by space: how are you
you entered three words: word1: how, word2: are, word3: you
are, how, you
Please enter a character to exit

sorting integers

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 3 Exercise 6
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 3 Exercise 6

#include "std_lib_facilities.h"

int main()
{
	int val1, val2, val3;
	int smallest = 0, medium = 0, largest = 0;
	cout << "enter tree integer value ";
	cin >> val1 >> val2 >> val3;
	cout << "you entered three values: " << "val1: " << val1 << ", val2: " << val2 << ", val3: " << val3 << "\n";

	if (val1 <= val2 && val1 <= val3)
	{
		smallest = val1;
		if (val2 <= val3)
		{
			medium = val2;
			largest = val3;
		}
		else
		{
			medium = val3;
			largest = val2;
		}
	}
	
	if (val2 <= val1 && val2 <= val3)
	{
		smallest = val2;
		if (val1 <= val3)
		{
			medium = val1;
			largest = val3;
		}
		else
		{
			medium = val3;
			largest = val1;
		}
	}

	if (val3 <= val2 && val3 <= val1)
	{
		smallest = val3;
		if (val1 <= val3)
		{
			medium = val1;
			largest = val2;
		}
		else
		{
			medium = val2;
			largest = val1;
		}
	}
	
	cout << smallest << ", " << medium << ", " << largest << "\n";
	keep_window_open();
}
Output:
enter tree integer value 1 2 3
you entered three values: val1: 1, val2: 2, val3: 3
1, 2, 3
Please enter a character to exit

double operations

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 3 Exercise 5
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 3 Exercise 5

#include "std_lib_facilities.h"

int main()
{
	double val1, val2;
	cout << "enter one float value ";
	cin >> val1;
	cout << "enter another float value ";
	cin >> val2;
	cout << val1 << ", " << val2 << "\n";

	if (val1 < val2)
		cout << "val1 " << val1 << " is smaller than val2 " << val2 << "\n";
	else
		cout << "val1 " << val1 << " is larger than val2 " << val2 << "\n";

	cout << "the sum of val1 and val2 is " << val1 + val2 << "\n";
	cout << "the difference of val1 minus val2 is " << val1 - val2 << "\n";
	cout << "the product of val1 and val2 is " << val1 * val2 << "\n";
	cout << "the ratio of val1 to val2 is " << val1 / val2 << "\n";

	keep_window_open();
}
Output:
enter one float value 1 2
enter another float value 1, 2
val1 1 is smaller than val2 2
the sum of val1 and val2 is 3
the difference of val1 minus val2 is -1
the product of val1 and val2 is 2
the ratio of val1 to val2 is 0.5
Please enter a character to exit

integer operations

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 3 Exercise 4
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 3 Exercise 4

#include "std_lib_facilities.h"

int main()
{
	int val1, val2;
	cout << "enter one integer value ";
	cin >> val1;
	cout << "enter another integer value ";
	cin >> val2;
	cout << val1 << ", " << val2 << "\n";

	if (val1 < val2)
		cout << "val1 " << val1 << " is smaller than val2 " << val2  <<"\n";
	else
		cout << "val1 " << val1 << " is larger than val2 " << val2 << "\n";
	
	cout << "the sum of val1 and val2 is " << val1 + val2 << "\n";
	cout << "the difference of val1 minus val2 is " << val1 - val2 << "\n";
	cout << "the product of val1 and val2 is " << val1 * val2 << "\n";
	cout << "the ratio of val1 to val2 is " << val1 / val2 << "\n";

	keep_window_open();
}
Output:
enter one integer value 2
enter another integer value 3
2, 3
val1 2 is smaller than val2 3
the sum of val1 and val2 is 5
the difference of val1 minus val2 is -1
the product of val1 and val2 is 6
the ratio of val1 to val2 is 0
Please enter a character to exit

mile to kilometer converter

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 3 Exercise 2
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 3 Exercise 2

#include "std_lib_facilities.h"

int main()
{
	int mile;
	cout << "enter miles\n";
	cin >> mile;
	cout << mile << "miles are " << mile*1.609 << " km\n";
	keep_window_open();
}
Output:
enter miles
130
130miles are 209.17 km
Please enter a character to exit

unsafe conversion

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 3 Try This Page 81
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 3 Try This  Page 81

#include "std_lib_facilities.h"

int main()
{
	cout << "Enter a value.\n";
	double d = 0;
	while (cin >> d){
		int i = d;
		char c = i;
		int i2 = c;
		cout << "d == " << d
			<< " i == " << i
			<< " i2 == " << i2
			<< " char(" << c << ")\n";
	}
	keep_window_open(".");
}
Output:
Enter a value.
5
d == 5 i == 5 i2 == 5 char()