I am scanning a string and I want that string to be only integer type elements so I did some validation for it and it worked for characters but it isn't working good enough for floating points. For e.g. If I enter 1.2 value as input then it does not desired output for single digit and works for 2 digit and more. Similarly if I enter 12.3 then it does not desired output for two digit and works for 3 digit and more. Can anyone tell me what's going on in here? Is there any way I can delete all the elements entered in the string so that I can get only integer values?
P.S. I am using string because I need to take number input from string as per my given question of program.
printf("Enter the number you want to convert into words:\n\n-->");
gets(numberToBeConvertedToWords);
for (i = 0; i != strlen(numberToBeConvertedToWords); i++)
{
while (isdigit(numberToBeConvertedToWords[i]) == 0)
{
// deleting the characters entered in the string.
memset(numberToBeConverted, 0, sizeof(numberToBeConverted));
printf("Please enter positive integers only.\n\n-->");
gets(numberToBeConvertedToWords);
}
}
printf("Entered number: %d", numberToBeConvertedToWords);
convertAndPrintNumberToWords(numberToBeConvertedToWords);
To answer the question as written:
How can I delete all data from string (char array) in C lang?
The typical way to do this is to set every character of the string to zero. The easiest way to do this is to use memset, assuming you know how much much valid memory the string has.
//Safe, because we know the length of the string
char s[100];
memset(s, 0, 100);
//Only safe if the string is correctly null-terminated
char* s = getSomeString();
memset(s, 0, strlen(s));
Addressing the implied question:
It seems like the question you're actually trying to ask is "why does my code not do what I expect?". Vague "help me" questions are usually off-topic for Stack Overflow. You'll get a better response if you can ask a concrete question about your problem. In this specific case, it would be helpful to get the 'incorrect' output, as it's difficult to understand what the unexpected behaviour actually is.
However, I would take a guess that the reason that your edited code isn't doing what you expect is because the while
is in a strange place. If you encounter a '.', isdigit
will return 0 and you will correctly wipe the string. However, you then get a new string inside the while loop, which means you start validating the new string from the current position, not the start of the string.
You probably want to move the while loop outside the for loop:
BOOL valid = true;
do
{
gets(numberToBeConvertedToWords);
// reassigning the bool value as "true" so that loop does not to go infinite
// loop just in case if it's changed from 'if' block.
valid = true;
for (i = 0; i != strlen(numberToBeConvertedToWords); i++)
{
if(!isdigit(numberToBeConvertedToWords[i]))
{
// deleting the characters entered in the string.
memset(numberToBeConverted, 0, sizeof(numberToBeConverted));
printf("Please enter positive integers only.\n\n-->");
valid = false;
break;
}
}
} while(!valid);