Ofstream not working??

Ok so im making a simple GUI program that tests the user, its crappy right now but only meant for debugging. Ok so here is all of my code:

Main.cpp:

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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "resource.h"
#include <fstream>

HINSTANCE hInst;

int right = 0;
int wrong = 0;


ofstream Score("Score.txt");

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
            /*
             * TODO: Add code to initialize the dialog.
             */
            return TRUE;

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

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDC_BTN_1:
                    right += 1;
                    return TRUE;

                case IDC_BTN_2:
                    wrong += 1;
                    return TRUE;

                case IDC_BTN_3:
                    right += 1;
                    return TRUE;

                case IDC_BTN_4:
                    wrong += 1;
                    return TRUE;

                case IDC_BTN_5:
                    right += 1;
                    return TRUE;

                case IDC_BTN_6:
                    wrong += 1;
                    return TRUE;

                case IDC_BTN_DONE:
                    MessageBox(hwndDlg, "Your score has been written to a text document labeled 'Score.txt'", "Information", MB_ICONINFORMATION);
                    Score << "Grand Theft Auto 4 Test Results" << std::endl;
                    Score << "=====================" << std::endl;
                    Score << "Number Right: " << right << std::endl;
                    Score << "Number Wrong: " << wrong << std::endl;
                    return TRUE;

                    //MessageBox(hwndDlg, "", "Information", MB_ICONINFORMATION);
            }
    }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}



Resource.rc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "resource.h"

DLG_MAIN DIALOGEX 6, 5, 220, 350

CAPTION "GTA 4 Test"

FONT 8, "Tahoma"

STYLE 0x10CE0804

BEGIN
    CTEXT "Grand Theft Auto 4 Test", IDC_STATIC, 0, 0, 80, 15
    CTEXT "Written by Chay Hawk", IDC_STATIC, -3, 10, 80, 15
    CTEXT "In GTA 4 was the main character's name Niko?", IDC_STATIC, 50, 46, 150, 15
    CTEXT "In the game, did you have to rob a bank?", IDC_STATIC, 43, 82, 150, 15
    CTEXT "In the game is there 200 flying rats?", IDC_STATIC, 49, 116, 150, 15
    CONTROL "1. Yes", IDC_BTN_1, "Button", 0x10010000, 0,  35, 46, 15
    CONTROL "1. No", IDC_BTN_2, "Button", 0x10010000, 0, 52, 46, 15
    CONTROL "2. Yes", IDC_BTN_3, "Button", 0x10010000, 0, 70, 46, 15
    CONTROL "2. No", IDC_BTN_4, "Button", 0x10010000, 0, 88, 46, 15
    CONTROL "3. Yes", IDC_BTN_5, "Button", 0x10010000, 0, 105, 46, 15
    CONTROL "3. No", IDC_BTN_6, "Button", 0x10010000, 0, 122, 46, 15
    CONTROL "Done", IDC_BTN_DONE, "Button", 0x10010000, 0, 150, 46, 15
END



Resource.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <windows.h>

// ID of Main Dialog
#define DLG_MAIN 101

// ID of Button Controls
#define IDC_STATIC 1000
#define IDC_BTN_1 1001
#define IDC_BTN_2 1002
#define IDC_BTN_3 1003
#define IDC_BTN_4 1004
#define IDC_BTN_5 1005
#define IDC_BTN_6 1006
#define IDC_BTN_DONE 1007 



I use code blocks btw. Here are my errors:

C:\Users\Chay Hawk\Desktop\GTA 4 Test\main.cpp|12|error: 'ofstream' does not name a type|
C:\Users\Chay Hawk\Desktop\GTA 4 Test\main.cpp||In function 'BOOL DialogProc(HWND__*, UINT, WPARAM, LPARAM)':|
C:\Users\Chay Hawk\Desktop\GTA 4 Test\main.cpp|57|error: 'Score' was not declared in this scope|
||=== Build finished: 2 errors, 0 warnings ===|


I dont understand what im doing wrong?
Last edited on
Try:
std::ofstream Score("Score.txt");

you could also add one of these lines if you like:
1
2
using namespace std; // or
using std::ofstream;


This will fix the first error by using the correct type for ofstream. This will fix the second error because "Score" was declared correctly.

Also, I recommend declaring ofstream inside the DialogeProc(). It's best to avoid global variables or objects.
Ah, crap i forgot i wasnt working with the std namespace :P thanks!
Topic archived. No new replies allowed.