Thread: Simple Math Program

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    28

    Simple Math Program

    I haven't programmed in C in a while, and I decided to write a simple program for a friend of mine. It's a numerology program that takes your name, converts each letter into a number in the alphabet (A = 1, B = 2, Z = 26, etc.). I wrote a function that takes a number and splits the digits up, adding them together to form a single digit, and return that digit, only when I run it, it only returns 9, no matter what number I put in (23, 543, 7, etc.) Can you help me? I don't know what I did wrong.

    The code below is a program to test the crunch function:
    Code:
    #include<stdio.h>
    
    int crunch(int num);
    
    int main()
    {
    	int c, d;
    
    	printf("Enter a number: ");
    	scanf("%d", c);
    
    	d = crunch(c);
    
    	printf("%d", d);
    	return 0;
    }
    
    int crunch(int num)
    {
    	int sum = 0;
    	int tmp;
    	while(num != 0)
    	{
    		tmp = num % 10;
    		sum += tmp;
    		num = num / 10;
    	}
    	if(sum >= 10)
    		return crunch(sum);
    	return sum;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf("%d", c);
    If your compiler isn't complaining, turn up the warning level until it does.

    This should be
    scanf("%d", &c);

    Recommended compiler options for gcc are
    -W -Wall -ansi -pedantic -O2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > scanf("%d", c);

    Try:
    scanf("%d", &c);

    Now let me disappear before Dave_Sinkula tells me I'm wrong ...

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    I can't believe I forgot about that. Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  4. Simple window program
    By baniakjr in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2006, 03:46 PM
  5. Help with simple program
    By nik2007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2006, 09:54 AM