I need help

I have a very simple program that take two inputs from the user and depending on what the input is there is a response. Now it works well in the condole but I was wondering if it was possible to make a nice windows application of it with too much knowledge of it. I want to have a nice recorded interface for this program. thanks
Here is the code for reference:

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{

string food, beverage;
char choice;
top:
cout << "Favorite Food: ";
cin >> food;
cout << "Favorite Drink: ";
cin >> beverage;
if (food == "pizza", beverage == "coke")
{
cout << "Your Result: GOOD STUFF!!" << endl;
}
else if (food != "pizza", beverage != "coke")
{
cout << "Your Result: You SUCK!!!" << endl;
}
else if (food == "pizza", beverage != "coke")
{
cout << "Your Result: That's always good...I guess." << endl;
}
else if (food != "pizza", beverage == "coke")
{
cout << "Your Result: Whatever, Pizza is where it's at!" << endl;
}
goto top;
return 0;

}
closed account (zwA4jE8b)
basically you just need to learn how to create and register a window and write a message loop and callback function.

This is my main window. I give this to you because I got it from directxtutorial.com.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <Windows.h>

LRESULT CALLBACK MessageProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
	WNDCLASSEXA wc;

	ZeroMemory(&wc, sizeof(WNDCLASSEX));

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = MessageProc;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1)); 
	wc.lpszClassName = "SNAKE";

	RegisterClassExA(&wc);

	HWND hWnd = CreateWindowExA(NULL,
                          "SNAKE", "Snake",
						  WS_POPUP,
                          45, 45,
                          screen_width, screen_height,
                          NULL, NULL,
                          hInstance,
                          NULL);

	ShowWindow(hWnd, nCmdShow);
	HDC hDC = GetDC(hWnd);

	MSG msg;

	while(TRUE)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				break;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{

		}
	}
	return msg.wParam;
}

LRESULT CALLBACK MessageProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		} break;
		case WM_KEYDOWN:
		{
			switch (wParam)
			{
			
			}
			return 0;
		} break;
     }
     return DefWindowProcA(hWnd, message, wParam, lParam);
 }
closed account (zwA4jE8b)
from here you handle messages, in your case user keyboard input.

TextOut() is a simple windows function for displaying text.

if you want text boxes and list boxes, I cannot help you there.
Last edited on
No when i run ur code the debugger says:
1>------ Build started: Project: trial1, Configuration: Debug Win32 ------
1> trial1.cpp
1>c:\users\user\documents\visual studio 2010\projects\trial1\trial1\trial1.cpp(20): error C2065: 'IDI_ICON1' : undeclared identifier
1>c:\users\user\documents\visual studio 2010\projects\trial1\trial1\trial1.cpp(29): error C2065: 'screen_width' : undeclared identifier
1>c:\users\user\documents\visual studio 2010\projects\trial1\trial1\trial1.cpp(29): error C2065: 'screen_height' : undeclared identifier
1>c:\users\user\documents\visual studio 2010\projects\trial1\trial1\trial1.cpp(70): warning C4060: switch statement contains no 'case' or 'default' labels
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

also where would my code go nto ur code. I jst want a simple code to replace the console withe a windows window and be able to execute the program in an orderly fashion
closed account (zwA4jE8b)
yes you need to remove line 19 because that is my icon. and like your error message says, there are no case or default labels in the switch, so on line 68 just add a default: break;

but as you will learn, if you use this template, in that switch case is where you will handle keyboard input.
still giving me the same error:
1>------ Build started: Project: trial1, Configuration: Debug Win32 ------
1> trial1.cpp
1>c:\users\user\documents\visual studio 2010\projects\trial1\trial1\trial1.cpp(28): error C2065: 'screen_width' : undeclared identifier
1>c:\users\user\documents\visual studio 2010\projects\trial1\trial1\trial1.cpp(28): error C2065: 'screen_height' : undeclared identifier
1>c:\users\user\documents\visual studio 2010\projects\trial1\trial1\trial1.cpp(70): warning C4065: switch statement contains 'default' but no 'case' labels
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
closed account (zwA4jE8b)
just change screen_width to 800 and screen_height to 600 or whatever resolution you want.
or define them before you call main.
Last edited on
Sorry if i am a noob but when i say screen_width = 800 screen_width is still a undeclared identifier. can a get a noob guide to fixing this
#define screen_width 800 or const int screen_width = 800;

Just like any other identifier, it needs to be declared as a specific type, or #defined.
closed account (zb0S216C)
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// #include <windows.h> // If you compiler doesn't like the next one.
#include <Windows.h>

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

int WINAPI WinMain( HINSTANCE CurrInst, HINSTANCE PrevInst, LPSTR CmdLine, int CmdArgs )
{
    WNDCLASSEX ClassEx;
    ::ZeroMemory( &ClassEx, sizeof( WNDCLASSEX ) );

    ClassEx.cbSize = sizeof( WNDCLASSEX );
    ClassEx.lpfnWndProc = &( *WindowProcedure );
    ClassEx.hInstance = &( *CurrInst );
    ClassEx.hIcon = ::LoadIcon( nullptr, IDI_APPLICATION );
    ClassEx.hCursor = ::LoadCursor( nullptr, IDC_ARROW );
    ClassEx.hbrBackground = ::CreateSolidBrush( RGB( 200, 200, 200 ) );
    ClassEx.lpszClassName = TEXT( "DEFAULT_CLASS" );

    ::RegisterClassEx( &ClassEx );

    HWND WindowHandle( nullptr );
    WindowHandle = ::CreateWindowEx( 0, TEXT( "DEFAULT_CLASS" ), TEXT( "Window Name" ),
                                     WS_OVERLAPPEDWINDOW, 10, 10, 500, 400, &( *::GetDesktopWindow( ) ),
                                     nullptr, &( *CurrInst ), nullptr );

    if( WindowHandle == nullptr )
    {
        ::MessageBox( &( *GetDesktopWindow( ) ), TEXT( "Failed to create the window." ), TEXT( "Failure" ),
                      ( MB_OK | MB_ICONERROR | WS_POPUP ) );

        return 1;
    }

    ::ShowWindow( &( *WindowHandle ), SW_SHOW );

    MSG Messages;
    ::ZeroMemory( &Messages, sizeof( MSG ) );

    while( GetMessage( &Messages, nullptr, 0, 0 ) != WM_QUIT )
    {
        TranslateMessage( &Messages );
        DispatchMessage( &Messages );
    }

    return 0;
}

LRESULT CALLBACK WindowProcedure( HWND Handle, UINT Msg, WPARAM WParam, LPARAM LParam )
{
    switch( Msg )
    {
        case WM_QUIT:
            PostQuitMessage( 0 );
            return 0;
 
        default:
            break;
    }

    return DefWindowProc( &( *Handle ), Msg, WParam, LParam );
}

Wazzak
Last edited on
ok framework i tried ur code and the debugger replied:
1>------ Build started: Project: trial1, Configuration: Debug Win32 ------
1> trial1.cpp
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>c:\users\user\documents\visual studio 2010\Projects\trial1\Debug\trial1.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Also Framework where am i supposed to put my code to make this be usable with my program?
Last edited on
closed account (zwA4jE8b)
you cannot use your current code, you will need to use windows functions and objects
ok so should i continue working on the condole applications or should i move on to the windows application? also wats a good book and/or tutorial to start with for windows applications, from the beggining?
closed account (zwA4jE8b)
http://www.winprog.org/tutorial/

this is pretty good.
so will windows program be able to do similar things, for example my code?
closed account (zb0S216C)
Yes, but in a more graphical way. For example, text boxes (input), message boxes (can also be used with consoles), images, radio buttons, check boxes, etc. Windows applications are the next step up from consoles.

Wazzak
Ok to learn the windows application part of c++ i have picked out 2 books: Windows via C/C++ (Pro - Developer) by Jeffrey M. Richter and Microsoft Visual C++ Windows Applications by Example by Stefan Björnander. Are these any good or can you recommend something better?
Topic archived. No new replies allowed.