Subscribe to Invariance_Under_Translation        RSS Feed
-----

Here we go..

Icon Leave Comment
I will introduce myself by stating that i am a novice programmer. So if you are looking for advanced topics this is not where you want to be :)
Critics for posted code are more than welcome though, I believe it is impossible to become a better programmer without recognizing your mistakes.

The intent of this blog is to give examples of novice topics using C++ as the language. Not only do i hope this will assist other learning coders,
but it gives me an excuse to practice writing more code.

I'll start out by posting a little piece here that will introduce many topics you will see when you first start coding; to include assigning user input to
a variable, using control structures such as do-while loops and 'if' statments and passing parameters to functions. Any question are welcome, and there
are plenty more to come. Enjoy!

#include<iostream>
using namespace std;

// Below we are declaring our functions prior to our main. 
// This simply allows for our functions to be positioned below 'main'. 

int add (int x, int y);
int sub (int x, int y);
int mult(int x, int y);
int divi(int x, int y);

//The start of our 'main' function
int main () {

int x;
int y;
int choice;  //Declaring variables that will allow the user to input the
int sum;	 //numbers to manipulate, along with their chosen calculation.

cout<<"Welcome to the SCP (Simple Calculator Program).";
cout<<end1;

do {	   //Starting a do-while loop that will allow the user repeated use of the program until
		   //they are done. 

cout<<"Please enter a 1 to Add, a 2 to Subtract, a 3 to Multiply, or a 4 to divide. Enter 0 to quit";
cin>>choice;   //Inputing the desired calculation

 if ( choice == 1 ) {

cout<<"Please enter the numbers that you would like to add.";			  //You could use a 'switch' statement 
cin>>x;																							//that would be more efficient, but we
cin>>y;																						   //will demonstrate here how to use the
add(x, y);																						//control structure 'if' to give the user
cout<<"Your total is: ", sum;															 //control of which calculation they would 
																									  //like to perform. 
}

 if ( choice == 2 ) {

cout<<"Please enter the numbers that you would like to subtract.";
cin>>x;
cin>>y;								   //'cin' takes the users input, and then 'sub()' passes that input
sub(x, y);								//to our seperate function that we declared in the begining of the program. 
cout<<"Your total is: ", sum;	//Our function returns the calculation, and 'sum' outputs the end result. 
 
 }

 if ( choice == 3 ) {

cout<<"Please enter the numbers that you would like to multiply.";
cin>>x;
cin>>y;
mult(x, y);
cout<<"Your total is: ", sum;
 
 }

 if ( choice == 4 ) {

cout<<"Please enter the numbers that you would like to divide.";
cin>>x;
cin>>y;
divi(x, y);
cout<<"Your total is: ", sum;
 
 }

 if ( choice == 0 ) {

cout<<"Thank you for using SCP, Goodbye.";
cin.get();
 
 }

else {
cout<<"You did not select a valid option.";
  }

 }while (choice != 0);

}



int add(int x, int y) {							//Here are our functions that perform the actual
int sum;											 //calculations. The user input is passed using the 
sum = x + y;									 //function call in the main function. These sub functions recieve 
return (sum);									//the input using '(int x, int y)' and perform the designated
}													 //calculation. They assign the 

int sub(int x, int y) {
int sum;
sum = x - y;
return (sum);
}

int multi(int x, int y) {
int sum;
sum = x * y;
return (sum);
}

int divi(int x, int y)  {
int sum;
sum = x / y;
return (sum);
}

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

July 2018

S M T W T F S
1234567
891011121314
1516 17 18192021
22232425262728
293031    

Tags

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)