Thread: I need help modifying my calculator!!

  1. #1
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235

    Question I need help modifying my calculator!!

    Hey guys, ok i got a kinda decent working calculator id say, i know can compete with some out there, but its a start. Ok it works and everything, but id like to modify it this way. I would for main to be a seperate entity whereby it waits for the user to enter input, basically sits and waits for input from keyboard, and then i would like to have a seperate function to do the calculations, some like "float calc (float num1,float num2, char operator); /**calculates result of num1 and num2. So can anyone help me split the calculation part into a seperate function. My coding is below, i know it aint all that neat either, thanks!!

    Code:
    #include<stdio.h>
    
    int  main(void)
    {
    float numone,numtwo,ans;
    char operation;
    int loop=1;
    
    system ("Cls");
    printf("\n Welcome to calculator in C");
    printf("\n These are the following supported operations");
    printf("\n + -> addition \n - -> subtraction");
    printf("\n * -> multiplication \n / -> division");
    
    printf("\n\n\t\t Welcome to calculator in C Programming");
    printf("\n\t\t\t Press 'Q' to EXIT Program");
    printf("\n\t\t\t Matus' Calculator\n\n");
        
    while(loop){
    printf("\n\n Press any key to continue....");
    getch();
    system ("Cls");
    printf("\t\t Welcome to calculator in C Programming");
    printf("\n\t\t\t Press 'Q' to EXIT Program");
    printf("\n\t\t\t Matus' Calculator");
    
    printf("\n\n\n Enter the operation: ");
    operation=getch();
    
    switch(operation){
    
    case '+': printf("\n Enter two numbers to add, ");
    printf("\n\t\tnum 1: ");
    	
    scanf("%f",&numone);
    printf("\t\tnum 2: ");
    scanf("%f",&numtwo);
    
    ans=numone+numtwo;
    
    printf("\n\n %f + %f = %f",numone,numtwo,ans);
    break;
    
    case '-': printf("\n Enter two numbers to subtract, ");
    printf("\n\t\tnum 1: ");
    scanf("%f",&numone);
    
    printf("\t\tnum 2: ");
    scanf("%f",&numtwo);
    
    ans=numone-numtwo;
    
    printf("\n\n %f - %f = %f",numone,numtwo,ans);
    break;
    
    case '*': printf("\n Enter two numbers to multiply, ");
    printf("\n\t\tnum 1: ");
    scanf("%f",&numone);
    
    printf("\t\tnum 2: ");
    scanf("%f",&numtwo);
    
    ans=numone*numtwo;
    
    printf("\n\n %f * %f = %f",numone,numtwo,ans);
    break;
    
    case '/': printf("\n Enter two numbers to divide, ");
    printf("\n\t\tnum 1: ");
    scanf("%f",&numone);
    
    printf("\t\tnum 2: ");
    scanf("%f",&numtwo);
    
    ans=numone/numtwo;
    
    printf("\n\n %f / %f = %f",numone,numtwo,ans);
    break;
    
    case 'q': exit(0); //Exits Program Cleanly
    break;
    
    default: system("Cls");
    printf("\n\n Unoperatable keys in program !!");
    printf("\n\n Strike any key....");
    getch();
    loop=1;
    
           } //End switch
                 }//End While
                        }//End Main

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, why don't you go along and fix your indentation, and then produce a function that calculates one operation.

    I'm not sure what you want us to help you with, other than "encouragement"?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Is the problem here that you don't know how to write functions? That's my best guess. You really should state your problems, or ask a question.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    I would recommend:

    1. A separate function to set up your menu items
    2. Separate function for each mathematical operation. No need to pass the operand just stick with your switch statement to call the appropriate function.

    3. Declare your functions outside of main

    example:

    addition(int a, int b);

  5. #5
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by citizen View Post
    Is the problem here that you don't know how to write functions? That's my best guess. You really should state your problems, or ask a question.
    Ok im not that experienced with C, but yes i know the logic behind functions, however i can get the main to pass the variables to the function, however the calculations are wrong, must be something written wrong, like i said im pretty new to C, so i was asking for help.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Post what you've got so far and we'll be more than happy to point out any flaws

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. c++ Reverse Polish Calculator Help
    By knight101 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2001, 09:31 AM