Thread: Homework Struggles

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    76

    Homework Struggles

    I am writing a program that will act as a simple calculator when it is complete. The instructions state to use one function that returns 2 output parameters and another to do the calculations. I am stuck trying to get my accumulator to keep the result. This version runs but the last input is always the output that is printed. Any hints?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    //Function prototypes
    void scan_data (char *opp, double *nump);
    void do_next_op (char op, double num, double *accup);
    
    int main (void)
    
    {
    	//declare variables, not sure if the accumulator ACCU needs to be declared here
    	double num, accu;
    	char op;
    
    	//Start do while loop to accept input from user until Q or q is chosen
    	do
    	{
    
    	//Call functions until to get input and perform calculations
    	scan_data(&op, &num);
    	do_next_op (op, num, &accu);
    	}
    	while (op != 'q' && op != 'Q');
    
    	//Print end result when Do While is exited
    	printf ("The end result is %lf.\n", &accu);
    }
    
    
    //Get data function
    void scan_data (char *opp, double *nump)
    
    {
    	double num;
    	char op;
    	printf ("Please enter an operator and number: ");
    	scanf(" %c%lf", &op, &num);
    	while (op != '+' && op != '-' && op != '*' && op != '/' && op != '^' && op != 'q' && op != 'Q')
    		{
    		fflush (stdin);
    		printf ("Please enter an operator and Number: ");
    		scanf(" %c %lf", &op, &num);
    		}
    
    		*opp = op;
    		*nump = num;
    }
    
    //Here is where I think the problem lies, I reset the accumulator to 0 each time I enter the function, not sure how to fix it.
    //I'm also not sure how to use the ACCU and *ACCUP as an input and output variable.
    void do_next_op (char op, double num, double *accup)
    {
    
    	double accu = 0;
    
    	if (op == '+')
    	{
    		*accup = accu + num;
    		printf ("The result so far is %lf\n", *accup);
    	}
    	else if (op == '-')
    	{
    		*accup = accu - num;
    	}
    
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Just some suggestions...

    Try using more constants / defines for the different symbols and important input characters. Also, make your functions return 1 or 0 and check them with an if statement in your main to handle errors.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    A compiler which warns you when you mess up printf/scanf is a good idea.
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:27: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘double *’
    bar.c:28: warning: control reaches end of non-void function
    Also, see the FAQ on why fflush(stdin) is a bad idea.
    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.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    In main you have to initialize accu = 0 when you define it.
    In do_next_op() all you need to do is *accup = *accup + num. No need for variable 'accu'

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    76
    Quote Originally Posted by nonoob View Post
    In main you have to initialize accu = 0 when you define it.
    In do_next_op() all you need to do is *accup = *accup + num. No need for variable 'accu'
    Thanks a lot, that did it. I was already playing with *accup = *accup + num, the initialization fixed it.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) Return 0 at the end of main

    2) Test often and compile after every single instruction.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help With Struggles
    By dolfaniss in forum C Programming
    Replies: 4
    Last Post: 04-25-2011, 07:55 AM
  2. Do my homework
    By heiroglikano in forum C Programming
    Replies: 3
    Last Post: 05-31-2009, 06:26 AM
  3. Homework Help!!!!!
    By bcianfrocca in forum C++ Programming
    Replies: 20
    Last Post: 09-12-2004, 07:44 PM
  4. homework help
    By computerjunkie5 in forum C++ Programming
    Replies: 13
    Last Post: 10-27-2003, 11:54 AM