Joined: 11 Aug 2008 Posts: 114 Location: Ohio bitch
Posted: Tue Feb 22, 2011 12:09 pm Post subject: Finished my calculator!
Code:
// Calculator, by James Fisher
// Include the iostream library
#include <iostream>
//Use the standard namespace
using namespace std;
void main ( )
{
// Declare the variables
float Number_1;
float Number_2;
float Result;
int Which_Calculation;
// Give instructions
cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide." << endl;
cin >> Which_Calculation;
// Get numbers
cout << "Please enter the first number." << endl;
cin >> Number_1;
cout << "Please enter the second number." << endl;
cin >> Number_2;
if (Which_Calculation == 1)
{
// Calculate the result
Result = Number_1 + Number_2;
}
if (Which_Calculation == 2)
{
// Calculate the result
Result = Number_1 - Number_2;
}
if (Which_Calculation == 3)
{
// Calculate the result
Result = Number_1 * Number_2;
}
if (Which_Calculation == 4)
{
// Calculate the result
Result = Number_1 / Number_2;
}
// Print the answer is...
cout << "And the answer is..." << endl;
//Print the result
cout << Result << endl;
system ("PAUSE");
}
Joined: 23 Apr 2006 Posts: 1757 Location: The Netherlands
Posted: Tue Feb 22, 2011 2:18 pm Post subject:
Use a switch brother . I made it so that it won't crash when a number outside of the given range is entered.
Code:
// Calculator, by James Fisher
// Include the iostream library
#include <iostream>
//Use the standard namespace
using namespace std;
void main ( )
{
// Declare the variables
float Number_1;
float Number_2;
float Result;
int Which_Calculation;
// Give instructions
cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide." << endl;
cin >> Which_Calculation;
// Get numbers
cout << "Please enter the first number." << endl;
cin >> Number_1;
cout << "Please enter the second number." << endl;
cin >> Number_2;
switch(Which_Calculation) {
case 1:
Result = Number_1 + Number_2;
break;
case 2:
Result = Number_1 - Number_2;
break;
case 3:
Result = Number_1 * Number_2;
break;
case 4:
Result = Number_1 / Number_2;
break;
default:
Result = 0;
break;
}
// Print the answer is...
cout << "And the answer is..." << endl;
//Print the result
cout << Result << endl;
system ("PAUSE");
}
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum