Since you guys have been so helpful through the years with macros and scripts, I figured it was the best place to come for a little help.
I'm finally starting my programming classes, and its the best. However, I'm having a little bit of trouble rationalizing my if commands, and was wondering what you guys might be able to offer.
Background:
All thats required is a simple program to do adding/subtracting/multiplying/dividing. I've gotten the program down no problem. My issue that I've come to, is I'm an overachiever, and my book fails to mention how to fix my issue in a way myself or my friend have been able to put into common terms.
Issue:
I'm trying to demonstrate an if statement that checks for the number zero in number2, since its impossible to divide by zero currently, and output a message while returning to the beginning of the program again. Another issue, is that while returning to the main after the attempted if statement, I haven't found the proper terms to simply return to the beginning without a complete interrupt of the program if the second number is or isn't zero.
Code:
PS- All commands are documented for simple grading reasons. Not obligated to post documented reasons for commands; I can always review and learn from the things I overlooked.
Look forward to hearin back from some of you guys
-Kyle
I'm finally starting my programming classes, and its the best. However, I'm having a little bit of trouble rationalizing my if commands, and was wondering what you guys might be able to offer.
Background:
All thats required is a simple program to do adding/subtracting/multiplying/dividing. I've gotten the program down no problem. My issue that I've come to, is I'm an overachiever, and my book fails to mention how to fix my issue in a way myself or my friend have been able to put into common terms.
Issue:
I'm trying to demonstrate an if statement that checks for the number zero in number2, since its impossible to divide by zero currently, and output a message while returning to the beginning of the program again. Another issue, is that while returning to the main after the attempted if statement, I haven't found the proper terms to simply return to the beginning without a complete interrupt of the program if the second number is or isn't zero.
Code:
PS- All commands are documented for simple grading reasons. Not obligated to post documented reasons for commands; I can always review and learn from the things I overlooked.
Code:
// Purpose: Create a program to add, subtract, divide and multiply input numbers.
//Extra Credit: Identify the zeros of the denumerator, and write a solution to restart with proper inputted numbers.
#include <iostream> // Allows the program to carry out input and output commands
using namespace std; // Allows program to use names from std library
int main()
{
// Variable Declaration
int number1; // First number to add
int number2; // Second number to add
cout << "Enter first integer: "; // Prompts user for number1
cin >> number1; // Grabs the first integer inputted
cout << "Enter second integer: "; // Prompts user for number2
cin >> number2; // Grabs the second integer inputted
int sum; // Sum of number1 and number2
sum = number1 + number2; // Adds numbers together
cout << "Sum is " << sum << endl; // Displays sum and ends line
int difference; // Difference of number 1 and number2
difference = number1 - number2; // Subtracts number1 by number2
cout << "Difference is " << difference << endl; // Displays difference and ends line
int product; // Product of number1 and number2
product = number1 * number2; // Multiplies number1 and number2 together
cout << "Product is " << product << endl; // Displays product and ends line
int quotient; // Quotient of number1 and number2
quotient = number1 / number2; // Divides number1 by number2
cout << "Quotient is " << quotient << endl; // Displays quotient and ends line
{
if (number2 == 0)
cout << "Cannot be divided by zero, please try again." << quotient << endl; // Calls out the impossibility of zero division.
return main ();
}
return main(); // Returns to beginning of script
} // Ends main function
Look forward to hearin back from some of you guys
-Kyle
Last edited: