number guessing game

ok, ive been learning C++ on my own, and i now have a number guessing game compiled and everything. The only thing that im not sure of is the quality of the program and whether or not it is coded properly.

here is my code:
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123


#include <iostream>
#include <string>
#include <time.h>
#include <cstdlib>
#include <windows.h>
using namespace std;
void setcolor(unsigned short color)                 //The function that you'll use to
{                                                   //set the colour
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}

int main()

{ 


	int a,b,c,d,e,f,g;
	string str1, str2, str3;
	
	e = 1;
	while (e == 1)
	{
		g = 11;
		a = 0;
		f = 0;
	string str2 = "yes";
	string str3 = "no";
	
	for(c = 4; c <=1000; c++)
	{
		srand((unsigned)time(NULL));
		a = (rand() % 1000) +1;

	}

	
	d = 11;
	while (d > 0)
	{
	g = g-1;
	d = d-1;
	if (d == 10)
	{ setcolor (9);
		system("CLS");
		cout << "Number of tries left: " << g << endl << endl;
		cout << "Try to guess a number between 1 and 1000 in under 10 tries: ";
	cin >> b;
	}
	
	else if ( (d == 9) && (b == a))
	{ setcolor (4);
		system("CLS");
		cout << "Number of tries left: " << g << endl << endl;
		cout << "WOW!!! .__. You managed to get it on the first try!! That's AMAZING!!!!!!!" << endl;
	d = 0;
	}
	else if ( (d < 10) && (b < a))
	{ setcolor (5);
		system("CLS");
		cout << "Number of tries left: " << g << endl << endl;
		cout << "Your guess was too low, why don't you guess again?  "; 
		cin >> b;
	}
	else if ( (d < 10) && (b > a))
	{ setcolor (15);
		system("CLS");
		cout << "Number of tries left: " << g << endl << endl;
		cout << "Your guess was too high, why don't you guess again?  ";
		cin >> b;
	}
	else if ( (d < 10) && (b == a))
	{ setcolor (11);
		system("CLS");
		cout << "Number of tries left: " << g << endl << endl;
		cout << "CONGRATULATIONS!!! You finally got it! ^_^" << endl;
	d = 0;
	} 
	}
if ( (d == 0) && (b != a))
{ setcolor (12);
	system("CLS");
	cout << "The number was: " << a << endl << endl;
	cout <<  "Well, I'm sorry. You didn't manage to guess it.\nWas this your first time playing?";
}
else if ( (d == 0) && (b == a))
		{ setcolor (3);
			system("CLS");
			cout << "Number of tries left: " << g << endl << endl;
		cout << "CONGRATULATIONS!!! You finally got it! ^_^" << endl;
	d = 0;
	}

while (f != 1)
{ setcolor (13);
cout << endl << endl << "Would you like to play again? (yes/no) ";
cin >> str1;

if (str1.compare(str2) == 0)
{
	e = 1;
	f = 1;
}
else if (str1.compare(str3) == 0)
{
	e = 2;
	f = 1;
}
else 
{ setcolor (14);
	system("CLS");
	cout << "WOW! You really are stupid. -_- You can't even type yes or no....\nGo fall in a hole you waste of space!!\n";
}
	}
}
	return 0;
}



	


would anybody be able to comment on it and tell me what i could do to improve my overall programming and what i should do now to improve my knowledge of C++?

^_^ thanks in advance for your time and comments
a good game!
I just don't understand why you have to write a for loop to get a random number?
1
2
3
4
5
6
	for(c = 4; c <=1000; c++)
	{
		srand((unsigned)time(NULL));
		a = (rand() % 1000) +1;

	}
in the book that i got the number generator from it had that as part of the code, but now that i think about it, the example was generating multiple random numbers so that might be why, so that it gets a new number every time.

I just thought of it, and i doesnt seem to detract from the game, though would the game run different without it?

also, i have this code which came from visual C++ express 2008 for making a windows application and i was wondering how i would go about combining the two codes to make my game appear in a window instead of command prompt

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// GT_HelloWorldWin32.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <iostream>
#include <time.h>
using namespace std;

void setcolor(unsigned short color)                 //The function that you'll use to
{                                                   //set the colour
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application dows not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application
    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 100,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("Hello, how are you?");
	
    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
		setcolor (14);

        // Here your application is laid out.
        // For this introduction, we just print out "Hello, World!"
        // in the top left corner.
        TextOut(hdc,
            5, 5,
            greeting, _tcslen(greeting));
		

        // End application-specific layout section.

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

	return 0;
}


again, thank you for your time and help ^_^
Last edited on
Topic archived. No new replies allowed.