Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 15th, 2005, 11:11 AM   #11
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
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 !
xavier is offline   Reply With Quote
Old Sep 15th, 2005, 11:12 AM   #12
ViOLATiON
Programmer
 
Join Date: Sep 2005
Posts: 58
Rep Power: 4 ViOLATiON is on a distinguished road
An example would probably help. Thanks for the quick reply
ViOLATiON is offline   Reply With Quote
Old Sep 15th, 2005, 11:15 AM   #13
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Sep 15th, 2005, 11:27 AM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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;
}
Notice that the protection didn't unduly lengthen the code.
__________________
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
DaWei is offline   Reply With Quote
Old Sep 15th, 2005, 11:32 AM   #15
ViOLATiON
Programmer
 
Join Date: Sep 2005
Posts: 58
Rep Power: 4 ViOLATiON is on a distinguished road
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.
ViOLATiON is offline   Reply With Quote
Old Sep 16th, 2005, 6:04 PM   #16
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:28 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC