first C++ program

this is my first program,simple calculator but has helped me with a fair bit of understanding. Is there anyway to optimize it to make it smaller, the release size is 12kb compiled, seems kinda large for just a bunch of text play.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
using namespace std;
// set varaible for menu choice
int unsigned short choice;
// declare functions
void Addition();
void Subtract();
void Multi();
void Divi();
void About();
// main function, display menu
// and get selection
int main()
{
	do // loop menu while choice isnt quit [6]
	{
		system("cls");
		cout << "_____________________ " << endl;
		cout << " ------ MENU ------- |" << endl;
		cout << "Addition ------- [1]||" << endl; 
		cout << "Subtraction ---- [2]||" << endl;
		cout << "Multiplication - [3]||" << endl;
		cout << "Division ------- [4]||" << endl;
		cout << "About ---------- [5]||" << endl;
		cout << "Quit ----------- [6]||" << endl;
		cout << "-------------------- |" << endl;
		cout << "||||||||||||||||||||||" << endl;
		cout << "~~~~~~~~~~~~~~~~~~~~~~" << endl;
		cout << "Enter Selection: ";
		cin >> choice;
		switch(choice) // switch to what function to preform
		{
		case 1:
			Addition();
			break;
		case 2:
			Subtract();
			break;
		case 3:
			Multi();
			break;
		case 4:
			Divi();
			break;
		case 5:
			About();
			break;
		case 6:
			break; // break from loop, quit app
		}
	}
	while (choice != 6);
	return 0;
}

void Addition()
{
	system("cls");
			double firstAdd,
				   secondAdd,
				   totalAdd;

			cout << "Enter first digit: ";
			cin >> firstAdd;
			cout << "Enter second digit: ";
			cin >> secondAdd;
			totalAdd = firstAdd + secondAdd;
			cout << firstAdd << " + " << secondAdd << " = " 
				 << totalAdd << endl;
			system("pause"); // wait for user
}

void Subtract()
{
	system("cls");
			double firstSub,
				   secondSub,
				   totalSub;

			cout << "Enter first digit: ";
			cin >> firstSub;
			cout << "Enter second digit: ";
			cin >> secondSub;
			totalSub = firstSub - secondSub;
			cout << firstSub << " - " << secondSub << " = "
				 << totalSub << endl;
			system("pause");
}

void Multi()
{
	system("cls");
			float firstMulti,
				  secondMulti,
				  totalMulti;

			cout << "Enter first digit: ";
			cin >> firstMulti;
			cout << "Enter second digit: ";
			cin >> secondMulti;
			totalMulti = firstMulti * secondMulti;
			cout << firstMulti << " * " << secondMulti << " = "
				 << totalMulti << endl;
			system("pause");
}

void Divi()
{
	system("cls");
			int firstDivi,
				secondDivi,
				remainder,
				totalDivi;

			cout << "Enter first digit: ";
			cin >> firstDivi;
			cout << "Enter second digit: ";
			cin >> secondDivi;
			totalDivi = firstDivi / secondDivi;
			remainder = firstDivi % secondDivi;
			cout << firstDivi << " / " << secondDivi << " = "
				 << totalDivi << "." << remainder << endl;
			system("pause");
}

void About()
{
	system("cls");
	cout << "Simple console calculator" << endl;
	cout << "to help me get familure with loops" << endl;
	cout << "By, eversi0n." << endl;
	cout << "Feb, 2008" << endl;
	system("pause");
}
yes there are some apparent ones,
1. if you always ask for two numbers, may it be + or / , so why not ask it only once. like
cout << "Enter first digit: ";
cin >> firstNum;
cout << "Enter second digit: ";
cin >> secondNum;

and then pass them to your code. and let it return the value and print it once only.
like
1
2
  
double add(double firstAdd, secondAdd);

so in your main
something like this can compact stuffs:
 
cout << "result is" << add(firstNum, secondNum);
this is my calculator:

it does addition, subtractio, division and multiplication(*, x, and X) and if you try to divide by zero it'll come up with a error message.



#include <iostream.h>
using namespace std;

int main(void)
{
system ("TITLE calculator");
system ("COLOR 5E");
char cChar;
double dfirstnumber;
double dsecondnumber;
char cDoagain;

do
{
system ("CLS");

cout << "Please enter the first number that you want to use " << endl;
cin >> dfirstnumber;

cout << "Please enter the operation that you would like to perform" << "(+,-,* or/)" << endl;
cin >> cChar;

cout << "Please enter the second number that you want to use" << endl;
cin >> dsecondnumber;
switch (cChar)
{
case '+':
cout << "The answer is " << dfirstnumber << " + " << dsecondnumber << " = " << (dfirstnumber + dsecondnumber) << endl;
break;

case '-':
cout << "The answer is " << dfirstnumber << " - " << dsecondnumber << " = " << (dfirstnumber - dsecondnumber) << endl;
break;

case '*':
cout << "The answer is " << dfirstnumber << " * " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;
break;

case 'x':
cout << "The answer is " << dfirstnumber << " x " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;
break;

case 'X':
cout << "The answer is " << dfirstnumber << " X " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;
break;

case '/':
if(dsecondnumber == 0){
cout << "This is an invalid operation" << endl;
}else{
cout << "The answer is " << dfirstnumber << " / " << dsecondnumber << " = " << (dfirstnumber / dsecondnumber) << endl;
break;
}
default:
cout << "That is an invalid operation " << endl;
break;
}
cout << "Would you like to start again? (y or n)" << endl;
cin >> cDoagain;
}while (cDoagain == 'y' || cDoagain == 'Y');

system ("PAUSE");
return (0);

}

Last edited on
Topic archived. No new replies allowed.