![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Professional Programmer
|
Can you use like in JAVA : try - catch statements ? to check if the user has given the right type of input ?
If i'd want the user to enter an integer i'd do something like : try{
Integer.parseInt(variable);
}catch(Exception e){ //do stuff
}If u can use that in c++ , a clear example would be apreciated.
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#12 |
|
Programmer
Join Date: Sep 2005
Posts: 58
Rep Power: 4
![]() |
An example would probably help. Thanks for the quick reply
![]() |
|
|
|
|
|
#13 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The use of error detecting mechanisms like try/catch is contraindicated for simple errors due to functions working as they advertise themselves to do, or failing under explicitly defined conditions. I won't go into that (see Bruce Eckel's books), but I will dump some code on you shortly.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#14 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
As mentioned previously, I would not implement a calculator this way. This does, however, afford protection. On another point, when outputting, you will want to output an "endl" instead of a "\n" at least at some point. It causes the output to be flushed. One doesn't always see a failure to flush, because other conditions cause it to happen, but be prepared. You can always flush overtly, but the endl does it for you.
#include <iostream>
#include <string>
using namespace std;
int ohHell (string badNews)
{
cerr << badNews << endl;
return 1;
}
int main (int argc, char *argv [])
{
int opchoice;
int numberone;
int numbertwo;
int answer;
bool failure = false;
// Choose your operation
cout << "Choose your operation\n\n1 - Multiplication\n2 - Subtraction\n3 - Addition\n4 - Division\n\n"<<endl;
cin >> opchoice;
if (!cin.good ()) return ohHell ("Input failure on operator choice");
cout << "\nPlease input the first number\n\n";
cin >> numberone;
if (!cin.good ()) return ohHell ("Input failure on first operand");
cin.sync ();
cout << "\n\nPlease input the second number\n\n";
cin >> numbertwo;
if (!cin.good ()) return ohHell ("Input failure on second operand");
switch( opchoice)
{
case 1:
// Multiplication
answer = numberone * numbertwo;
break;
case 2:
// Subtraction
answer = numberone - numbertwo;
break;
// Addition
case 3:
answer = numberone + numbertwo;
break;
case 4:
//division
answer = numberone / numbertwo;
default:
failure = true;
}
if (failure) cout << "No operation was performed";
else cout << "\nThe final answer is " << answer << endl;
cin.get();
return 0;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#15 |
|
Programmer
Join Date: Sep 2005
Posts: 58
Rep Power: 4
![]() |
I just got into reading about endl;
Thanks for your help, I'm reading about the cerr part right now, to see exactly what it is, etc. ![]() |
|
|
|
|
|
#16 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
dawei made some good points about error checking and good habits here. if you look in the "finished projects" or whatever forum you will find a post on four-function calculator code by myself back in the day with code for what you are trying to do in C, C++, and Java with a great reply by a member called "eggbert" showing source code implementing a stack-based calculator. this would be a good thing to peruse. it may take you an hour to write a program, but it can take 3 to debug it (feed it crappy input and find out how to prevent that, etc.) i recently did a homework assignment that counts out the denominations of pennies, nickels, etc. (fucking change) that would be counted out for a number in the range of 0-99 (b/c these are the only values that make sense although it works for as large a data type as you throw at it). if the user tried to enter a negative value they got a message saying "i don't think so buddy..." and the program terminated. a sense of humour is typically common among programmers.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|