can someone run my project?

Can someone run my project and tell me what i am messing up on?
Its a project that lets the user keep a journal, but I can't get the writing to a file to work.
I'm using a lot of things like class and fstream that I don't understand, and would appreciate some help.
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
  //Headers
#include<iostream>
#include<fstream>
#include<cctype>
#include<string>
using namespace std;

//class
class profile
{
	int userID;
	char name[30];

public:
	void create_profile(); // function to get user data
	int retuserID() const;	//function to return account number
}; // end of class

void profile::create_profile()
{
	cout<<"\nEnter The user ID number: ";
	cin>>userID;
	cout<<"\n\nEnter The profile's owner's name: ";
	cin.ignore();
	cin.getline(name,50);
	cout<<"\n\n\nAccount Created..";
}


int profile::retuserID() const
{
	return userID;
}

//Function Declarations
void write_profile();	//function to write record in binary file
void new_entry(int, int);//function to add new journal entries
void view_entry(int, int);//function to view previous journal entries

//MAIN
int main()
{
	{
	char ch;
	int num;
	do
	{
		system("cls");
		cout<<"\n\n\n\tMAIN MENU";
		cout<<"\n\n\t01. NEW PROFILE";
		cout<<"\n\n\t02. NEW ENTRY";
		cout<<"\n\n\t03. VIEW ENTRIES";
		cout<<"\n\n\t04. EXIT";
		cout<<"\n\n\tSelect Your Option (1-4) ";
		cin>>ch;
		system("cls");
		switch(ch)
		{
		case '1':
			write_profile();
			break;
		case '2':
			cout<<"\n\n\tEnter The USER ID. : "; cin>>num;
			new_entry(num, 1);
			break;
		case '3':
			cout<<"\n\n\tEnter The USER ID. : "; cin>>num;
			view_entry(num, 2);
			break;
		case '4': 
			cout<<"\n\n\tGoodbye.\n";
			break;
		 default :cout<<"\a";
		}
		cin.get();
	}while(ch!='4');
	return 0;
}
}

void write_profile()
{
	profile userID;
	ofstream outFile;
	outFile.open("Journal5.txt",ios::binary|ios::app);
	userID.create_profile();
	outFile.write(reinterpret_cast<char *> (&userID), sizeof(profile));
	outFile.close();
}

void new_entry(int n, int)
{
	fstream outFile;
	string date;
	string entry;
	bool found=false;
	profile userID;
	outFile.open("journal5.txt", ios::app);
	if(!outFile)
	{
		cout<<"File could not be open !! Press any Key...";
		return;
	}
	while(!outFile.eof() && found==false)
	{
		outFile.read(reinterpret_cast<char *> (&userID), sizeof(profile));
		if(userID.retuserID()==n)
		{
				cout << "\n Enter the date: ";
				getline(cin,date);
				cin.ignore();
				cout << "\n Whats on your mind?";
				getline(cin,entry);
				outFile << endl << entry << endl;
		}
	 }
	outFile.close();
	if(found==false)
		cout<<"\n\n Record Not Found ";
}

void view_entry(int n, int)
{
	fstream inFile;
	string entry;
	profile userID;
	bool found = false;

	cout << "Enter your userID:\n";
	cin >> n;
	inFile.read(reinterpret_cast<char *> (&userID), sizeof(profile));
		if(userID.retuserID()==n)
	{
	ifstream inFile;
	inFile.open("journal.txt");
	if(inFile.fail())
	{
		cout<<"File could not be open !! Press any Key...";
		return;
	}
	while(! inFile.eof())
	{
	cin.ignore();
	getline(inFile,entry);
	cout << entry;
	}
	}
}
Sorry, didn't run the code but noticed some things that I'd like to comment on.

As a test during development, try writing to screen using cout, if that works, then you can write it to the file. Comment out the test lines later.

What I see that is missing is a check to see if the filename is good.

So after each time you open a file:
1
2
	ofstream outFile;
	outFile.open("Journal5.txt",ios::binary|ios::app);


I recommend
if ( !outFile.good() )
{// give error}
else
{// write to file}

Otherwise it won't do anything and you have no notice of what is wrong.

After you do what I talked about above I think the problem will become obvious.

1
2
line 85:  outFile.open("Journal5.txt",ios::binary|ios::app);
line 98:  outFile.open("journal5.txt", ios::app);
Last edited on
Topic archived. No new replies allowed.