Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++: Add a button that calculates the factorial of the number and also add a

ID: 3780295 • Letter: I

Question

In C++:

Add a button that calculates the factorial of the number and also add a button calculates the power of a number?

#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>

using namespace std;
HINSTANCE hInst;

//declare global variables
//windows handles variables ( elements like buttons text boxes etc)
HWND textBox1;
// char variables
char numtext[10];
// double variables
double number1,number2,result;
//variable to know the operation
enum operation {SUM,MUL,DIV,SUB,NONE};
//string result
string to_string(double x);// function to transform to double to string
string resultstring;

operation SelOperation=NONE;

BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_INITDIALOG:
    {
    }
    return TRUE;

    case WM_CLOSE:
    {
        EndDialog(hwndDlg, 0);
    }
    return TRUE;

    case WM_COMMAND:
    {
        switch(LOWORD(wParam))
        {
            // code for each button
        //get handle for text box

        case 1:
          //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"1");
          // show thetext

        break;

        case 2:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"2");
          // show thetext
          SetWindowText(textBox1,numtext);
        break;

        case 3:
          //get handle
          //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"3");
          // show thetext
          SetWindowText(textBox1,numtext);
        break;

         case 4:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"4");
          // show thetext
          SetWindowText(textBox1,numtext);
        break;

         case 5:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"5");
          // show thetext
          SetWindowText(textBox1,numtext);
        break;

         case 6:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"6");
          // show thetext
          SetWindowText(textBox1,numtext);
        break;

         case 7:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"7");
          // show thetext
          SetWindowText(textBox1,numtext);
        break;

         case 8:
        //get the number in the textbox
          //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"8");
          // show thetext
          SetWindowText(textBox1,numtext);
        break;

         case 9:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"9");
          // show the text
          SetWindowText(textBox1,numtext);
        break;

         case 10:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"0");
          // show the text
          SetWindowText(textBox1,numtext);
        break;

        case 11:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,".");
          // show the text
          SetWindowText(textBox1,numtext);
        break;

        case 12:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          SetWindowText(textBox1,"0");
          cout<<"hello";

        break;

        case 100:
        //get the number in the textbox
        //get handle
          textBox1=GetDlgItem(hwndDlg,200);
          GetWindowText(textBox1,&numtext[0],10);
          number1=atof(numtext);
          //define operation
          SelOperation=SUM;
          //delete text box
          SetWindowText(textBox1,"");
        break;

        case 101:
        //get the number in the textbox
        //get handle
          textBox1=GetDlgItem(hwndDlg,200);
          GetWindowText(textBox1,&numtext[0],10);
          number1=atof(numtext);
          //define operation
          SelOperation=DIV;
          //delete text box
          SetWindowText(textBox1,"");
        break;

        case 102:
        //get the number in the textbox
        //get handle
          textBox1=GetDlgItem(hwndDlg,200);
          GetWindowText(textBox1,&numtext[0],10);
          number1=atof(numtext);
          //define operation
          SelOperation=MUL;
          //delete text box
          SetWindowText(textBox1,"");
        break;

        case 103:
        //get the number in the textbox
        //get handle
          textBox1=GetDlgItem(hwndDlg,200);
          GetWindowText(textBox1,&numtext[0],10);
          number2=atof(numtext);
          //cout<<number2+0;
        // Perform operation
         switch (SelOperation)
         {
             case SUM:
             result=number1+number2;
             resultstring = to_string(result);
             //transform double to char
             strcpy( numtext, resultstring.c_str() );
             SetWindowText(textBox1,numtext);
             break;
             case DIV:
             result=number1/number2;
             resultstring = to_string(result);
             //transform double to char
             strcpy( numtext, resultstring.c_str() );
             SetWindowText(textBox1,numtext);
             break;
             case MUL:
             result=number1*number2;
             resultstring = to_string(result);
             //transform double to char
             strcpy( numtext, resultstring.c_str() );
             SetWindowText(textBox1,numtext);
             break;

         }
        break;
        }
    }
    return TRUE;
    }
    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst=hInstance;
    InitCommonControls();
    return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}


// function
std::string to_string(double x)
{
std::ostringstream ss;
ss << x;
return ss.str();
}

-----------------------------------------------------------------------


#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"


//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
DLG_MAIN DIALOG 0, 0, 186, 212
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "Ms Shell Dlg"
{
    EDITTEXT        200, 17, 12, 148, 18, ES_RIGHT | ES_AUTOHSCROLL, WS_EX_LEFT
    GROUPBOX        "Numbers", 0, 14, 52, 105, 156, 0, WS_EX_LEFT
    PUSHBUTTON      "1", 1, 22, 136, 25, 20, 0, WS_EX_LEFT
    GROUPBOX        "OP.", 0, 129, 47, 51, 161, 0, WS_EX_LEFT
    PUSHBUTTON      "2", 2, 56, 136, 23, 20, 0, WS_EX_LEFT
    PUSHBUTTON      "3", 3, 87, 136, 24, 22, 0, WS_EX_LEFT
    PUSHBUTTON      "+", 100, 139, 60, 33, 32, 0, WS_EX_LEFT
    PUSHBUTTON      "/", 101, 140, 98, 31, 31, 0, WS_EX_LEFT
    PUSHBUTTON      "X", 102, 141, 135, 31, 30, 0, WS_EX_LEFT
    PUSHBUTTON      "=", 103, 141, 172, 31, 29, 0, WS_EX_LEFT
    PUSHBUTTON      "4", 4, 22, 105, 24, 21, 0, WS_EX_LEFT
    PUSHBUTTON      "5", 5, 56, 105, 23, 20, 0, WS_EX_LEFT
    PUSHBUTTON      "6", 6, 87, 105, 22, 19, 0, WS_EX_LEFT
    PUSHBUTTON      "7", 7, 22, 74, 23, 21, 0, WS_EX_LEFT
    PUSHBUTTON      "8", 8, 56, 74, 23, 21, 0, WS_EX_LEFT
    PUSHBUTTON      "9", 9, 87, 74, 22, 20, 0, WS_EX_LEFT
    PUSHBUTTON      "CE", 12, 23, 171, 24, 20, 0, WS_EX_LEFT
    PUSHBUTTON      "0", 10, 57, 171, 22, 20, 0, WS_EX_LEFT
    PUSHBUTTON      ".", 11, 88, 171, 23, 22, 0, WS_EX_LEFT
}

//
// Manifest resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
1                  RT_MANIFEST    ".\manifest.xml"

2. Download the Wo project. The program let the user to guess a 4 letter word each time that the use click a letter button the program will check if the letter is in the word and show the letter in a text box. Th word is stored in a char variable (constraint the size to a 4 letter word) 3. Code all the missing buttons 4. Show a counter of the number of mistakes. 5. Use a message box to show the user that he gassed the word correctly 6. Let the user to reset the game.

Explanation / Answer

#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>

using namespace std;
HINSTANCE hInst;

//declare global variables
//windows handles variables ( elements like buttons text boxes etc)
HWND textBox1;
// char variables
char numtext[10];
// double variables
double number1,number2,result;
//variable to know the operation
enum operation {SUM,MUL,DIV,SUB,NONE};
//string result
string to_string(double x);// function to transform to double to string
string resultstring;

operation SelOperation=NONE;

BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_INITDIALOG:
    {
    }
    return TRUE;

    case WM_CLOSE:
    {
        EndDialog(hwndDlg, 0);
    }
    return TRUE;

    case WM_COMMAND:
    {
        switch(LOWORD(wParam))
        {
            // code for each button
        //get handle for text box

        case 1:
          //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"1");
          // show thetext

        break;

        case 2:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"2");
          // show thetext
          SetWindowText(textBox1,numtext);
        break;

        case 3:
          //get handle
          //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"3");
          // show thetext
          SetWindowText(textBox1,numtext);
        break;

         case 4:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"4");
          // show thetext
          SetWindowText(textBox1,numtext);
        break;

         case 5:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"5");
          // show thetext
          SetWindowText(textBox1,numtext);
        break;

         case 6:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"6");
          // show thetext
          SetWindowText(textBox1,numtext);
        break;

         case 7:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"7");
          // show thetext
          SetWindowText(textBox1,numtext);
        break;

         case 8:
        //get the number in the textbox
          //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"8");
          // show thetext
          SetWindowText(textBox1,numtext);
        break;

         case 9:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"9");
          // show the text
          SetWindowText(textBox1,numtext);
        break;

         case 10:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,"0");
          // show the text
          SetWindowText(textBox1,numtext);
        break;

        case 11:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          //get the text from the box
          GetWindowText(textBox1,&numtext[0],10);
          //add the number to the text
          strcat(numtext,".");
          // show the text
          SetWindowText(textBox1,numtext);
        break;

        case 12:
        //get the number in the textbox
          textBox1=GetDlgItem(hwndDlg,200);
          SetWindowText(textBox1,"0");
          cout<<"hello";

        break;

        case 100:
        //get the number in the textbox
        //get handle
          textBox1=GetDlgItem(hwndDlg,200);
          GetWindowText(textBox1,&numtext[0],10);
          number1=atof(numtext);
          //define operation
          SelOperation=SUM;
          //delete text box
          SetWindowText(textBox1,"");
        break;

        case 101:
        //get the number in the textbox
        //get handle
          textBox1=GetDlgItem(hwndDlg,200);
          GetWindowText(textBox1,&numtext[0],10);
          number1=atof(numtext);
          //define operation
          SelOperation=DIV;
          //delete text box
          SetWindowText(textBox1,"");
        break;

        case 102:
        //get the number in the textbox
        //get handle
          textBox1=GetDlgItem(hwndDlg,200);
          GetWindowText(textBox1,&numtext[0],10);
          number1=atof(numtext);
          //define operation
          SelOperation=MUL;
          //delete text box
          SetWindowText(textBox1,"");
        break;

        case 103:
        //get the number in the textbox
        //get handle
          textBox1=GetDlgItem(hwndDlg,200);
          GetWindowText(textBox1,&numtext[0],10);
          number2=atof(numtext);
          //cout<<number2+0;
        // Perform operation
         switch (SelOperation)
         {
             case SUM:
             result=number1+number2;
             resultstring = to_string(result);
             //transform double to char
             strcpy( numtext, resultstring.c_str() );
             SetWindowText(textBox1,numtext);
             break;
             case DIV:
             result=number1/number2;
             resultstring = to_string(result);
             //transform double to char
             strcpy( numtext, resultstring.c_str() );
             SetWindowText(textBox1,numtext);
             break;
             case MUL:
             result=number1*number2;
             resultstring = to_string(result);
             //transform double to char
             strcpy( numtext, resultstring.c_str() );
             SetWindowText(textBox1,numtext);
             break;

         }
        break;
        }
    }
    return TRUE;
    }
    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst=hInstance;
    InitCommonControls();
    return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}


// function
std::string to_string(double x)
{
std::ostringstream ss;
ss << x;
return ss.str();
}

-----------------------------------------------------------------------


#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"


//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
DLG_MAIN DIALOG 0, 0, 186, 212
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "Ms Shell Dlg"
{
    EDITTEXT        200, 17, 12, 148, 18, ES_RIGHT | ES_AUTOHSCROLL, WS_EX_LEFT
    GROUPBOX        "Numbers", 0, 14, 52, 105, 156, 0, WS_EX_LEFT
    PUSHBUTTON      "1", 1, 22, 136, 25, 20, 0, WS_EX_LEFT
    GROUPBOX        "OP.", 0, 129, 47, 51, 161, 0, WS_EX_LEFT
    PUSHBUTTON      "2", 2, 56, 136, 23, 20, 0, WS_EX_LEFT
    PUSHBUTTON      "3", 3, 87, 136, 24, 22, 0, WS_EX_LEFT
    PUSHBUTTON      "+", 100, 139, 60, 33, 32, 0, WS_EX_LEFT
    PUSHBUTTON      "/", 101, 140, 98, 31, 31, 0, WS_EX_LEFT
    PUSHBUTTON      "X", 102, 141, 135, 31, 30, 0, WS_EX_LEFT
    PUSHBUTTON      "=", 103, 141, 172, 31, 29, 0, WS_EX_LEFT
    PUSHBUTTON      "4", 4, 22, 105, 24, 21, 0, WS_EX_LEFT
    PUSHBUTTON      "5", 5, 56, 105, 23, 20, 0, WS_EX_LEFT
    PUSHBUTTON      "6", 6, 87, 105, 22, 19, 0, WS_EX_LEFT
    PUSHBUTTON      "7", 7, 22, 74, 23, 21, 0, WS_EX_LEFT
    PUSHBUTTON      "8", 8, 56, 74, 23, 21, 0, WS_EX_LEFT
    PUSHBUTTON      "9", 9, 87, 74, 22, 20, 0, WS_EX_LEFT
    PUSHBUTTON      "CE", 12, 23, 171, 24, 20, 0, WS_EX_LEFT
    PUSHBUTTON      "0", 10, 57, 171, 22, 20, 0, WS_EX_LEFT
    PUSHBUTTON      ".", 11, 88, 171, 23, 22, 0, WS_EX_LEFT
}

//
// Manifest resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
1                  RT_MANIFEST    ".\manifest.xml"