Hey,
i want to enter a number by user.
printf("Enter a number: ");
scanf("%d",&number);
but,user enter text.i mean:
Enter a number: One
Now,i want to convert this text to number.
I hope,i tell my problem.Sorry my bad english.
Hey,
i want to enter a number by user.
printf("Enter a number: ");
scanf("%d",&number);
but,user enter text.i mean:
Enter a number: One
Now,i want to convert this text to number.
I hope,i tell my problem.Sorry my bad english.
char numberAsString[6];
char numbers[][]=
{"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
do{
printf("Enter a number (0-9): ");
scanf("%d",&number);
}while(number<0||number>9);
strcpy(numberAsString, numbers[number]);
printf("You entered %s.\n", numberAsString);
Ah, right. I sometimes get mental dyslexia when I read something too fast (and subsequently jump into coding). Two ways to do this.
If you just want to get numbers 0 through 9 (or even 19), you can make an array of strings, and for-loop through them using strcmp. If strcmp gives you 0, you've got your match. Break out of the for-loop and return the number to the user or whoever wants it.
However...
If you want a full-fledged Text to Number, we need to do things differently. Let's start with the following even though it is wrong:
This will handle numbers 0 from 19. It will even handle the individual multiples of ten (twenty, thirty, forty, etc...). But it won't handle larger numbers. Why not?
We're only working with one word.
Looping and multiplying by 10 won't work either.
To get 123 we'd have to say "one two three" instead of "one hundred twenty three".
Before I go to far, I presumed I'd have to section out the sub-thousand numbers into their own little word before adding them to some sort of accumulator. I even toyed briefly with the idea of parsing it into byte code instructions (i.e. add 10, multiply by 100, etc.), then executing the byte code like it was a scripting language.
We won't be doing that.
I stopped to do a little research while writing this post. I found this:
http://www.cplusplus.com/forum/general/36407/
It's unanswered, but it effectively says that. But my mind did open a little and I considered the sub-thousand numbers. You'd have to create a sort of "working register" for them. Then when you hit "million", "billion" or just the end of the line, you multiply the working register by the appropriate value and add it to your accumulator.
That's all the help I can give you for now. Happy coding!
Ah, right. I sometimes get mental dyslexia when I read something too fast (and subsequently jump into coding). Two ways to do this.
If you just want to get numbers 0 through 9 (or even 19), you can make an array of strings, and for-loop through them using strcmp. If strcmp gives you 0, you've got your match. Break out of the for-loop and return the number to the user or whoever wants it.
However...
If you want a full-fledged Text to Number, we need to do things differently. Let's start with the following even though it is wrong:
- setup a hash table of lower-case strings and their corresponding integers.
- convert the string to lower-case
- read through the string past any initial white space to the actual word and up to the next non-letter (presumably white space)
- hash the word
- find the hash in a mapped table and get the associated integer.
This will handle numbers 0 from 19. It will even handle the individual multiples of ten (twenty, thirty, forty, etc...). But it won't handle larger numbers. Why not?
We're only working with one word.Looping and multiplying by 10 won't work either.
To get 123 we'd have to say "one two three" instead of "one hundred twenty three".Before I go to far, I presumed I'd have to section out the sub-thousand numbers into their own little word before adding them to some sort of accumulator. I even toyed briefly with the idea of parsing it into byte code instructions (i.e. add 10, multiply by 100, etc.), then executing the byte code like it was a scripting language.
We won't be doing that.
I stopped to do a little research while writing this post. I found this:
http://www.cplusplus.com/forum/general/36407/
It's unanswered, but it effectively says that. But my mind did open a little and I considered the sub-thousand numbers. You'd have to create a sort of "working register" for them. Then when you hit "million", "billion" or just the end of the line, you multiply the working register by the appropriate value and add it to your accumulator.That's all the help I can give you for now. Happy coding!
ok,thx.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.