// Name: myCalc.cpp
// Author: Joel Zimmerman
// Date: May 03, 2005
// Description: Calculator that does Addition, Subtraction, Multiplication
// Division, Modulus and the Cube of an integer.
#include <iostream>
int main()
{
int number1;
int number2;
int choice;
char indicator;
cout << "Welcome to my Calculator it will perform the following functions:" << endl
<< "1) Addition" << endl
<< "2) Subtraction" << endl
<< "3) Multiplication" << endl
<< "4) Division" << endl
<< "5) Modulus" << endl
<< "6) Cube" << endl;
do
{
cout << "Choose Operator: ";
cin >> choice;
cout << "Enter First Digit: ";
cin >> number1;
cout << "Enter Second Digit: ";
cin >> number2;
switch(choice)
{
case 1: cout << endl << "The answer is: " << number1 + number2 << endl;
break;
case 2: cout << endl << "The answer is: " << number1 - number2 << endl;
break;
case 3: cout << endl << "The answer is: " << number1 * number2 << endl;
break;
case 4: cout << endl << "The answer is: " << number1 / number2 << endl;
break;
case 5: cout << endl << "The answer is: " << number1 % number2 << endl;
break;
default: cout << endl <<"You chose invalid selection" << endl;
}
cout << endl << "Do you want to enter another (y or n)? ";
cin >> indicator;
} while((indicator == 'Y' || indicator == 'y'));
return 0;
}
I am not sure how to go about the "Cubing"
I don't want to cube two numbers, only one.
Ideas/suggestions about ways i could do this would be greatly appreciated.
Also, how do i check if my Number1 and Number2 are actually numbers?
Quote from: BaDDBLooD on May 04, 2005, 04:08 PM
I am not sure how to go about the "Cubing"
I don't want to cube two numbers, only one.
Ideas/suggestions about ways i could do this would be greatly appreciated.
Also, how do i check if my Number1 and Number2 are actually numbers?
I don't know about actual C runtime functions to check if number1 and number2 are actually numbers. When I compiled and ran in MSVC++ 2003, the program just exited if I input a non-number.
I wrote a function called getnum:
// gets a number from the console.
// maxDigits: [in] maximum number of digits to accept
// *pNum: [out]value from console.
// return value: whether or not the operation completed successfully.
bool getnum(unsigned short int maxDigits, long int *pNum) {
unsigned int i;
char curVal;
long int temp = 0;
bool okay = false;
for (i = 0; i < maxDigits; i++)
{
curVal = getchar();
if (curVal >= '0' && curVal <= '9') {
// valid character
temp *= 10;
temp += (curVal - '0');
okay = true;
} else {
break;
}
}
// reads the end-of-line character
fseek(stdin, 1, SEEK_CUR);
*pNum = temp;
return okay;
}
getnum takes two parameters. The first is the maximum number of digits it should accept. The second is a pointer to the location where it should store the result of the input. The return value indicates whether any number was input; if it was only invalid text, the function returns false.
To cube a value, simply multiply it by itself two more times:
case 6: cout << endl << "The answer is: " << number1 * number1 * number1 << endl;
break;
Note that you don't need a second number input to do the cube function.
Aside from that, I changed around some of your other functions. First, I redeclared your variables:
long int number1;
long int number2;
long int choice;
char indicator;
bool gotnum1 = false;
bool gotnum2 = false;
Note that I included two booleans (you'll see why in a minute), and changed your other three int choices to long ints. That was to establish type compatibility with the getnum function -- getnum can return values up to 32 bits in length.
Then I changed your initial prompt:
choice = 0;
cout << "Choose Operator: ";
bool okay = false;
do {
okay = getnum(1, &choice);
} while (okay && (choice >= 1) && (choice <= 6));
As long as someone is entering a choice outside of 1 to 6, it will keep prompting them. Note that only the first number is considered.
I also changed your input prompts to use getnum instead of cin. It checks for the number to be okay, and if it was not input, prompts to insert the number:
do
{
cout << "Enter First Digit: ";
gotnum1 = getnum(9, &number1);
} while (gotnum1 == false);
if (choice >= 1 && choice <= 5)
{
do
{
cout << "Enter Second Digit: ";
gotnum2 = getnum(9, &number2);
} while (gotnum2 == false);
}
All told, this is what I used in VC++ 2003:
// Calc.cpp : Defines the entry point for the console application.
//
#pragma once
// Name: myCalc.cpp
// Author: Joel Zimmerman
// Date: May 03, 2005
// Description: Calculator that does Addition, Subtraction, Multiplication
// Division, Modulus and the Cube of an integer.
//#include "stdafx.h"
#include <iostream>
//protos
bool getnum(unsigned short int, long int*);
int main();
using namespace std;
int main()
{
long int number1;
long int number2;
long int choice;
char indicator;
bool gotnum1 = false;
bool gotnum2 = false;
cout << "Welcome to my Calculator it will perform the following functions:" << endl
<< "1) Addition" << endl
<< "2) Subtraction" << endl
<< "3) Multiplication" << endl
<< "4) Division" << endl
<< "5) Modulus" << endl
<< "6) Cube" << endl;
do
{
choice = 0;
cout << "Choose Operator: ";
bool okay = false;
do {
bool okay = getnum(1, &choice);
} while (okay && (choice >= 1) && (choice <= 6));
do
{
cout << "Enter First Digit: ";
gotnum1 = getnum(9, &number1);
} while (gotnum1 == false);
if (choice >= 1 && choice <= 5)
{
do
{
cout << "Enter Second Digit: ";
gotnum2 = getnum(9, &number2);
} while (gotnum2 == false);
}
switch(choice)
{
case 1: cout << endl << "The answer is: " << number1 + number2 << endl;
break;
case 2: cout << endl << "The answer is: " << number1 - number2 << endl;
break;
case 3: cout << endl << "The answer is: " << number1 * number2 << endl;
break;
case 4: cout << endl << "The answer is: " << number1 / number2 << endl;
break;
case 5: cout << endl << "The answer is: " << number1 % number2 << endl;
break;
case 6: cout << endl << "The answer is: " << number1 * number1 * number1 << endl;
break;
default: cout << endl <<"You chose invalid selection" << endl;
}
cout << endl << "Do you want to enter another (y or n)? ";
cin >> indicator;
// reads the end-of-line character
fseek(stdin, 1, SEEK_CUR);
} while((indicator == 'Y' || indicator == 'y'));
return 0;
}
// gets a number from the console.
// maxDigits: [in] maximum number of digits to accept
// *pNum: [out]value from console.
// return value: whether or not the operation completed successfully.
bool getnum(unsigned short int maxDigits, long int *pNum) {
unsigned int i;
char curVal;
long int temp = 0;
bool okay = false;
for (i = 0; i < maxDigits; i++)
{
curVal = getchar();
if (curVal >= '0' && curVal <= '9') {
// valid character
temp *= 10;
temp += (curVal - '0');
okay = true;
} else {
break;
}
}
// reads the end-of-line character
fseek(stdin, 1, SEEK_CUR);
*pNum = temp;
return okay;
}
Hope it helps :)
Every time I see 'cout', I want to blow my brains out.
Quote from: DarkMinion on June 11, 2005, 05:50 PM
Every time I see 'cout', I want to blow my brains out.
Why's that then? if you don't like the object orientated features of c++/stl stick to C. Pretty simple.
Quote from: DarkMinion on June 11, 2005, 05:50 PM
Every time I see 'cout', I want to blow my brains out.
Me too :)
I can manage just fine without Captain Know-It-All telling me what I need to be doing.
Yeah, the cout's are ugly. Also, you can reduce that switch by a shitload if you just declare one more variable. The output is all the same minus the value.