I'm trying to write a program that reads a five digit zip code, then outputs the numbers in word form. The program would also only accept a 5 digit number. So if the input was 123, the output would be "You entered 3 digits" and loop back until the user entered five digits. Once they did, it would output the number in word format. So if 12345 was entered, the output would be one two three four five. This is what I have so far:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
int main(void) {
int ac, count = 0;
printf("Enter a 5 digit area code: ");
scanf("%d", &ac);
do {
ac /= 10;
count;
} while (ac != 0);
while (count != 5) {
printf("You intered in %d digits.\n", count);
printf("Enter a 5 digit area code: ");
scanf(" %d", &ac);
}
while (count == 5) {
}
return(0);
}
There are two issues I need help with. The first is that whenever a number that doesn't have five digits is input, it outputs correctly once, and then the loop is infinite. If I entered 123, output would be "You entered 3 digits," and then it would prompt me to enter another number. If I then entered 12345 it would output "You entered 3 digits" again. I'm new to loops not sure where this problem is coming from.
The second issue is just the conversion from numbers to words, I've never encountered this issue and I'm not sure where to start.
CodePudding user response:
For the first issue, the second while loop condition always evaluates false if the number is not 5 digits. You have to update count
each time you get a new number, this means you have to repeat the do ... while
loop. A function will be great in this case, and never forget to check the return value of scanf()
is the only you know the conversion succeeded (you scanned a number).
Your function can be something like this,
int count_digits(int num)
{
int digits = 0;
do
{
digits ;
num /= 10;
} while (num != 0);
return digits;
}
and your updated main()
int main(void)
{
int ac = 0, count = 0;
printf("Enter a 5 digit area code: ");
if (scanf("%d", &ac) != 1) /* you read one int */
{
/* conversion error - maybe display error message */
exit(EXIT_FAILURE);
}
count = count_digits(ac);
while (count != 5)
{
printf("You intered in %d digits.\n", count);
printf("Enter a 5 digit area code: ");
if (scanf(" %d", &ac) != 1)
{
exit(EXIT_FAILURE); /* include <stdlib.h> for exit */
}
count = count_digits(ac);
}
}
As for the second part of your question, its not clear if you want to perform some operations on the number. If you need to have it as a int, you may use snprintf()
and large enough buffer to convert it to a string.
char buff[100] = "";
snprintf(buff, sizeof(buff), "%d", ac);
If you don't need to perform any operations on the number, you can read it as string right away, with fgets()
.
char buff[100] = "";
fgets(buff, sizeof(buff), stdin);
(This will also work for zip codes starting with 0)
CodePudding user response:
Sometimes it helps to see if there is a way to simplify the method to do what you are trying to do. In this case maybe eliminating a while loop, but still keeping the ability to iterate until the right input is entered using a helper function to do the work...
The main:
int main(void)
{
int num;
char buf[20]={0};
printf("Enter a 5 digit number:");
while (!isNumeric(buf))//helper function
{
printf("try again\n:");
scanf("%s", buf);
}
printf("%s", buf);
return 0;
}
The helper function:
bool isNumeric(char *number)
{
bool allgood = false;
if(strlen(number) != 5) return allgood;
while(*number)
{
if(!isdigit(*number))
return allgood;
number ;
}
return true;
}