Aureole Posted February 19, 2008 Share Posted February 19, 2008 I figured I'd learn C++, I set myself a goal, to create an extremely simple mini-calculator. All is well so far, but I've ran into a few problems. #1: When you choose whether to add, subtract, multiply or divide... if you enter a number (e.g. 6) then it does what I want it to do (asks you to enter a number between 1-4), though if you enter a letter (e.g. "h") then the do() part runs over and over again extremely fast and fills the entire console and it won't stop. #2: As far as my searches have turned up, there is no way to check if input is numeric... I need something like PHPs is_numeric(). #3: Within main() after answer() I want the program to "restart", so that the user can choose another option and go again, but I can't figure out how to do that. #4: I'd like to make it so a user can enter "exit" at any time to exit the program. If it makes any difference I'm using Microsoft Visual C++ 2008 Express Edition. I realize that this probably isn't the best way of doing things and it's probably not very efficient but I just started learning, take it easy. I'm just trying to learn the basics. #include <iostream> #include <string> #include "stdafx.h" void start() { using namespace std; cout << "Welcome to my simple Calculator!" << endl; } int choice() { using namespace std; int uChoice; do { cout << endl << "What do you want to do?" << endl; cout << "1) Add" << endl; cout << "2) Subtract" << endl; cout << "3) Multiply" << endl; cout << "4) Divide" << endl; cout << endl << "Waiting for input... (enter a number): "; cin >> uChoice; cout << endl; } while( uChoice != 1 && uChoice != 2 && uChoice != 3 && uChoice != 4 ); switch ( uChoice ) { case 1: cout << endl << "You chose addition." << endl; break; case 2: cout << endl << "You chose subtraction." << endl; break; case 3: cout << endl << "You chose multiplication." << endl; break; case 4: cout << endl << "You chose division." << endl; break; } return uChoice; } int input( bool i = false ) { using namespace std; string text; text = ( i == true ) ? "Enter another number: " : "Enter a number: "; cout << endl << text; int number; cin >> number; return number; } int work( int one, int two, int todo ) { using namespace std; int answer; switch ( todo ) { case 1: answer = one + two; break; case 2: answer = one - two; break; case 3: answer = one * two; break; case 4: answer = one / two; break; } return answer; } void answer( int theanswer ) { using namespace std; cout << endl << "The answer is " << theanswer << "." << endl; cout << endl << "Hit Return to exit."; cin.clear(); cin.ignore( 255, '\n' ); cin.get(); } int main() { using namespace std; start(); int todo = choice(); int one = input(); int two = input( true ); int theanswer = work( one, two, todo ); answer( theanswer ); return 0; } Quote Link to comment https://forums.phpfreaks.com/topic/91845-need-help-with-my-simple-c-mini-calculator/ Share on other sites More sharing options...
fert Posted February 22, 2008 Share Posted February 22, 2008 #2: As far as my searches have turned up, there is no way to check if input is numeric... I need something like PHPs is_numeric(). the function atoi in stdlib.h returns 0 if the input is not a number. #3: Within main() after answer() I want the program to "restart", so that the user can choose another option and go again, but I can't figure out how to do that. create an infinite loop like this for(; { //do something } #4: I'd like to make it so a user can enter "exit" at any time to exit the program. doing that would be kinda tricky, it's better to offer an option to exit and then use the exit() function in stdlib.h Quote Link to comment https://forums.phpfreaks.com/topic/91845-need-help-with-my-simple-c-mini-calculator/#findComment-473589 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.