Help me expand my beginner program?
Aug 31, 2013 at 7:17am UTC
Okay well my code below works fine right now. It is a simple journal/diary program, after I create the entry, I can then read it when choosing "View Entries" in my main menu, but I want to know how I can continue to add more entries after the first one I add, because if I try right now it just overwrites the old one (I understand why it does this, I just don't know how to achieve what I want it to).
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
#include <iostream>
#include <string>
using namespace std;
class Entry{
public :
void setEntry(string x){
entry = x;
}
string getEntry(){
return entry;
}
private :
string entry;
};
bool mainMenu(Entry & obj){
string choice;
cout << "\n" << "< Menu >" ;
cout << "\n01. Create a journal entry." ;
cout << "\n02. View your journal entries." ;
cout << "\nEnter your choice or /exit to exit the program: " ;
getline (cin,choice);
if (choice == "01" ){
string x;
cout << "\n< Create Entry >" << endl;
getline (cin,x);
obj.setEntry(x);
cout << "\nYour entry has been saved, returning to the main menu.." << endl;
return true ;
}
else if (choice == "02" ){
cout << "\n< View Entries >" << endl;
cout << obj.getEntry() << endl;
return true ;
}
else if (choice == "/exit" ){
string warning;
cout << "\nAre you sure? Type /exit to exit or /menu to return to the main menu: " ;
getline (cin,warning);
if (warning == "/exit" ){
return false ;
}
else if (warning == "/menu" ){
return true ;
}
}
}
int main()
{
bool loop = true ;
Entry MyEntry;
cout << "Welcome to this program!" << endl;
while (loop)
loop = mainMenu(MyEntry);
cout << "\nExiting program!" << endl;
}
Aug 31, 2013 at 7:22am UTC
use list of entries instead of string..
for e.g list<string> entries;
hope this helps!
Aug 31, 2013 at 7:29am UTC
Could you give me some direction on how I would implement that? Like what exactly I should replace? Im new to C++ its my first language, sorry if I sound dumb.
Aug 31, 2013 at 7:34am UTC
What i was trying to say you is
1 2 3 4 5 6 7 8 9 10 11 12
class Entry{
public :
void setEntry(string x){
entry = x;
}
string getEntry(){
return entry;
}
private :
//string entry;
list<string> entries;
};
http://www.cplusplus.com/reference/list/list/
Topic archived. No new replies allowed.