Skip to content
Advertisement

C program works on my Ubuntu VM terminal but not on school Linux server?

I have this C program that runs perfectly on my Ubuntu Virtual Machine but does not run at all(doesn’t prompt user for input, just finishes) on the school Linux server.

School Linux version: Linux 2.6.18-371.9.1.e15 x86_64

My Ubuntu VM version: Linux 3.16.0-33-generic x86_64

Here is the program:

 #include <stdio.h>
 #include <string.h>
 int main()
 {
    char value[50];
    char *end;
    int sum = 0;
    long conv;
    while(conv != 0 )
    {
            printf("Enter a measurement and unit(Ex: 4' or 3";0' or 0" when done): ");
            fgets(value, 50, stdin);
            conv = strtol(value, &end, 10);
            if(strstr(value, "'") != NULL)
            {
                    conv = strtol(value, &end, 10);
                    sum = sum + (conv*12);
            }
            else if(strstr(value, """) != NULL)
            {
                    conv = strtol(value, &end, 10);
                    sum = sum + conv;
            }
    }
    printf("Total: %d, %sn", sum, "inches" );
    return 0;
 }

Any idea why this is??

P.S. thanks to those who helped me with this program from an earlier question 🙂

Advertisement

Answer

You need to initially set conv, for example:

long conv = ~0;

As far as it running perfectly on one machine and not at all on another, you just got lucky. conv does have a value, even if you don’t explicitly set it. On one machine, it was 0 and on the other it was something other than 0, hence the different behavior.

Another method would be to use a do while loop:

do
{
    ...
}
while(conv != 0);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement