0

want user to input an integer value...the integer value is then converted to an ascii string....the ascii string is then passed into isValidNum(num); and it should return 1 if the number is valid and 0 if number is invalid.


for some reason it keeps returning 1 no matter what value i enter....any help would be greatly appreciated where i'm goofing up at! thx

int isValidNum( char str[] )
{
	int i = 0;
	int valid = TRUE;
	int sign = FALSE;

	if( str[0] == '\0' ) 
		valid = FALSE;

	if( str[0] == '-' || str[0] == '+' ) {
		sign = TRUE;
		i = 1;
	}

	if( sign == TRUE && str[1] == '\0' )
		valid = FALSE;

	while( valid == TRUE && str[i] != '\0' ) {
		if( str[i] < '0' || str[i] > '9' )
			valid = FALSE;
		i++;
	}

	return valid;
}

int main()
{
  int num = 33;
  char string[sizeof(int)+1] = {'\0','\0','\0','\0','\0'};

  itoa( num, string, 10 );
  printf("%d", isValidNum(string));
  
  return 0;
}

Edited by lochnessmonster: n/a

4
Contributors
7
Replies
9
Views
6 Years
Discussion Span
Last Post by WaltP
Featured Replies
  • 1
    WaltP 2,905   6 Years Ago

    If the number must be [I]exactly[/I] 5 digits, you'd be better off reading the number as a string and passing it into your function. By reading an int, you have the possibility of an error on the read if they type in a non-digit character. Reading a string removes that … Read More

0

I would try simplifying your code...

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>

bool isValidNum( char str[] )
{
	int i = 0;
	
	for (i = 0; i < strlen(str); ++i)
	{
		if (!isdigit(str[i]))
			return false;
	}

	return true;
}

int main()
{
	char string[] = {'\0','\0','\0','\0','\0'};

	printf("%d", isValidNum(string));

	return 0;
}

Edited by gerard4143: n/a

0

the main reason im not doing it like this is because the idnum must be atleast 5 digits long! your way is nice :) but wont quite work for my situation

0

the main reason im not doing it like this is because the idnum must be atleast 5 digits long! your way is nice :) but wont quite work for my situation

I really don't understand your concerns. If you have more information, please post it.

Edited by gerard4143: n/a

0

the number must be atleast 5 digits long!

now that i look at it i think thats where i messed up...tbh i have no idea how i would do this.

0

I fail to see how this won't work

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>

bool isValidNum( char *str )
{
	int i = 0;
	
	for (i = 0; i < strlen(str) - 1; ++i)
	{
		if (!isdigit(str[i]))
			return false;
	}

	return true;
}

int main()
{
	char string[10];

	fputs("enter a number->", stdout);
	fgets(string, 10, stdin);

	printf("%d", isValidNum(string));

	return 0;
}

Edited by gerard4143: n/a

0

I could tell you how I would do it, but it smells like homework and something you should be working out on your own.

If you have specific problems, post complete, compilable code and ask specific questions.

1

If the number must be exactly 5 digits, you'd be better off reading the number as a string and passing it into your function. By reading an int, you have the possibility of an error on the read if they type in a non-digit character. Reading a string removes that possibility.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.