code help :)

This is a discussion on code help :) within the C Programming forums, part of the General Programming Boards category; I have made a calculator for my homework, but am having a few problems. Here is what I have in ...

  1. #1
    Unregistered
    Guest

    Talking code help :)

    I have made a calculator for my homework, but am having a few problems.

    Here is what I have in main.c:

    Code:
    /************************************************************
    Filename:	main.c
    Author:		Mike Hartwig(mike.hartwig@attbi.com)
    Input:		Keyboard
    Output:		Monitor
    *************************************************************/
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include "functions.h"
    
    int main()
    {
    	int menu_choice;
    	int data1 = 0;
    	int data2 = 0;
    	int answer = 0;
    	char sign = '_';
    	char pcsign = '%';
    	
    	while (menu_choice < 8)
    		{
    		printf("\n*************Calculator*************\n");
    		printf("Data 1:	    %d\n", data1);
    		printf("Data 2:     %d\n", data2);
    		printf("Operator:   %c\n", sign);
    		printf("Results:    %d\n", answer);
    		printf("************************************\n");
    		printf("1)Set Data 1 \n");
    		printf("2)Set Data 2 \n");
    		printf("3)Set Operator +\n");
    		printf("4)Set Operator -\n");
    		printf("5)Set Operator *\n");
    		printf("6)Set Operator /\n");
    		printf("7)Set Operator %c\n", pcsign);
    		printf("8)Set Operator pow\n");
    		printf("9)Set Operator exp\n");
    		printf("10)Set Operator sqrt\n");
    		printf("11)Set Operator sin\n");
    		printf("12)Quit");
    		printf("\n\nEnter Your Menu Choice: ");
    		scanf("%d", &menu_choice);
    
    	switch (menu_choice)
    			{
    			case 1:
    				{
    				printf("Enter Data 1: ");
    				scanf("%d", &data1);
    				break;
    				}
    			case 2:
    				{
    				printf("Enter Data 2: ");
    				scanf("%d", &data2);
    				break;
    				}
    			case 3:
    				{
    				answer = add(data1, data2);
    				sign = '+';
    				break;
    				}
    			case 4:
    				{
    				answer = sub(data1, data2);
    				sign = '-';
    				break;
    				}
    			case 5:
    				{
    				answer = mult(data1, data2);
    				sign = '*';
    				break;
    				}
    			case 6:
    				{
    				if (data1 == 0 || data2 == 0)
    					{
    					printf("\nCan't Divide By Zero\n");
    					}
    				else
    					{
    					answer = divide(data1, data2);
    					sign = '/';
    					}
    				break;
    				}
    			case 7:
    				{
    				if (data1 == 0 || data2 == 0)
    					{
    					printf("\nCan't Divide By Zero\n");
    					}
    				else
    					{
    					answer = pcent(data1, data2);
    					sign = '%';
    					}
    				break;
    				}
    			case 12:
    				{
    				return(0);
    				}
    
    			default:
    				{
    				printf("Invalid Menu Choice\n");
    				break;
    				}
    			}		
    		}
    return (0);
    }
    Here is what I have in functions.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "functions.h"
    /*****************************************************
    Function Name:	add()
    Purpose:		Add data1 and data2
    ******************************************************/
    int add(int data1, int data2)
    {
    	int answer;
    	printf("\nCalculating...\n");
    	answer = data1 + data2;
    return answer;
    }
    
    /*****************************************************
    Function Name:	sub()
    Purpose:		Subtracts data1 and data2
    ******************************************************/
    int sub(int data1, int data2)
    {
    	int answer;
    	printf("Calculating...\n");
    	answer = data1 - data2;
    return answer;
    }
    
    /*****************************************************
    Function Name:	mult()
    Purpose:		Multiplies data1 and data2
    ******************************************************/
    int mult(int data1, int data2)
    {
    	int answer;
    	printf("\nCalculating...\n");
    	answer = data1 * data2;
    return answer;
    }
    
    /*****************************************************
    Function Name:	div()
    Purpose:		Divides data1 and data2
    ******************************************************/
    int divide(int data1, int data2)
    {
    	int answer;
    	printf("\nCalculating...\n");
    	answer = data1 / data2;
    	
    return answer;
    }
    
    /*****************************************************
    Function Name:	pcent()
    Purpose:		Divides data1 and data2
    ******************************************************/
    int pcent(int data1, int data2)
    {
    	int answer;
    	printf("\nCalculating...\n");
    	answer = data1 % data2;
    return answer;
    }
    The default function is supposed to be addition, but i am not exactly sure of how to do this. Also when the user enters in a new value intop one of the data varialbes, it is supposed to update the answer, but it doesnt. It only updates the answer if you set the operator again.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:
    Code:
    int main()
    {
    	int menu_choice = 0;
    	int data1 = 0;
    	int data2 = 0;
    	int answer = 0;
    	char sign = '+';
    	char pcsign = '%';
    	
    	while (menu_choice < 8)
    		{
    		printf("\n*************Calculator*************\n");
    		printf("Data 1:	    %d\n", data1);
    		printf("Data 2:     %d\n", data2);
    		printf("Operator:   %c\n", sign);
    		printf("Results:    %d\n", answer);
    		printf("************************************\n");
    		printf("1)Set Data 1 \n");
    		printf("2)Set Data 2 \n");
    		printf("3)Set Operator +\n");
    		printf("4)Set Operator -\n");
    		printf("5)Set Operator *\n");
    		printf("6)Set Operator /\n");
    		printf("7)Set Operator %c\n", pcsign);
    		printf("8)Set Operator pow\n");
    		printf("9)Set Operator exp\n");
    		printf("10)Set Operator sqrt\n");
    		printf("11)Set Operator sin\n");
    		printf("12)Quit");
    		printf("\n\nEnter Your Menu Choice: ");
    		scanf("%d", &menu_choice);
    
    	switch (menu_choice)
    			{
    			case 1:
    				{
    				printf("Enter Data 1: ");
    				scanf("%d", &data1);
    				break;
    				}
    			case 2:
    				{
    				printf("Enter Data 2: ");
    				scanf("%d", &data2);
    				break;
    				}
    
    			case 3:
    				{
    				sign = '+';
    				break;
    				}
    			case 4:
    				{
    				sign = '-';
    				break;
    				}
    			case 5:
    				{
    				sign = '*';
    				break;
    				}
    			case 6:
    				{
    				sign = '/';
    				break;
    				}
    			case 7:
    				{
    				sign = '%';
    				break;
    				}
    			case 12:
    				{
    				return(0);
    				}
    
    			default:
    				{
    				printf("Invalid Menu Choice\n");
    				break;
    				}
    			}
    	switch (sign)
    			{
    			case '+':
    				{
    				answer = add(data1, data2);
    				break;
    				}
    			case '-':
    				{
    				answer = sub(data1, data2);
    				break;
    				}
    			case '*':
    				{
    				answer = mult(data1, data2);
    				break;
    				}
    			case '/':
    				{
    				if (data1 == 0 || data2 == 0)
    					{
    					printf("\nCan't Divide By Zero\n");
    					}
    				else
    					{
    					answer = divide(data1, data2);
    					}
    				break;
    				}
    			case '%':
    				{
    				if (data1 == 0 || data2 == 0)
    					{
    					printf("\nCan't Divide By Zero\n");
    					}
    				else
    					{
    					answer = pcent(data1, data2);
    					}
    				break;
    				}
    			}
    		}
    return (0);
    }

  3. #3
    Unregistered
    Guest

    Talking

    works great thanks m8. You are my saviour.

  4. #4
    Unregistered
    Guest
    i finally got around to coding the sqrt, exp and pow functions. The only thing wrong with that, is i catn have the sign as sqrt or anything, cuz the compiler thinks its a math function. And there arentand specifics operator symbols for those functions.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You probably need to change 'sign' to a character array, so you can store something like "sqrt" or "sin" for your display. If you do this, you will have to use strcpy() from <string.h> to assign to the variable 'sign': strcpy(sign,"+") or strcpy(sign,"sin").

    If you use a character array, you will not be able to use a switch() statement with 'sign', as I think switch() only works for one char. Either use if-then-else, or create a second variable. If you create a second variable, then choose one character to associate with each function: for example, for sin(), 's'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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