# Thread: Need help from the gurus. homework

1. ## Need help from the gurus. homework code provided.

here is the question posed.

# Ask the user to enter a digit between 0 and 9. Have the program print out the digit in words, for example:

Enter a digit between 0 and 9: 4
You entered the number four

Assume that the user will enter only a single digit. The user may accidentally enter a single character, and this should generate an error message.

Code:
```#include <stdio.h>
int main()
{
int num;
printf("Please enter a digit from 0 through 9\n");
scanf("%d,",&num);

if (num == 0)
printf("The number you have typed is zero.\n");
else if (num == 1)
printf("The number you have typed is one.\n");
else if (num == 2)
printf("The number you have typed is two.\n");
else if (num == 3)
printf("The number you have typed is three.\n");
else if (num == 4)
printf("The number you have typed is four.\n");
else if (num == 5)
printf("The number you have typed is five.\n");
else if (num == 6)
printf("The number you have typed is six.\n");
else if (num == 7)
printf("The number you have typed is seven.\n");
else if (num == 8)
printf("The number you have typed is eight.\n");
else if (num == 9)
printf("The number you have typed is nine.\n");
else
printf("You have selected to not follow the instructions therefore an error has occurred.\n");

return 0;
}```
when i type in other integers greater then 9 i display the error message, however when i type a character i return the your number is two.

let me know where i went wrong.

2. Go through the following code. If you have any questions, please don't hesitate to ask!
Code:
```#include <stdio.h>
#include <ctype.h>
int main()
{
int num;
printf("Please enter a digit from 0 through 9\n");
num=getchar();
if (isdigit(num))
{
if (num == '0')
printf("The number you have typed is zero.\n");
else if (num == '1')
printf("The number you have typed is one.\n");
else if (num == '2')
printf("The number you have typed is two.\n");
else if (num == '3')
printf("The number you have typed is three.\n");
else if (num == '4')
printf("The number you have typed is four.\n");
else if (num == '5')
printf("The number you have typed is five.\n");
else if (num == '6')
printf("The number you have typed is six.\n");
else if (num == '7')
printf("The number you have typed is seven.\n");
else if (num == '8')
printf("The number you have typed is eight.\n");
else if (num == '9')
printf("The number you have typed is nine.\n");
}
else
printf("You have selected to not follow the instructions therefore an error has occurred.\n");

return 0;
}```
As a further exercise, try making the code more compact.

3. yeah i appreciate the help

the isdigit i assume detects if it is an actual interger.

I could make it more compact however it was the display as a word which got me.

I hope by me providing code and not asking people to do my homework, I can come back for more.

Sad i am a nuclear engineering major but c kicks my but i cant think in a logarithmic fashion to well sometimes.

4. > the isdigit i assume detects if it is an actual interger.
It checks by lookup if ascii values are decimal digits. That's all. C is generally type safe so an int stays an int unless you cast it to something else or attempt to store a value differently.

> I hope by me providing code and not asking people to do my homework, I can come back for more.
Yes.

5. > Sad i am a nuclear engineering major but c kicks my but
What does a nuclear engineer (should we all be afraid at this point?) want with a language which isn't rated for such safety critical applications like nuclear installations?

7. *contemplates the irony of his own avatar...*
If you're dyslexic, are you unclear about nuclear?

8. Well my friend who posted there code the issue is the last Else if statement is var ==9 so you have a few things you can do. A just simply add a loop. Inside all the Else If statements place a break inside all the statements. Then add an Else with a Printf saying retype the number and as a result you reloop. Another way is just too simply say but have it wrong n<=9. a little back story the reason why when you typed in a char you received an error is because you scanf is looking for a decimal "scanf("%d,",&num);" and in ANSI C all chars are trying ints example A==1 B==2 (also caps are greater value then lower case A>a). To avoid this error to a degree is to have the code gets(num) then num1 = atoi(num). This will take any int or char and turn it into an int. Lets say you typed in 12334 and did not have atoi what can happen is the computer looks at the number like a solid string "12334". Sorta like you zip code but if you atoi the int string it will break it up to read 12,334 and from there you can apply you int.

9. Stop hijacking threads, posting opinionated nonsense, and generally acting like an idiot.

10. Originally Posted by Salem
> Sad i am a nuclear engineering major but c kicks my but
What does a nuclear engineer (should we all be afraid at this point?) want with a language which isn't rated for such safety critical applications like nuclear installations?
It is a requirement for the degree is all. I chose it a class due to my limited linux and programming skills and needing to learn more about programming languages. Plus learning BASIC was not really exciting to me. I work more on the hardware end and use ladder logic and AML programming at my work and C will help big time in coding and debugging practices especially pertaining to robotics.

I appreciate the humorous comment though.

BTW only reason i am taking nuclear engineering major is cause i was prior military, waiting to get that done to work on a project management - possibly IT or business focused MBA.

Thanks again everyone and honestly i would be scared if a reactor was only electronically controlled chernobyl 2007 haha

11. That would be like Chernobyl/Matrix