Can't find the problem

Problem in checking if an array of characters can represent a float va

Page 1 of 1

8 Replies - 1233 Views - Last Post: 21 December 2009 - 02:19 PM Rate Topic: -----

#1 CodingDesire   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 21-December 09

Can't find the problem

Post icon  Posted 21 December 2009 - 01:40 PM

Hello everyone, this is my first post there. I have to create a function which checks whether an array of characters can represent a float or no. I was using Pascal (Old-School, yes) until yesterday, and I've decided to move to C++ (I'm using Visual Studio 2008 right now). I can't say that I'm that familiar with the syntax, so here's my code:

bool is_float(const char buff[256])
{
	if (buff[0] == '.')
		return false;

	int err = 0;

	for (int i = 0; i <= strlen(buff); i++)
		if (buff[i] != '.')
			err++;

	if (err == strlen(buff))
		return false;
	
	int suc = 0;
	int nr = 0;

	for (int i = 0; i <= strlen(buff); i++)
		if (buff[i] == '.')
			nr++;

	if (nr >= 2)
		return false;

	for (int i = 0; i <= strlen(buff); i++)
		if (isdigit(buff[i]) || buff[i] == '.')
			suc++;

	if (suc == strlen(buff))
		return true;

	return false;
}



The problem is that the function always returns false. I think the logic is correct, but still I couldn't figure out the problem. Any help would be greately appreciated. Thanks everyone for taking their time to watch the post.

This post has been edited by CodingDesire: 21 December 2009 - 01:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Can't find the problem

#2 debjit625   User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 452
  • Joined: 06-September 08

Re: Can't find the problem

Posted 21 December 2009 - 01:44 PM

As you have writen
return false;



isnt
if you didnt understand that ,it just that if the execution execute the last line that is "return false" then it will return false.again if any condition is true then also like the first one you say that if the first char of buff is '.' then return false from my function.Now comes the logic that part you have to do.

The only condition that will return a true is
if (suc == strlen(buff))
		return true;



Good Luck

This post has been edited by debjit625: 21 December 2009 - 01:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 GenHornet18   User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • Posts: 629
  • Joined: 19-October 09

Re: Can't find the problem

Posted 21 December 2009 - 01:45 PM

I can't say I understand the purpose of this program. What exactly is this function supposed to check for in a char array? Could you elaborate?
Was This Post Helpful? 0
  • +
  • -

#4 CodingDesire   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 21-December 09

Re: Can't find the problem

Posted 21 December 2009 - 01:51 PM

The program has absolutely no purpose. I have made it just to make myself familliar with C++. It checks if an array of charracters can represent a float value or not. This is the main program:

int main()
{ 
	char input[256];
	std::cout << "Please enter a number: ";
	std::cin >> input;

	if (is_float(input))
	   std::cout << "The number represents a float value" << std::endl;
	else
	   std::cout << "The number does not represent a float value" << std::endl;

	return 0;
}



If my input is 34.193, the function should return 'true', because my input represents a float value. If my input is 'alex', then the function should return 'false', because this isn't even a number. If my input is 33, then the number is an integer, not a float, so it will return 'false'. Did you get it now?
Was This Post Helpful? 0
  • +
  • -

#5 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Can't find the problem

Posted 21 December 2009 - 01:53 PM

if your using c++ just use the STL string and use is digit to check if it is numeric
Was This Post Helpful? 0
  • +
  • -

#6 CodingDesire   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 21-December 09

Re: Can't find the problem

Posted 21 December 2009 - 01:57 PM

Debjit, as I know, if the line :

if (suc == strlen(buff))


succedes, then the function will return true, and will ignore the 'return false' part, which is the default returning value of the function, but the problem is that this line never executes.

This post has been edited by CodingDesire: 21 December 2009 - 01:57 PM

Was This Post Helpful? 0
  • +
  • -

#7 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Can't find the problem

Posted 21 December 2009 - 02:09 PM

I dont understand why you have some much code for a task like this

bool checkArray(char *myArray, int size)
{
	bool flag = true;
	for(int i=0; i<size; i++)
	{
		if(!isdigit(myArray[i])&&myArray[i] != '.')
			flag = false;
	}
	return flag;
}


ofcourse that would only work if there was for sure a decimal
Was This Post Helpful? 0
  • +
  • -

#8 debjit625   User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 452
  • Joined: 06-September 08

Re: Can't find the problem

Posted 21 December 2009 - 02:11 PM

Think you have entered 33.55 then the problem is in using strlen() function as it returns the size of a null terminated string from starting with 1 to 5 and you are counting 0 to 5 inside the loop that means you counted 6 times. and the value of err will become 5. thats makes the function to return false.

Changed code
for (int i = 0; i < strlen(buff); i++)
{ 
		if (buff[i] != '.')
			err++;
}



and in loops and conditions use braces(scopes are needed for multipal instructions)

Good Luck

This post has been edited by debjit625: 21 December 2009 - 02:21 PM

Was This Post Helpful? 1
  • +
  • -

#9 CodingDesire   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 21-December 09

Re: Can't find the problem

Posted 21 December 2009 - 02:19 PM

Oh, that was it debjit. Thank you very much, it fixed my problem.
ImaSexy, i could simplify it too of course, but as you can see, I have more additional checks. Take some time to look and you will see that there's no hardcode as you may think. EDIT: your code is wrong too.

Braces are needed for multiple instructions inside loops, conditions, yes I know. Here not the case for that. The problem is that the code worked in Pascal, and I think that the indexing is done differently in C++. As I said, your fix provided full functionality of the function. Thanks again.

This post has been edited by CodingDesire: 21 December 2009 - 02:32 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1