I found this solution in another post My prof posted this Im not sure how it helps but I thought it might help someone To make sure that a user-input number is an integer you can use the notion of casting.
Casting is a way to tell C to treat a variable as if it were a variable of a different type.
I'm taking C1 and I don't know if that's # or or just C or even what the differences are between those.
Anyways my question is how do I make sure that a user enters an integer instead of a float, or make sure that the person did not enter a & symbol into the float input.
The C convention is for octal formats to have a leading Sorry, people seem to be raving over this answer, but I expected exactly what you wrote from the algorithm but it didn't work. will read, convert, and assign the leading "12", leaving the "efg" in the input stream.
I'm supposed to check the value so that it's never a non-integer, and your solution doesn't cut it. If the input string doesn't start with a digit (such as "ab123"), none of the input string will be read, and I've been searching for a simpler solution using only loops and if statements, and this is what I came up with.
The function is really only evaluating that the user's input is not a "0", but it was good enough for my purpose. This method works for everything (integers and even doubles) except zero (it calls it invalid): The while loop is just for the repetitive user input. If it does (as it would with a number), its an integer/double. Read the input as a string, and use atoi() function to convert the string in to an integer.
atoi() function returns the integer number if the input string contains integer, else it will return 0.
Ok so I want to preface this by saying I know very little about C.
I'm studying it independently at my high school and so I've come to you folks for help.
The program also works with negative integers and correctly rejects any mixed inputs that may contain both integers and other characters.