Me again!

This is a discussion on Me again! within the C Programming forums, part of the General Programming Boards category; Hey guys, me again, with another problem. I've gotten past the user inputting and outputting data, but i need help ...

  1. #1
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168

    Me again!

    Hey guys, me again, with another problem.
    I've gotten past the user inputting and outputting data, but i need help on getting the computer to output decimals, this is the code i have at the minute:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    float num, num2, num3;
    int num4, num5;
    
    void Enter()
    {
    	printf ("\nEnter the first number: \n");
    	scanf ("%d", &num);
    
    	printf ("Enter the second number: \n");
    	scanf ("%d", &num2);
    }
    
    void multi (float num, float num2)
    {
    	num3 = num * num2;
    }
    
    void divide (float num, float num2)
    {
    	num3 = num/num2;
    }
    
    void add (float num, float num2)
    {
    	num3 = num + num2;
    }
    
    void subtract (float num, float num2)
    {
    	num3 = num - num2;
    }
    
    void Mathsprog()
    {
    
    	Enter();
    	printf ("\n\n 1. Add them \n 2. Subtract the second number from the first \n 3. Multiply them \n 4. Divide the first number by the second \n\n What would you like to do with these numbers? ");
    	scanf ("%d\n", &num4);
    	
    	if (num4 == 1)
    	{
    		add (num, num2);
    	}
    	
    	if (num4 == 2)
    	{
    		subtract (num, num2);
    	}
    
    	if (num4 == 3)
    	{
    		multi (num, num2);
    	}
    
    	if (num4 == 4)
    	{
    		divide (num, num2);
    	}
    
    	printf ("The answer is: %d\n\n", num3);
    
    }
    
    
    int main()
    {
    	printf ("My Maths program, a work in progress\n\n\n");
    	num5 = 1;
    
    	do {
    		Mathsprog();
    		printf ("What would you like to do now? (press 1 to start again or 2 to quit): ");
    		scanf ("%d", &num5);
    
    	   } while (num5 != 2);
    
    return (1);
    }
    I'd like the output num3 to be able to output a decimal because at the minute if i divide any numbers that should be decimal, come out as 0. I thought the problem was that the data type was int, so i tryed float, but now it keeps hanging on me, any ideas?

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Why is that all your functions are declared with a void type
    When no one helps you out. Call google();

  3. #3
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    All the tutorials i've read tell/show you to do it like that, i take it it's wrong? How else can you do it?

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Functions should return a result, atleast the functions you are using should, yet they dont return a result.

    edit: read this tutorial on functions http://www.cprogramming.com/tutorial/lesson4.html
    When no one helps you out. Call google();

  5. #5
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Maybe he's suggesting that you should localize your global variables. Personally, I think your overusing functions. I really don't think making new functions for Enter() and Mathsprog() are neccesary. My final comment, style wize, is that you should name your variables something more useful then num1, num2, etc.

  6. #6
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    but they do return a result, using the variables num and num2, compile and run the program to see for yourself

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Code:
    void add (float num, float num2)
    {
    	num3 = num + num2;
    }
    that function does nothing. A better way to do what you want to do there is as follows
    Code:
    int integer_add( int x, int y)
    {
    	int result;
    	result = x +y;
    	return result;
    }
    When no one helps you out. Call google();

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    How about something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int add     (int num1, int num2) { return num1 + num2; }
    int subtract(int num1, int num2) { return num1 - num2; }
    int multiply(int num1, int num2) { return num1 * num2; }
    int divide  (int num1, int num2) { return num1 / num2; }
    
    int main(void)
    {
      char buf[50];
      int choice;
      int num1, num2;
      int ((*funcs[4])(int, int)) = { add, subtract, multiply, divide };
    
      do
      {
        puts("What would you like to do?");
        puts("\n1. Add\n2. Subtract\n3. Multipy\n4. Divide\n5. Quit");
        printf("\nChoice: ");
        fflush(stdout);
        fgets(buf, sizeof(buf), stdin);
        choice = atoi(buf);
      } while(choice < 1 || choice > 5);
    
      if(choice == 5)
        exit(EXIT_SUCCESS);
    
      printf("First number: ");
      fflush(stdout);
      fgets(buf, sizeof(buf), stdin);
      num1 = atoi(buf);
    
      printf("Second number: ");
      fflush(stdout);
      fgets(buf, sizeof(buf), stdin);
      num2 = atoi(buf);
    
      printf("Result: %d\n", funcs[choice - 1](num1, num2));
    
      return EXIT_SUCCESS;
    }
    Code:
    What would you like to do?
    
    1. Add
    2. Subtract
    3. Multipy
    4. Divide
    5. Quit
    
    Choice: 3
    First number: 7
    Second number: 3
    Result: 21
    Me likes function pointers =)
    Last edited by itsme86; 03-16-2005 at 03:48 PM.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    MadCow257: It looks like im overusing functions because im just learning how to use them, so im using them for anything that they may be useful in, plus i intend to add to the program, so they will prove themselves later on.

    InvariantLoop: Cheers for your help

    Anyway, could we get back to my original question on producing outputs with decimals? sorry to nag but i've been pulling my hair out over this...

  10. #10
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    wow thanks itsme86, even though i dont know what half of those functions do....

    *rushes off to look them up*

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    The problem is that you're using %d to print a float instead of %f.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Here you go, a slightly fixed up version (with working decimals)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int multi (int num1, int num2);
    float divide (int num1, int num2);
    int add (int num1, int num2);
    int subtract (int num1, int num2);
    
    int main()
    {
    	int quit = 1, selection, num1, num2;
    	printf ("My Maths program, a work in progress\n\n\n");
    	while (quit != 2)
    	{
    		printf ("\nEnter the first number: \n");
    		scanf ("%d", &num1);
    		printf ("Enter the second number: \n");
    		scanf ("%d", &num2);
    		printf ("\n\n 1. Add them \n 2. Subtract the second number from the first \n 3. Multiply them \n 4. Divide the first number by the second \n\n What would you like to do with these numbers? ");
    		scanf ("%d\n", &selection);
    		switch (selection)
    		{
    		case 1:
    			printf("The answer is: %d\n\n", add (num1, num2));
    			break;
    		case 2:
    			printf("The answer is: %d\n\n", subtract (num1, num2));
    			break;
    		case 3:
    			printf("The answer is: %d\n\n", multi (num1, num2));
    			break;
    		case 4:
    			printf("The answer is: %f\n\n", divide (num1, num2));
    			break;
    		}
    		printf ("What would you like to do now? [1 = repeat, 2 = quit]\n");
    		scanf ("%d", &quit);
    	}
    	return 1;
    }
    
    int multi (int num1, int num2)
    {
    	return num1*num2;
    }
    
    float divide (int num1, int num2)
    {
    	return num1/num2;
    }
    
    int add (int num1, int num2)
    {
    	return num1+num2;
    }
    
    int subtract (int num1, int num2)
    {
    	return num1-num2;
    }

  13. #13
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    ah! and i thought it was the data type, thanks

Popular pages Recent additions subscribe to a feed

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