PDA

View Full Version : Calculator Example C++



TheEliteOne
04-04-2010, 04:02 AM
Hello, here is a calculator example that I typed up, edit and remove things to try and learn more



/*

TheEliteOne's C++ Calculator Example

Every thing that I could explain is explained

Try changing some things, removing things, ect. to learn more.

*/

#include <iostream> //Includes the iostream file from your library into your program when you compile it

#include <cmath> //Includes the cmath file from your library into your program when you compile it

using namespace std;

char type; //Allows us to use the variable type

int number1, number2, number3; //Allows us to use the variables type, number1, number2, and number3 in are program

int main() //Says that we are entering the main function of are program

{ //This is the start of are main function

First: //This will be used for goto command later

system("cls"); //This clears the screen, so if we goto first the screen will clear

cout<<"TheEliteOne's Calculator Example\n\n"; //Displays the words TheEliteOne's Calculator Example, the \n is like pressing enter

cout<<"Enter the number for the operation you want to peform:\n\n"; //Displays text

cout<<"1) Addition\n2) Subtraction\n3) Multiplication\n4) Divition\n\n";

cin>>type; //This makes it so what ever you typed in is stored in the vareable type

if(type=='1') //If the user typed 1 they will go through the following until the }

{ //Starts what will happen when you type in 1

Add: //This will be used for a goto command

system("cls"); //Clears the screen

cout<<"Enter the first number:\n\n"; //Displays text

cin>>number1; //This makes it so what ever you typed in is stored in the vareable number1

cout<<"\nEnter the second number:\n\n"; //Displays text

cin>>number2; //This makes it so what ever you typed in is stored in the vareable number2

number3=number1+number2; //Adds number1 and number2 and stores the result into number3

system("cls"); //Clears the screen

cout<<"Result:\n\n" <<number3<< "\n\n"; //Displays the result

cout<<"Try again? (y/n)\n\n"; //Displays text

cin>>type; //This makes it so what ever you typed in is stored into the vareable type

if(type=='Y') //If the user typed Y they will continue on, C++ is case sinsitive, so I added this check to make sure it works with a Y

{ //Starts what will happen when you type in Y

goto Add; //If the user typed in Y they would go back to Add

} //Ends what would happen if the user typed in Y, so if they didn't type Y they would go to here

if(type=='y') //Here is the same thing for y

{ //Starts what would happen when you type in y

goto Add; //Goto add

} //Ends

if(type=='N') //Now here is if the user typed in N

{ //Start

goto First; //So if the user typed in N we would goto First

} //Ends

if(type=='n') //One for n

{ //Start

goto First; //Goto First

} //Ends
} //Ends of what heppends if you press 1 in the main menu

if(type=='2') //If the user typed in 2 in the main menu they would go through the following untell the }

{ //Start

Sub: //This will be used for a goto command

system("cls"); //Clears the screen

cout<<"Enter the first number:\n\n"; //Displays text

cin>>number1; //This makes it so what ever you typed in is stored in the vareable number1

cout<<"\nEnter the second number:\n\n"; //Displays text

cin>>number2; //This makes it so what ever you typed in is stored in the vareable number2

number3=number1-number2; //Subtracts number1 and number2 and stores the result into number3

system("cls"); //Clears the screen

cout<<"Result:\n\n" <<number3<< "\n\n"; //Displays the result

cout<<"Try again? (y/n)\n\n"; //Displays text

cin>>type; //This makes it so what ever you typed in is stored into the vareable type

if(type=='Y') //If the user typed Y they will continue on, C++ is case sinsitive, so I added this check to make sure it works with a Y

{ //Starts what will happen when you type in Y

goto Sub; //If the user typed in Y they would go back to Sub

} //Ends what would happen if the user typed in Y, so if they didn't type Y they would go to here

if(type=='y') //Here is the same thing for y

{ //Starts what would happen when you type in y

goto Sub; //Goto Sub

} //Ends

if(type=='N') //Now here is if the user typed in N

{ //Start

goto First; //So if the user typed in N we would goto First

} //Ends

if(type=='n') //One for n

{ //Start

goto First; //Goto First

} //Ends
} //Ends of what heppends if you press 2 in the main menu

if(type=='3') //If the user typed 3 in the main menu they will go through the following until the }

{ //Start
Mult: //This will be used for a goto command

system("cls"); //Clears the screen

cout<<"Enter the first number:\n\n"; //Displays text

cin>>number1; //This makes it so what ever you typed in is stored in the vareable number1

cout<<"\nEnter the second number:\n\n"; //Displays text

cin>>number2; //This makes it so what ever you typed in is stored in the vareable number2

number3=number1*number2; //Multiplies number1 and number2 and stores the result into number3

system("cls"); //Clears the screen

cout<<"Result:\n\n" <<number3<< "\n\n"; //Displays the result

cout<<"Try again? (y/n)\n\n"; //Displays text

cin>>type; //This makes it so what ever you typed in is stored into the vareable type

if(type=='Y') //If the user typed Y they will continue on, C++ is case sinsitive, so I added this check to make sure it works with a Y

{ //Starts what will happen when you type in Y

goto Mult; //If the user typed in Y they would go back to Mult

} //Ends what would happen if the user typed in Y, so if they didn't type Y they would go to here

if(type=='y') //Here is the same thing for y

{ //Starts what would happen when you type in y

goto Mult; //Goto Mult

} //Ends

if(type=='N') //Now here is if the user typed in N

{ //Start

goto First; //So if the user typed in N we would goto First

} //Ends

if(type=='n') //One for n

{ //Start

goto First; //Goto First

} //Ends
} //Ends what happens if you press 3 in the main menu

if(type=='4') //If the user typed 4 in the main menu they will go through the following until the }

{ //Start
Div: //This will be used for a goto command

system("cls"); //Clears the screen

cout<<"Enter the first number:\n\n"; //Displays text

cin>>number1; //This makes it so what ever you typed in is stored in the vareable number1

cout<<"\nEnter the second number:\n\n"; //Displays text

cin>>number2; //This makes it so what ever you typed in is stored in the vareable number2

number3=number1/number2; //Divides number1 and number2 and stores the result into number3

system("cls"); //Clears the screen

cout<<"Result:\n\n" <<number3<< "\n\n"; //Displays the result

cout<<"Try again? (y/n)\n\n"; //Displays text

cin>>type; //This makes it so what ever you typed in is stored into the vareable type

if(type=='Y') //If the user typed Y they will continue on, C++ is case sinsitive, so I added this check to make sure it works with a Y

{ //Starts what will happen when you type in Y

goto Div; //If the user typed in Y they would go back to Div

} //Ends what would happen if the user typed in Y, so if they didn't type Y they would go to here

if(type=='y') //Here is the same thing for y

{ //Starts what would happen when you type in y

goto Div; //Goto Div

} //Ends

if(type=='N') //Now here is if the user typed in N

{ //Start

goto First; //So if the user typed in N we would goto First

} //Ends

if(type=='n') //One for n

{ //Start

goto First; //Goto First

} //Ends
} //Ends what happens if you press 3 in the main menu

else //So if the user types in some thing in the main menu that we don't wont we will go through this

{ //Start

goto First; //So if you type in a number that we don't want nothing happens
} //Ends
} //Ends main, and are program



//Try changing some things, removing things, ect. to learn more.


Here are two links to the program that this source code would make:

Download Calculator Example.exe from Sendspace.com - send big files the easy way (http://www.sendspace.com/file/53padc)

MEGAUPLOAD - The leading online storage and file delivery service (http://www.megaupload.com/?d=88Z8XYEU)

SonniE
04-18-2010, 09:10 PM
Works i assume try not to you goto labels when you can, and you can shorten the code by using and/or operators.