Hi there, i have an assignment, and i need to create a monthly expenditure. I have managed to get alot done, a fully functioning menu and all the functions. Im stuck when it comes to deleting an entry
I have been using structured arrays to store data, ive posted all my code, i thought it be easier for help, sorry for the massive post.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
int i;
int count;
int userChoice;
string temp;
int newExpenses();
int newIncome();
void menuExpenses();
void menuIncome();
void mainMenu();
void monthlyExpenses();
void monthlyIncome();
void deleteEntry();
int main()
{
string line;
ifstream myfile ("counter.txt");
if(myfile.is_open())
{
while(!myfile.eof())
{
getline(myfile,line);
temp=line;
}
myfile.close();
}
count=atoi(temp.c_str());
do{
mainMenu();
}while(userChoice != 1 || userChoice != 2);
return 0;
}
struct entry{
int ID;
string date;
string item;
string price;
string category;
}entries[100];
void mainMenu()
{
cout<<"Monthly Expenditure"<<endl<<endl<<endl;
cout<<"Main Menu"<<endl<<endl;
cout<<"Income [1]"<<endl<<"Expenses [2]"<<endl<<"View Monthly Income [3]"<<endl<<"View Monthly Expenses [4]"<<endl<<"Delete Entry [5]"<<endl;
cin>>userChoice;
if(userChoice == 1)
{
system("cls");
menuIncome();
}
else if(userChoice == 2)
{
system("cls");
menuExpenses();
}
else if(userChoice == 3)
{
system("cls");
monthlyIncome();
}
else if(userChoice == 4)
{
system("cls");
monthlyExpenses();
}
else if(userChoice == 5)
{
system("cls");
deleteEntry();
}
else
{
system("cls");
cout<<"Invalid input, please select again [1] [2] [3] [4]";
}
}
void monthlyIncome()
{
string line;
ifstream myfile ("Income.txt");
if(myfile.is_open())
{
while(!myfile.eof())
{
getline(myfile,line);
cout<<line<<endl;
}
myfile.close();
}
}
void monthlyExpenses()
{
string line;
ifstream myfile ("Expenses.txt");
if(myfile.is_open())
{
while(!myfile.eof())
{
getline(myfile,line);
cout<<line<<endl;
}
myfile.close();
}
}
void menuExpenses()
{
int userChoice2=0;
do{
cout<<"Expenses"<<endl<<endl;
cout<<"Add Entry (1)"<<endl;
cout<<"Sort Ascending (2)"<<endl;
cout<<"Sort Descending (3)"<<endl;
cin>>userChoice2;
if(userChoice2 == 1)
{
system("cls");
newExpenses();
}
else if(userChoice2 == 2)
{
system("cls");
//Sort code
}
else if(userChoice2 == 3)
{
system("cls");
//Sort code
}
else
{
system("cls");
cout<<"Invalid input, please select again [1] [2] [3]"<<endl<<endl;
userChoice2=0;
}
}while(userChoice2==0);
}
void menuIncome()
{
int userChoice3=0;
do{
cout<<"Income"<<endl<<endl;
cout<<"Add Entry (1)"<<endl;
cout<<"Sort Ascending (2)"<<endl;
cout<<"Sort Descending (3)"<<endl;
cin>>userChoice3;
if(userChoice3 == 1)
{
system("cls");
newIncome();
}
else if(userChoice3 == 2)
{
system("cls");
//Sort code
}
else if(userChoice3 == 3)
{
system("cls");
//Sort code
}
else
{
system("cls");
cout<<"Invalid input, please select again (1) (2) (3)"<<endl<<endl;
userChoice3=0;
}
}while(userChoice3==0);
}
int newIncome()
{
cout<<"Income"<<endl<<endl;
cout<<"New Entry"<<endl;
entries[count].ID=count;
cout<<"Date: ";
cin>>entries[count].date;
cout<<"Item: ";
cin>>entries[count].item;
cout<<"Price: ";
cin>>entries[count].price;
cout<<"Category: ";
cin>>entries[count].category;
cout<<endl;
ofstream fout;
fout.open("Income.txt", ios::app);
fout<<entries[count].ID<<" "<<entries[count].date<<" "<<entries[count].item<<" "<<entries[count].category<<" "<<entries[count].price<<endl;
fout<<flush;
fout.close();
count++;
ofstream fout1;
fout1.open ("counter.txt");
fout1<<count;
fout1<<flush;
fout1.close();
return 0;
}
int newExpenses()
{
cout<<"Expenses"<<endl<<endl;
cout<<"New Entry"<<endl;
entries[count].ID=count;
cout<<"Date: ";
cin>>entries[count].date;
cout<<"Item: ";
cin>>entries[count].item;
cout<<"Price: ";
cin>>entries[count].price;
cout<<"Category: ";
cin>>entries[count].category;
cout<<endl;
ofstream fout;
fout.open("Expenses.txt", ios::app);
fout<<entries[count].ID<<" "<<entries[count].date<<" "<<entries[count].item<<" "<<entries[count].category<<" "<<entries[count].price<<endl;
fout<<flush;
fout.close();
count++;
ofstream fout1;
fout1.open ("counter.txt");
fout1<<count;
fout1<<flush;
fout1.close();
return 0;
}
void deleteEntry()
{
int choice;
int i;
cout<<"What entry do you wish to delete?: "<<endl;
cin>>choice;
if(choice >= 0 && choice <= 100)
{
for(i=choice;i<100;i++)
{
entries[i] = entries[i + 1];
}
}
else
{
cout<<"Invalid Entry";
}
}
What my tutor suggested was to use a counter, and then save the counter also in a text file, so when i reopen the console, the counter doesnt reset to 0
Is there a simpler way to be able to delete one of these entries? each entry has an ID
(new to programming, still learning at uni)