Need help writing to a file.

So i'm working on a program to run along side a game called minecraft, with the stories people have made for the game the app will tell the story.

The Basic functionality works fine the devmode which allows people to customise features of the app which allows people to customise some of the names of stuff isn't working, specifically the writing the input'ed data to the required text file.

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
156
157
158
159
160
161
162
163
164
165
166
// To Do
// Fix functionality of replacing map and creator name
// Add function to change the password
// Add rule addign feature
// Add note making feature

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    int noteNumber, exit = 0, passcheck;
    passcheck = 0;
    
    char input[5], str[256], mapName[30], mapNameInput[30], creatorName[15], creatorNameInput[15];
    
    ifstream rules("rules.txt");
    ifstream note1("note 1.txt");
    ifstream note2("note 2.txt");
    ifstream map("map.txt");
    ifstream creator("creator.txt");
    
    
        
    while(exit == 0)
    {
        cout << "Beta v 2.0\n\nWelcome to ";
        
        map.clear();
        map.seekg(0);
        while(map.getline(mapName, 30))
            cout << mapName;
            
        cout << " Adventure Map. \nThis Map was made by ";
        
        creator.clear();
        creator.seekg(0);
        while(creator.getline(creatorName, 15))
            cout << creatorName;
        
        cout << ".\nThis application was made by Aaron Walwyn.\n\n"
             << "To read a Note type 'Note' \nTo read the Rules tye 'Rules' \nTo exit Type 'Exit'"
             << "\n\nInput: ";
        cin >> input;
        cout << "\n";
        
        if ((strcmpi(input,"note")==0))
        {
            cout << "Note: ";
            cin >> noteNumber;
            cout << "\n";
          
            switch(noteNumber)
            {
                case 1:
                     note1.clear();
                     note1.seekg(0);
                     while(note1.getline(str, 256))
                         cout << str << "\n";
                     break;
                case 2:
                     note2.clear();
                     note2.seekg(0);
                     while(note2.getline(str, 256))
                         cout << str << "\n";
                     break;
                default:
                     cout << "\n\nThat is not a valid note number, please try again...\n\n";
            }
        }
        else if ((strcmpi(input,"rules")==0)) //Rules 
        {
             rules.clear();
                     rules.seekg(0);
                     while(rules.getline(str, 256))
                         cout << str << "\n";
        }
        else if ((strcmpi(input,"devmode")==0)) //Dev Mode
        {
            ofstream map("map.txt");
            ofstream creator("creator.txt");
    
            int devExit;
            devExit = 0;
            
            while(devExit==0)
            {    
             char passEnter[30], password[30];
             password[30] = 'default';
             int devMenu;
             
             while (passcheck==0 && devExit==0)
                 {                     
                     cout << "DEV MODE is password protected, please enter the password: ";
                     cin >> passEnter;
                     cout << "\n\n";
                     //passcheck=1;
                 
                     if ((strcmp(passEnter,"default")==0)) //password is entered here
                     {
                         passcheck=1;
                         system("CLS");
                         cout << "DEV MODE\n\n"
                              << "Select a menu option by typing the number\n"
                              << "1) Rename Map\n"
                              << "2) Create New Note\n"
                              << "3) Create Map Rules\n"
                              << "4) Change creator name\n"
                              << "5) Change DEV MODE password\n";
                         
                         cout << "\nMenu Choice: ";
                         cin >> devMenu;
                         
                         switch(devMenu)
                             {
                                 case 1:
                                      cout << "\nType what goes as the map name: ";
                                      cin >> mapName;
                                      map << mapName;
                                      break;
                                 case 2:
                                      cout << "New note function goes here\n";
                                      break;
                                 case 3:
                                      cout << "Rules functions goes here\n";
                                      break;
                                 case 4:
                                      cout << "\nType the creator's name: ";
                                      cin >> creatorName;
                                      map << creatorName;
                                      break;
                                 default:
                                      devExit = 1;
                             }
                     }
                     else
                     {
                        cout << "\n\nPassword incorrect please try again\n\n";
                     }
                 passcheck = 0;
                }
            }
            
            }        
        
            else if ((strcmpi(input,"exit")==0)) //Exit 
            {
                 exit=1;
            }
        cout << "\n\n";     
        system("PAUSE");
        system("CLS");            
        }
    
    note1.close();
    note2.close();
    rules.close();
    map.close();
    creator.close();
    cout << "Thanks for using this feature \nThis software was made by Aaron Walwyn. \nwww.youtube.com/actionmanmedia \nDo not distribute.\n\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}


Input at 120 - 122 & 131 - 133
Last edited on
In your code:

1
2
                                     cin >> mapName;
                                      map << mapNameInput;


you are reading input from the keyboard into variable [mapName], however, when you output to the ofstream [map] you are outputting [mapNameInput]

This seems like a bug to me.

closed account (zb0S216C)
Have you contemplated the use of std::string? By the looks of it, you haven't. Should your string input accept white-space, use std::getline(). Have you considered the use of functions?

Where's the part where the user is allowed to customize?

Wazzak
Topic archived. No new replies allowed.