Author | Message | Time |
---|---|---|
BaDDBLooD | [code] // 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; } [/code] 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? | May 4, 2005, 9:08 PM |
Myndfyr | [quote author=BaDDBLooD link=topic=11488.msg111061#msg111061 date=1115240935] 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] 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: [code] // 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; } [/code] 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: [code] case 6: cout << endl << "The answer is: " << number1 * number1 * number1 << endl; break; [/code] 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: [code] long int number1; long int number2; long int choice; char indicator; bool gotnum1 = false; bool gotnum2 = false; [/code] 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: [code] choice = 0; cout << "Choose Operator: "; bool okay = false; do { okay = getnum(1, &choice); } while (okay && (choice >= 1) && (choice <= 6)); [/code] 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: [code] 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); } [/code] All told, this is what I used in VC++ 2003: [code] // 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; } [/code] Hope it helps :) | May 5, 2005, 12:05 AM |
DarkMinion | Every time I see 'cout', I want to blow my brains out. | June 11, 2005, 10:50 PM |
OnlyMeat | [quote author=DarkMinion link=topic=11488.msg115460#msg115460 date=1118530233] Every time I see 'cout', I want to blow my brains out. [/quote] Why's that then? if you don't like the object orientated features of c++/stl stick to C. Pretty simple. | June 11, 2005, 11:02 PM |
Arta | [quote author=DarkMinion link=topic=11488.msg115460#msg115460 date=1118530233] Every time I see 'cout', I want to blow my brains out. [/quote] Me too :) | June 11, 2005, 11:16 PM |
DarkMinion | I can manage just fine without Captain Know-It-All telling me what I need to be doing. | June 12, 2005, 12:14 AM |
R.a.B.B.i.T | 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. | June 12, 2005, 12:21 AM |