need help with my basic c++ calculator
im brand new to c++, so i was wondering if someone could help me with my calculator program.
it starts out by printing this on the screen:
1. Addition
2. Subtraction
3. Multiplication
4. Division
Choose one:
The user chooses one of the operations by entering the number.
It runs fine and all, except for one thing. When the user inputs a character or a string of characters, the program starts repeating itself at a very rapid rate.
instead of getting all screwed up, i am trying to figure out how to make the program print an error message like: "Thats a letter, not a number" or something.
the problem is, my variable for the user input is a float variable. is there some way for a variable or an array to be able to handle 2 different types, such as char and int.
ok i hope that came across in an understandable way
Re: need help with my basic c++ calculator
Quote:
Originally posted by iCouch_Potato
the problem is, my variable for the user input is a float variable. is there some way for a variable or an array to be able to handle 2 different types, such as char and int.
I dont think so
Re: need help with my basic c++ calculator
Quote:
Originally posted by iCouch_Potato
It runs fine and all, except for one thing. When the user inputs a character or a string of characters, the program starts repeating itself at a very rapid rate.
Stop using scanf(). It has terrible error recovery.
Re: need help with my basic c++ calculator
>im brand new to c++, so i was wondering if someone could help me with my calculator program.
Ahh, we always have to start somewhere =) Common sense and not throwing your computer out the window solves everything.
>it starts out by printing this on the screen:
1. Addition
2. Subtraction
3. Multiplication
4. Division
Choose one:
Do you have any error checking in your source documentation for the input?
>The user chooses one of the operations by entering the number.
After you enter in the number, where does it store the data, what type is it, and how does it deal with it?
>It runs fine and all, except for one thing. When the user inputs a character or a string of characters, the program starts repeating itself at a very rapid rate.
You need to setup code that will check if the number is a number :) If it's not then don't perform the calculations.
>instead of getting all screwed up, i am trying to figure out how to make the program print an error message like: "Thats a letter, not a number" or something.
Given that 'num' is the variable where the integer input value will be stored, your code might look like this:
Code:
do
{
if (num >=1 && num <= 4)
{
//Process data here.
}
else
std::cout << "\n\nError:Enter in a valid type.";
}
}while(continueCalc == true);
>the problem is, my variable for the user input is a float variable. is there some way for a variable or an array to be able to handle 2 different types, such as char and int.
ok i hope that came across in an understandable way
No, but thenagain, you could make an overloaded function to receive certain types and act on whichever data-type is passed into the function.
----------------------------------------------------------------------------
Without the source, this is all I could come up with.