-
Help again
Here I have already written the code which prints the rightmost integer of a float number, however when I run the program and put in a number, the program closes immediately. Can anyone help me fix this problem?
Code:
#include <stdio.h>
int main (void)
{
// Local Declarations
int fltNum;
int oneDigit;
// Statements
printf("Please enter in a float number: ");
(float)fltNum;
scanf("%d", &fltNum);
oneDigit = fltNum % 10;
printf("\nThe right digit is: %d", oneDigit);
scanf("%d", &oneDigit);
int temp;
printf("Enter an integer and press Enter to exit the program: ");
scanf("%d", &temp);
return 0;
}
-
This statement has no effect. It doesn't magically turn fltNum into a float, just casts it, but you don't do anything with the value after casting it. Get rid of it.
Quote:
Code:
scanf("%d", &oneDigit);
Read the scanf documentation, I feel you are greatly misunderstanding it's use (cf you previous post). The above line scans the input stream for an integer, and stop when it finds some non-integer characters. If you type 123.45, all it grabs is 123. You then have ".45" left in the input buffer. When you do the following scanf("%d", temp), scanf skips the . and reads 45 as an int, puts it in temp and exits your program.
Also, you can use fgets in combination with sscanf to avoid some of these problems.
-
I keep getting errors. The only problem is that the program closes immediately and I did remove the scanf("%d",temp)
-
Did you read the docs? I didn't say to remove the scanf("%d", &temp), I implied that the "%d" isn't doing what you think it's doing, i.e. it's not scanning for a float. Here's the man page for scanf, so you can understand the different format string specifiers.