Thread: win32 calculater

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    11

    Smile win32 calculater

    Hi
    I have written my first, basic, meaninglyfull program. I feel i have done everything to it, that i can do in its current state. I would really wish to transfrom it into a win32 apllication with buttons for numbers and a screen for the calcultor for the letters to come up. Could someone help me (not write the program for me - i am here to learn) here is the code if it helps:

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    
    main ()
    
    {
    int a;
    int b;
    int c;
    int d;
    int e;
    int f;
    int g;
    int h;
    
    
    
    printf("What would you like to do?\n1=Add\n2=Subtract\n3=Multiply\n4=Divide");
    scanf("%d",&a);
    
     
    
    if(a==1) 
    {printf("What is the first number you would like to add?");
    scanf("%d",&b);
    printf("What is the second number you would like to add?");
    scanf("%d",&c);
    d=b+c ;
    printf("Your number is %d",d);
    scanf("%d");}
    
    
    if(a==2)
    {printf("What is the first number you would like to subtract?");
    scanf("%d",&e);
    printf("What is the second number you would like to subtract?");
    scanf("%d",&f);
    g=e-f;
    printf("Your number is %d",g);
    scanf("%d");}
    
    if(a==3)
    {printf("What is the first number you would like to multiply?");
    scanf("%d",&e);
    printf("What is the second number you would like to multiply?");
    scanf("%d",&f);
    g=e*f;
    printf("Your number is %d",g);
    scanf("%d");}
    
    if(a==4)
    {printf("What is the first number you would like to divide?");
    scanf("%d",&e);
    printf("What is the second number you would like to divide?");
    scanf("%d",&f);
    g=e/f;
    printf("Your number is %d\n",g);
    scanf("%d");}
    
    
    
    
    return 0;
    }
    Thanks in advance!

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    I feel i have done everything to it, that i can do in its current state.
    What about square root or exponents? In math.h there is a sqrt() function for getting the square root and you can use a loop to do exponents.

    Code:
    main()
    main() returns an integer.
    Code:
    int main(void)
    You should use floats or doubles to do division. Otherwise the user cant get decimals.
    Last edited by 00Sven; 07-04-2006 at 02:56 PM.
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Here's another way to write your program using functions:
    Code:
    #include <stdio.h>
    
    int func_add(int num1, int num2)      { return num1 + num2; }
    int func_subtract(int num1, int num2) { return num1 - num2; }
    int func_multiply(int num1, int num2) { return num1 * num2; }
    int func_divide(int num1, int num2)   { return num1 / num2; }
    
    int main(void)
    {
      struct
      {
        char *name;
        int (*func)(int, int);
      } list[] = { { "Add", func_add }, { "Subtract", func_subtract },
                   { "Multiply", func_multiply }, { "Divide", func_divide } };
      int num1, num2;
      int i;
      int choice;
    
      puts("What would you like to do?");
      for(i = 0;i < 4;++i)
        printf("%d=%s\n", i + 1, list[i].name);
    
      scanf("%d", &choice);
    
      if(choice >= 1 && choice <= 4)
      {
        choice--;
        printf("What is the first number you would like to %s? ",
          list[choice].name);
        fflush(stdout);
        scanf("%d", &num1);
        printf("What is the second number you would like to %s? ",
          list[choice].name);
        fflush(stdout);
        scanf("%d", &num2);
        printf("Your number is %d\n", list[choice].func(num1, num2));
      }
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    56
    First of all, instead of all the ifs, you can use a switch statement....

    Anyways..to your question. I've never done what you are trying to accomplish in C without using graphics functions which was harder for me. My first calculator I wrote in Visual Basic, which looked pretty close to your code, yet in VB you can just draw the buttons out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console program to Win32
    By Ducky in forum Windows Programming
    Replies: 3
    Last Post: 02-25-2008, 12:46 PM
  2. Win32 API or Win32 SDK?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-20-2005, 03:26 PM
  3. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 08:50 AM
  4. OLE Clipboard :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2002, 05:57 PM
  5. Win32 API Tutorials?
    By c++_n00b in forum C++ Programming
    Replies: 9
    Last Post: 05-09-2002, 03:51 PM