Jun 8, 2013 at 6:18pm Jun 8, 2013 at 6:18pm UTC
Hi,I am trying to write a program that lets the user keep a journal or a diary.
This is what I have so far.
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
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
string newEntry(string entry1, int userID);
void viewEntry(string entry1, int userID);
int main()
{
int ch;
int userID = 130464;
string entry1;
do
{
system("cls" );
cout<<"\n\n\n\tMAIN MENU" ;
cout<<"\n\n\t01. NEW JOURNAL ENTRY" ;
cout<<"\n\n\t02. VIEW JOURNAL ENTRIES" ;
cout<<"\n\n\t03. EXIT" ;
cout<<"\n\n\tSelect Your Option (1-3) " ;
cin>>ch;
system("cls" );
switch (ch)
{
case '1' :
newEntry(entry1, userID);
break ;
case '2' :
viewEntry(entry1, userID);
break ;
case '3' :
cout<<"\n\n\tGoodbye" ;
break ;
default :cout<<"\a" ;
}
cin.ignore();
cin.get();
}while (ch!='3' );
return 0;
}
string newEntry(string entry1, int userID)
{
ofstream outFile;
outFile.open("journal" );
if (!outFile)
{
cout << "Error could not open file." ;
return entry1;
}
int pw;
cout << "Enter your userID: \n" ;
cin >> pw;
if (pw == userID)
{
cout << "Whats on your mind?" << endl;
getline(cin,entry1);
outFile << entry1;
}
return entry1;
}
void viewEntry(string entry1, int userID)
{
int pw;
cout << "Enter your userID:\n" ;
cin >> pw;
if (pw == userID)
{
ifstream inFile;
inFile.open("journal" );
if (!inFile)
{
cout<<"File could not be open !! Press any Key..." ;
return ;
}
cout << entry1;
}
}
I've set up a menu for the user to choose whether the user wants to write a new journal entry or view previous journal entries. I then used a switch control so that whatever the user chooses, it would call the necessary function. However, when ever the user choose something from the menu, the screen just goes blank and it doesn't go to my other functions. I am probably missing a lot of necessary things, but any advice would be much appreciated! please and thankyou!
p.s.
I would also like the program to be able to store more user profiles and more journal entries. I have no idea where to start with this.
Last edited on Jun 8, 2013 at 11:24pm Jun 8, 2013 at 11:24pm UTC
Jun 8, 2013 at 11:00pm Jun 8, 2013 at 11:00pm UTC
Line 55 and 69 are causing logic errors. A single = is used for assignment. You want to use the comparison operator which is ==.
Jun 8, 2013 at 11:23pm Jun 8, 2013 at 11:23pm UTC
thanks @icethatjaw for spotting that. How ever i still can't get the switch control to work. I think something is wrong with the loop.
Jun 8, 2013 at 11:24pm Jun 8, 2013 at 11:24pm UTC
ch
is not a character, it's an integer.
Anytime you're comparing ch
, drop the single quotes ('
)
Last edited on Jun 8, 2013 at 11:34pm Jun 8, 2013 at 11:34pm UTC
Jun 8, 2013 at 11:26pm Jun 8, 2013 at 11:26pm UTC
nvm icethatjaw, i got the loop to go to the next step. The problem was the single quotations around the number. thanks!
Jun 8, 2013 at 11:32pm Jun 8, 2013 at 11:32pm UTC
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
//headerfiles
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
string newEntry(string entry1, int userID);
void viewEntry(string entry1, int userID);
int main()
{
int ch;
int userID = 130464;
string entry1;
do
{
system("cls" );
cout<<"\n\n\n\tMAIN MENU" ;
cout<<"\n\n\t01. NEW JOURNAL ENTRY" ;
cout<<"\n\n\t02. VIEW JOURNAL ENTRIES" ;
cout<<"\n\n\t03. EXIT" ;
cout<<"\n\n\tSelect Your Option (1-3) " ;
cin>>ch;
system("cls" );
switch (ch)
{
case 1:
newEntry(entry1, userID);
break ;
case 2:
viewEntry(entry1, userID);
break ;
case 3:
cout<<"\n\n\tGoodbye\n" ;
return 1;
break ;
default :cout<<"\a" ;
}
cin.ignore();
cin.get();
}while (ch!='3' );
return 0;
}
string newEntry(string entry1, int userID)
{
ofstream outFile;
outFile.open("journal" );
if (outFile.fail())
{
cout << "Error could not open file." ;
return entry1;
}
int pw;
cout << "Enter your userID: \n" ;
cin >> pw;
cin.ignore();
if (pw == userID)
{
cout << "Whats on your mind?" << endl;
getline(cin,entry1);
outFile << entry1;
}
cout << "Entry submitted. \n\tpress enter to return to main menu\n" ;
outFile.close();
return entry1;
}
void viewEntry(string entry1, int userID)
{
int pw;
cout << "Enter your userID:\n" ;
cin >> pw;
if (pw == userID)
{
ifstream inFile;
inFile.open("journal" );
if (!inFile)
{
cout<<"File could not be open !! Press any Key..." ;
return ;
}
cout << entry1;
}
}
this is my new updated code
I can get the program to run through, however it will not write to my file. please help
Last edited on Jun 8, 2013 at 11:35pm Jun 8, 2013 at 11:35pm UTC
Jun 8, 2013 at 11:51pm Jun 8, 2013 at 11:51pm UTC
Try to add .txt to the filename. Are you trying to write to an existing file?
Last edited on Jun 8, 2013 at 11:54pm Jun 8, 2013 at 11:54pm UTC
Jun 9, 2013 at 12:30am Jun 9, 2013 at 12:30am UTC
that worked. thanks ice. im forgetting a lot of little details.