Some weird issue.

This is a discussion on Some weird issue. within the C Programming forums, part of the General Programming Boards category; So my program basically allows the user to enter a positive integer and then a menu will pop up like ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    23

    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
    Your choice: .....

    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;
    }

  2. #2
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    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 + ??

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    sum is taking any garbage value while calculating
    sum=count+sum;
    so your output is not as expected.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    23
    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?

  5. #5
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    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.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    23
    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....

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    23
    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.
    Last edited by dbzx; 04-12-2009 at 03:44 PM.

  8. #8
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Quote Originally Posted by dbzx View Post
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 10:23 PM
  2. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  3. weird pointer issue
    By Chaplin27 in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2006, 09:20 AM
  4. Weird gethostbyname() issue
    By Xterria in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-26-2006, 08:44 PM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21