You are here:

C++/calculator

Advertisement


Question
Hi, I am writing a simple calculator porgram.  I can not find out why when I click the "1" button it doesnt do anything.  Here is my source code.

#include <windows.h>
#include <string.h>
#include <iostream>
using namespace std;
char total[MAX_PATH] = "0";
bool multiply = false;
bool divide = false;
bool subtract = false;
bool add = false;
int number = 0;
#define IDC_ONE       100
#define IDC_two       101
#define IDC_three     102
#define IDC_four      103
#define IDC_five      104
#define IDC_six       105
#define IDC_seven     106
#define IDC_eight     107
#define IDC_nine      108
#define IDC_zero      109
#define IDC_clear     110
#define IDC_dot       111
#define IDC_multiply  112
#define IDC_divide    113
#define IDC_add       114
#define IDC_subtract  115
#define IDC_equal     116
#define IDC_text      117

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

static char gszClassName[]  = "Class";
static HINSTANCE ghInstance = NULL;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
      WNDCLASSEX WndClass;
      HWND hwnd;
      MSG Msg;

      ghInstance = hInstance;

      WndClass.cbSize        = sizeof(WNDCLASSEX);
      WndClass.style         = 0;
      WndClass.lpfnWndProc   = WndProc;
      WndClass.cbClsExtra    = 0;
      WndClass.cbWndExtra    = 0;
      WndClass.hInstance     = ghInstance;
      WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
      WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
      WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
      WndClass.lpszMenuName  = NULL;
      WndClass.lpszClassName = gszClassName;
      WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

      if(!RegisterClassEx(&WndClass)) {
         MessageBox(0, "Error Registering Window!", "Error!", MB_ICONSTOP | MB_OK);
         return 0;
      }

      hwnd = CreateWindowEx(
         WS_EX_STATICEDGE,
         gszClassName,
         "Calculator by Tom",
         WS_OVERLAPPEDWINDOW,
         CW_USEDEFAULT, CW_USEDEFAULT,
         320, 240,
         NULL, NULL,
         ghInstance,
         NULL);

      if(hwnd == NULL) {
         MessageBox(0, "Window Creation Failed!", "Error!", MB_ICONSTOP | MB_OK);
         return 0;
      }

      ShowWindow(hwnd, nCmdShow);
      UpdateWindow(hwnd);

      while(GetMessage(&Msg, NULL, 0, 0)) {
         TranslateMessage(&Msg);
         DispatchMessage(&Msg);
      }
      return Msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
      //HWND hButton, hCombo, hEdit, hList, hScroll, hStatic;
      HWND hButtona, hButtonb, hButtonc, hButtond, hButtone, hButtonf, hButtong, hButtonh, hButtoni, hButtonj;//0-9
      HWND hButtonk, hButtonl, hButtonm, hButtonn, hButtono;//*,/,-,+,.
      HWND hText, hClear, hEquals;

      switch(Message) {
         case WM_CREATE:
         hButtona = CreateWindowEx(
         0,
         "Button",
         "1",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         1, 1,//x,y
         50, 30,
         hwnd, (HMENU)IDC_ONE,
         ghInstance,
         NULL);
         hButtonb = CreateWindowEx(
         0,
         "Button",
         "2",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         51, 1,
         50, 30,
         hwnd, (HMENU)IDC_two,
         ghInstance,
         NULL);
         hButtonc = CreateWindowEx(
         0,
         "Button",
         "3",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         101, 1,
         50, 30,
         hwnd, (HMENU)IDC_three,
         ghInstance,
         NULL);
         hButtond = CreateWindowEx(
         0,
         "Button",
         "4",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         1, 31,
         50, 30,
         hwnd, (HMENU)IDC_four,
         ghInstance,
         NULL);
         hButtone = CreateWindowEx(
         0,
         "Button",
         "5",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         51, 31,
         50, 30,
         hwnd, (HMENU)IDC_five,
         ghInstance,
         NULL);
         hButtonf = CreateWindowEx(
         0,
         "Button",
         "6",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         101, 31,
         50, 30,
         hwnd, (HMENU)IDC_six,
         ghInstance,
         NULL);
         hButtong = CreateWindowEx(
         0,
         "Button",
         "7",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         1, 61,
         50, 30,
         hwnd, (HMENU)IDC_seven,
         ghInstance,
         NULL);
         hButtonh = CreateWindowEx(
         0,
         "Button",
         "8",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         51, 61,
         50, 30,
         hwnd, (HMENU)IDC_eight,
         ghInstance,
         NULL);
         hButtoni = CreateWindowEx(
         0,
         "Button",
         "9",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         101, 61,
         50, 30,
         hwnd, (HMENU)IDC_nine,
         ghInstance,
         NULL);  
         hButtonj = CreateWindowEx(
         0,
         "Button",
         "0",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         51, 91,
         50, 30,
         hwnd, (HMENU)IDC_zero,
         ghInstance,
         NULL);
         hButtonk = CreateWindowEx(
         0,
         "Button",
         "*",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         151, 1,
         50, 30,
         hwnd, (HMENU)IDC_multiply,
         ghInstance,
         NULL);
         hButtonl = CreateWindowEx(
         0,
         "Button",
         "/",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         151, 31,
         50, 30,
         hwnd, (HMENU)IDC_divide,
         ghInstance,
         NULL);
         hButtonm = CreateWindowEx(
         0,
         "Button",
         "-",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         151, 61,
         50, 30,
         hwnd, (HMENU)IDC_subtract,
         ghInstance,
         NULL);
         hButtonn = CreateWindowEx(
         0,
         "Button",
         "+",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         151, 91,
         50, 30,
         hwnd, (HMENU)IDC_add,
         ghInstance,
         NULL);
         hButtono = CreateWindowEx(
         0,
         "Button",
         ".",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         101, 91,
         50, 30,
         hwnd, (HMENU)IDC_dot,
         ghInstance,
         NULL);
         hEquals = CreateWindowEx(
         0,
         "Button",
         "=",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         151, 121,
         50, 30,
         hwnd, (HMENU)IDC_equal,
         ghInstance,
         NULL);
         hClear = CreateWindowEx(
         0,
         "Button",
         "Clear",
         WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
         1, 91,
         50, 30,
         hwnd, (HMENU)IDC_clear,
         ghInstance,
         NULL);
         hText = CreateWindowEx(
         0,
         "Static",
         total,
         WS_BORDER | WS_CHILD | WS_VISIBLE | SS_LEFT,
         1, 121,
         150, 30,
         hwnd, (HMENU)IDC_text,
         ghInstance,
         NULL);
         break;
         
         case WM_CLOSE:
         DestroyWindow(hwnd);
         break;
         case WM_DESTROY:
         PostQuitMessage(0);
         break;
         case IDC_ONE: {
         if (multiply) { number = number * 1; }
         if (divide) { number = number / 1; }
         if (add) { number = number + 1; }
         if (subtract) { number = number - 1; }
         if (!multiply && !divide && !add && !subtract) { number = 1; }
         strcpy (total,"1");
         itoa (number,total,10);
         WM_CREATE; }
         break;
         default:
         return DefWindowProc(hwnd, Message, wParam, lParam);
      }
      return 0;
}
Do you see why it's not working?  Thank you for your help.

Answer
Hi Tom,

Nice program - looks cool!

You need a WM_COMMAND handler:

      case WM_COMMAND :
         switch( wParam )
      {
         case IDC_ONE:
         break;
         case IDC_two:
         break;
         }
etc.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.