Hi everyone,
I'm writing a fairly simple application. Below is the part of the code that is giving me troubles. The headers I have are:
<stdio.h><stdlib.h><math.h>
This is just one function of few. It is supposed to ask the user for an integer, check that it's an integer and return the value to the main function. However, whenever I enter an integer during runtime, it somehow gets a crazy value (ie. I enter 15, it gets 238 or something).
Code:
int read_int(void){
int number=0;
char c='';
do{
c=getc(stdin);
if ((c!='1')&&(c!='2')&&(c!='3')&&(c!='4')&&(c!='5')&&(c!='6')&&(c!='7')&&(c!='8')&&(c!='9')&&(c!='0')&&(c!='\n')){
printf("Invalid input.\n");
exit(1);
}
if (c!='\n'){
number=number*10+atoi(&c);
}
}while(c!='\n');
return number;
I'm not sure what's wrong with it... It has worked before at the university with a different compiler, but I don't know how that would make a difference. I'm using the same idea for another function and it works there. Any help would be greatly appreciated.