问题描述:

I have this project I'm working on, and after a week of coding, I finally completed it. I created the program (compiled it) using Code::Blocks (I'm using only Windows 7). It runs perfectly.

However, if I transfer the same c-file to a virtualbox(ubuntu), when I compile it, there were no errors, but the way the program runs is different.

In my program there are numerous functions for input restrictions. In my virtual box, I can't seem to pass through the first restriction. I have yet to discover if my I will not be able to get through the next restrictions.

for example:

 while(1){

printf("Enter the number of rows:");

fgets(rows, 100, stdin);

string_clean(rows); // '\n' to '\0'

c=numstring_check(rows, strlen(rows));

if(c==0){

printf("INVALID!\n");

}else if(c!=0){

rows_i=strtol(rows, &remain, 10);

if ((rows_i>10 || rows_i<1)){

printf("INVALID!\n");

continue;

}else

break;

}

}

numstring function is:

int numstring_check (char array[], int size) {

int i,j,flag=0;

for (i=0; i<size; i++){

j=isdigit(array[i]);

if(j!=1){

flag=1;

break;

}

}

if (flag==1)

return 0;

}

This is only one problem. My dilemma is, I have multiple functions that run through this function and this isn't my only input checker.

If this block of code is run through codeblocks, it works fine, but on ubuntu, it just prints "Invalid" whatever input I encode. I need serious help.

网友答案:

the function: isdigit() returns a NON_ZERO when the parameter is a digit.

That does not mean that it will return 1. Suggest using:

if( !j ) 

as your test for a non digit. A better plan would be to eliminate j so the call to isdigit() and the if() are combined into:

if( !isdigit(array[i]) )
相关阅读:
Top