Newbie C++ programmer again...

Robert Ambrose rna@testpt.com
Fri, 18 Aug 2000 14:01:12 -0700 (MST)


I think a minor change may make this a little clearer.  Also, concepts
used in this program are not introductory level, so dont worry if you dont
understand this program right off the bat.  

#include <iostream.h>
 
class xyz
{
public:
	xyx() {value = 0;}
	~xyz() {};

	int value;

	xyz& set() {value = 1; cout << "set" << endl; return *this;};
	xyz& clear() {value = 0; cout << "clear" << endl; return *this;};
	operator bool() const {cout << "op bool" << endl; return value;};
	operator !() const {cout << "op !" << endl; return value == 0;};
};

int main ()
{
	xyz abc;

	if(abc.set())
		cout << "true" << endl;
	if(!abc)
		cout << "shouldn't happen" << endl;

	if(!abc.clear())
		cout << "true" << endl;
	if(abc)
		cout << "shouldn't happen" << endl;
}

rna

On Fri, 18 Aug 2000, Eric Samson wrote:

> Ok, I looked up some reference on the syntax you used, and I am not sure I 
> understand it..  From what I gathered, (cin >> foo) is actually a test to 
> see if the input is of the type declared and it returns a NULL if not and a 
> non-NULL is legal?
> 
> 
> ----Original Message Follows----
> From: Robert Ambrose <rna@testpt.com>
> Reply-To: plug-devel@lists.PLUG.phoenix.az.us
> To: plug-devel@lists.PLUG.phoenix.az.us
> Subject: Re: Newbie C++ programmer again...
> Date: Fri, 18 Aug 2000 09:33:02 -0700 (MST)
> 
> 
> The short answer is the state in the ios parent class has to be reset to
> good (cleared).  The long answer reauires to much typing ;-), and I've
> spent to much time on this already (anthough I did learn some usefull
> things).  If you really want to know, I'm always happy to talk it over a
> beer.
> 
> Here's a working program:
> 
> #include <iostream.h>
> #include <ctype.h>
> #include <limits.h>
> 
> int main ()
> {
> 	for(;;)
> 	{
> 		double dollars;	// Maybe I'll win the lotto
> 
> 		cout << "Enter dollars:";
> 
> 		if(cin >> dollars)
> 		{
> 			double pounds;
> 
> 			pounds = dollars / 1.49;
> 			cout << "is " << pounds << " pounds" << endl;
> 			break;
> 		}
> 		else
> 		{
> 			cin.clear();
> 			cin.ignore(INT_MAX, '\n');
> 			cout << "Not a valid dollar ammount, try again" << endl;
> 		}
> 	}
> 
> 	return 0;
> }
> 
> rna
> 
> On Fri, 18 Aug 2000, Eric Samson wrote:
> 
>  > Ok, I have about gone nuts trying to figure this out...
>  >
>  > This is my assignment that I am having trouble with..
>  >
>  > It's a simple currency converter, the user enters a float which indicates
>  > how many US dollars they want to convert to a specified currency, in my 
> case
>  > Deutchmarks...  That is the easy part, got it written no problem, simple
>  > math... An optional part of the assignment is to validate the user input 
> and
>  > be sure it is a numeric value (float), and not an alpha charater, symbol,
>  > etc...  If they enter something that will not work, they are supposed to 
> be
>  > prompted to try to enter the info again...  Preventing negative values is 
> no
>  > problem with the <while (someinput < 0)> loop, but if they enter an alpha
>  > value, it enters an infinite loop.  I went to borders last night and my
>  > roommate and I (mostly because he is tired of hearing me whine about it)
>  > looked in a bunch of books for a solution, and didn't have any luck.  My
>  > instructor was no help either, he wasn't sure how to do it (he is a VB
>  > programmer teaching C++, go figure)...  This seems like something that
>  > should be painfully simple...  Is this something too complex for a 
> beginning
>  > programmer?  I should think that it would be a basic part of programming,
>  > but I could be wrong...  I notice a few people on here saying they don't 
> use
>  > C++ much, so what is the language of choice for programming in Linux?
>  >
>  > Thanks again,
>  >
>  > Eric
>  > ________________________________________________________________________
>  > Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
>  >
>  >
>  > _______________________________________________
>  > Plug-devel mailing list  -  Plug-devel@lists.PLUG.phoenix.az.us
>  > http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-devel
>  >
> 
> 
> _______________________________________________
> Plug-devel mailing list  -  Plug-devel@lists.PLUG.phoenix.az.us
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-devel
> 
> ________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
> 
> 
> _______________________________________________
> Plug-devel mailing list  -  Plug-devel@lists.PLUG.phoenix.az.us
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-devel
>