Testing a string as a valid float

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 791 Views - Last Post: 03 April 2013 - 01:02 PM Rate Topic: -----

#16 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: Testing a string as a valid float

Posted 03 April 2013 - 12:52 PM

Quote

What the heck is that?!? while(1)?!?

An endless loop, you exit by pressing Ctrl-C, not a graceful exit but it allows you to throughly test your function until you're satisfied you have covered "all" the possibilities.

Jim
Was This Post Helpful? 0
  • +
  • -

#17 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Testing a string as a valid float

Posted 03 April 2013 - 12:54 PM

View Postjimblumberg, on 03 April 2013 - 02:52 PM, said:

Quote

What the heck is that?!? while(1)?!?

An endless loop, you exit by pressing Ctrl-C, not a graceful exit but it allows you to throughly test your function until you're satisfied you have covered "all" the possibilities.

Jim


I think my head just exploded.


I have a lot more issues I'm trying to work on for this program. I don't have time to try to figure them out on my own anymore.. I'm sorry if I post a million times today and tomorrow. My usual rule of thumb is work on it by myself for at least 8 hours, but I don't have that luxury right now.

This post has been edited by synlight: 03 April 2013 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

#18 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,215
  • Joined: 02-June 10

Re: Testing a string as a valid float

Posted 03 April 2013 - 12:54 PM

View Postsynlight, on 03 April 2013 - 01:45 PM, said:

View PosttlhIn`toq, on 03 April 2013 - 02:36 PM, said:

Your loop from 19-23 of second block has a bug: YOu don't stop if you hit a false.
So it could all be false, so long as the last char is a period. At which point isValid becomes true.

There is no point continuing after a failure. Just return false at that point.

In the first block, you should move 14-15 in place of 07. That way the loop will keep asking AND THEN PROCESS. Have this loop at 14-15 with no processing after is pointless.

UPDATE: References to your original code. You updated as I was typing. <laugh>


Like this:
double getBalance()
{
	char testBal[40];
	double balance = 0.00;
	bool floatTest = false;

	while(!floatTest)
	{
		getString("\nPlease enter the balance due:", testBal, 40);
		floatTest = validFloat(testBal);
	}

	atof(testBal);
	
	return balance;

}//END GETBALANCE


How could I tell the user that they entered an invalid number?


Come on... Now you're just asking without even thinking first.
10.5 if (!floatTest) cout<< "Is that a valid decimal?";
Was This Post Helpful? 1
  • +
  • -

#19 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Testing a string as a valid float

Posted 03 April 2013 - 12:56 PM

Ugh I'm sorry. I got all excited and got chatty like we were sitting here having a beer.
Was This Post Helpful? 0
  • +
  • -

#20 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,215
  • Joined: 02-June 10

Re: Testing a string as a valid float

Posted 03 April 2013 - 01:02 PM

View Postsynlight, on 03 April 2013 - 01:56 PM, said:

Ugh I'm sorry. I got all excited and got chatty like we were sitting here having a beer.


No worries mate. I said exactly what I would have said if you were sitting right here. Only without the Gibbs-slap to the back of the head.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2