#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
using std::string;
usingnamespace std;
int main()
{
int userMenu;
cout<< "What would you like to do: " << endl;
//User Menu layout
do{
cout<< "1-Store Event(s)" << endl << "2-Lookup Events" << endl << "3-Delete Events" << endl << "4-Exit" << endl;
cin>>userMenu;
{
//Code for User Menu
switch(userMenu)
{
case 1: //Storing Events in a new file "events.txt
{
ofstream events;
events.open("events.txt", ofstream::app);
if(events.is_open())
{
events << "\n";
string storedEvent;
cout<< "What event would you like to log?"<< endl;
cin>> storedEvent;
events << storedEvent;
}
events.close();
break;
}
case 2: //Looking up stored events
{
system("cls");
cout << "---Events---"
string line;
ifstream events ("events.txt");
if(events.is_open())
{
while (! events.eof())
{
getline(events,line);
cout << line << endl;
}
}
events.close();
break;
}
case 3:
{
}
}
}
while (userMenu != 4);
return 0;
}
From what I can think of there are only two things that I need help doing
When I enter a line (by pushing one) it only tranfers what i'm typing into the file untill there's a space. So if i hit 1, type "hello my name is charlie", the txt file "events.txt" would show
hello.
The second problem is I want to be able to delete what I enter and since i'm storing everything i enter on a new line I was wondering how i'd be able to delete a specific line from the txt file i'm storing my events in. I was thinking maybe transferring the contents of the file to an array and then it would be easy to delete from the array(I'm not sure how i'd do this though). Any help would be appreciated as i'm working on this at my own leisure.
For problem #1 you have to use getline (just as you did for reading the file) instead of cin >> storedEvent.
For problem #2 you've got the idea; you just have to code it. Read the file into an array. You are already reading the file on lines 43-47. Put the 1st line you read into index [0], the second into index [1] and so forth.
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<vector>
using std::string;
usingnamespace std;
int main()
{
int userMenu;
cout<< "What would you like to do: " << endl;
//User Menu layout
do{
cout<< "1-Store Event(s)" << endl << "2-Lookup Events" << endl << "3-Delete Events" << endl << "4-Exit" << endl;
cin>>userMenu;
cin.get();
{
//Code for User Menu
switch(userMenu)
{
case 1: //Storing Events in a new file "events.txt
{
ofstream events;
events.open("events.txt", ofstream::app);
if(events.is_open())
{
system("cls");
string storedEvent;
cout<< "What event would you like to log?"<< endl;
getline (cin, storedEvent);
events << storedEvent << endl;
}
else cout<< "unable to open file";
events.close();
break;
}
case 2: //Looking up stored events
{
int i=1;
system("cls");
cout << "---Events---"<< endl;
string line;
ifstream events ("events.txt");
if(events.is_open())
{
while (! events.eof())
{
getline(events,line);
cout << i++ << " " << line << endl;
}
}
events.close();
break;
}
case 3:
{
system("cls");
vector<string> Delete;
string line;
fstream events;
events.open("events.txt", istream::in);
if(events.is_open())
{
while (! events.eof())
{
getline(events,line);
Delete.push_back(line);
}
int i;
for( i=1; i<Delete.size(); ++i)
cout <<i << " " << Delete[i] << endl;
}
events.close();
}
}
}
}
while (userMenu != 4);
return 0;
}
I'm mainly focusing on Case 3:
I've stored the file contents in a vector because an array doesn't automatically adjust to the file size like a vector does.
I've displayed the contents of the vector.
Now all i need to do is figure out how to delete a specific line from the vector
So if the vector prints out this
1 Clean car
2 Clean house
3 wash dog
I want to be able to push 3 and have it delete the 3rd line without erasing the other contents of the file. I know i have to save it and reprint it but i'm still i little lost can someone give me a push in the right direction.
In your Delete string your syntax is wrong. Use a vector to store strings. Then get the event to be deleted and iterate through the vector of strings and find the event. Then use the erase function that comes with the vector class.
Here is some sample code I wrote for another post similar to this.