Search This Blog

Wednesday, July 20, 2011

Inputing numbers

In the code below I am attempting to establish a method of inputing a year with digits (only digits) into the character array of year. It works, but now if the inputed non-digits are 4 or more (i.e. “abcd”) the program runs the printf command more than once. I recommend you examining in a compiler so you know what I mean. Please if anybody knows why is the program doing this?


#define kYear 5


int main() {


char year[kYear];

short i;

bool isYear;


do{

for(i=0;i!=(kYear-1);i++)

year[i]=0;

printf(“Enter year introduced: “);

fgets(year, kYear, stdin);


for(i=0;i
if(isdigit(year[i])){

isYear=true;

}

else{

isYear= false;

break;

}

}


}while(!isYear);

}

http://bit.ly/qfZqtm

No comments:

Post a Comment