Some weird issue.

• 04-11-2009
dbzx
Some weird issue.
So my program basically allows the user to enter a positive integer and then a menu will pop up like this:

Enter a positive integer: 11
1. Is the given number an even or an odd number
2. Find out if this number is a prime number or not
3. Calculate the factorial of this number
4. Find the sum of the numbers from 0 to this number
5. Return and integer showing the number in base of 8
6. Exit

Then the user can pick which ever function they want to do and the program will print out the answer.

This program works perfectly fine on Dev C++, but when I copy the code to the Unix Server for my school using Putty, I get this weird issue for the sum function. Basically, when I run the program and choose 4 at the beginning I get the right answer for sum, 66(using input = 11). But if i go through each choice, starting from one and then go to four, i get 78. This was not an issue that all on Dev C++. Anyone know the reason?

Heres the code

Code:

```#include <stdio.h> void menu(); int odd_even(int); int prime(int); int factorial(int); int sum(int); int base(int); int main() {     int input;     int choice = 0;             printf("Enter a positive integer: ");     scanf("%d", &input);         while (choice != 6 && input > 0)     {                menu();           printf("Your choice:");           scanf ("%d", &choice);               switch (choice)           {           case 1:                 odd_even(input);                 break;           case 2:                 prime(input);                 break;           case 3:                 printf("The factorial of %d is %d", input, factorial(input));                 break;           case 4:                 printf("Sum of numbers from 0 to %d is %d", input, sum(input));                 break;           case 5:                 printf("%d in base 8 is %d", input, base(input));                 break;           case 6:                 printf("Goodbye!\n");                 break;           default: /* If the value entered is invalid, prompt the user to enter again */                                 printf("\nInvalid Input. Try Again !\n");           }     }     return 0; } void menu() {         printf("\n1.    Is the given number even or odd \n");         printf("2.    Find out if this number is prime or not \n");         printf("3.    Calculate the factorial of this number \n");         printf("4.    Find the sum of the numbers from 0 to this number \n");         printf("5.    Return an integer showing the number in base of 8\n");         printf("6.    Exit\n"); } int odd_even(int input) {      if (input%2 == 0)       printf(" %d is an even number", input);     else         printf("%d is an odd number", input);     return 0; }        int prime(int input) {     int divisor;         for (divisor = 2; divisor <= input; divisor++) {         if (input%divisor != 0 && input != divisor)           printf("%d is a prime number", input);         else             printf("%d is not a prime number", input);         break;     }     return 0; } int factorial(int input) {     int count;     int result = 1;         for (count = 1; count <= input; count++)         result = result * count;             return result; } int sum(int input) {     int count = 0;     int sum;       for (count = 0; count <= input; count++)         sum = count + sum;         return sum; } int base(int input) {     int digits, power, result;         power = 1;        /*Value of digit position 0*/         result = 0; /*Where the decimal result will go*/         while(input > 0)         {                 digits = input % 8;                /*Get right most digit*/                 result += digits * power;        /*If 1, add its position value to the result*/                 power *= 10;                                        /*Increase the value of the next digit position*/                 input /= 8;                                /*Knock off the right most digit*/         }         return result; }```
• 04-11-2009
Drogin
This is probably where the problem is:
Code:

```int sum(int input) {     int count = 0;     int sum;       for (count = 0; count <= input; count++)         sum = count + sum;         return sum; }```
You're using the sum variable without initializing it first.
You must initialize it like this:
int sum = 0;

This is because you use
Code:

`sum = count + sum.`
So when this runs for the first time it will go like this:
sum = count + ??
• 04-11-2009
BEN10
sum is taking any garbage value while calculating
sum=count+sum;
so your output is not as expected.
• 04-11-2009
dbzx
thanks for the input but that didnt solve the problem. and it seems like after a certain choice, it will give me a different sum. (eg. choice 1 then 4, sum will be 77, choice 2 then 4 = 77, choice 3 then 4 = 78, choice 5 then 4 = 67)

any other suggestions?
• 04-12-2009
Drogin
Are you 100% sure that you edited the the right source-code, to this:
Code:

```int sum(int input) {     int count = 0;     int sum = 0;       for (count = 0; count <= input; count++)         sum = count + sum;         return sum; }```
saved the changes, compiled it, and ran the exact binaryfile you just compiled?
Anyway, try again. Make sure you edited your souce-code so that the sum() function looks like the one I just posted.
Make sure you compile the right file if you got multiple versions of the program or at different PC's or servers.
Make sure you execute the file that you just made, if you got multiple files or some at different servers.

Because I can't find any more reasons for your bug, and I just complied it on Linux without warnings or errors, and I tested the program using different commands...it runs just fine.
• 04-12-2009
dbzx
yes i am 100 percent positive. like i said, this program works with no issues on the IDE, Dev C++, just for some reason the Unix Server is being retarded....
• 04-12-2009
dbzx
i finally found out the reason. i changed the first 2 functions which return 0 to void functions because all it does it print stuff. anyways, thanks for all the input.
• 04-12-2009
Drogin
Quote:

Originally Posted by dbzx
i finally found out the reason. i changed the first 2 functions which return 0 to void functions because all it does it print stuff. anyways, thanks for all the input.

That's strange..that should'nt be a problem.
May I ask what compiler and what version you use?