I have a problem when creating the Personal Finance Software in my group. It's part of my group's assignment.
The EnterExpense() function only creates one line instead of many (if the user wants to continue entering more info).
The ViewInfo() function can read the file, but cannot load the text from the expense.txt file (download the given attachment below and try to run it).
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
using namespace std;
// Declaration of file stream variables
ifstream infile; // Input file stream variable
ofstream outfile; // Output file stream variable
char cont; // Asks the user to continue entering the information
int i = 0; // Counter
double total = 0.00; // Total expense for a month period
double y_total = 0.00; // Total expense for a year period
void EnterExpense()
{
int day; // Day of the month
string category; // Item category
string desc; // Item description
double amount; // Price amount of an item
system("cls");
do
{
cout << "Enter the day of the month: ";
cin >> day;
cout << "Enter the item category: ";
cin >> category;
cout << "Enter the item description: \n";
cin >> desc;
cout << "Enter amount: $";
cin >> amount;
outfile.open("information.txt");
outfile << day << "\t\t" << category << "\t\t" << desc << "\t\t" << amount << endl;
cout << "Do you want to continue entering more? (y/n): ";
cin >> cont;
} while (cont == 'y');
outfile.close();
return;
}
void Generate()
{
// Generating file variables
int day_g; // Day of the month
string category_g; // Item category
string desc_g; // Item description
double amount_g; // Price amount of an item
system("cls");
cout << "Generating file. Please wait" << endl;
infile.open("information.txt");
if (!infile)
{
cout << "Cannot open the information file." << "The program terminates." << endl;
}
outfile.open("expense.txt");
infile >> day_g;
infile >> category_g;
infile >> desc_g;
infile >> amount_g;
outfile << "Date \t\tCategory \t\tDescription \t\tAmount" << endl;
while (infile)
{
total = total + amount_g;
y_total = y_total + total;
i++;
outfile << day_g << "\t\t" << category_g << "\t\t" << desc_g << "\t\t" << amount_g << endl;
infile >> day_g;
infile >> category_g;
infile >> desc_g;
infile >> amount_g;
}
outfile << endl;
if (i != 0)
{
outfile << "Average expense per day over a month: $" << total / i << endl;
}
else
outfile << "No data." << endl;
infile.close();
outfile.close();
return;
}
void ViewInfo()
{
string data_info; // Data opened from the expenses text file
cout << "Here is your monthly expenses below:\n\n" << endl;
infile.open("expense.txt");
if (!infile)
{
cout << "Cannot open the expenses file." << "The program terminates." << endl;
}
infile >> data_info;
infile.close();
system("pause");
return;
}
void Report()
{
cout << "Yearly total expenses: $" << y_total << endl;
cout << "Average expense per day (yearly): $" << y_total / 365 << endl;
system("pause");
return;
}
int main()
{
int option; // Choosing an option
do
{
system("cls");
cout << "";
cout << "1. Enter expense info" << endl << endl;
cout << "2. Generate expense info into one file" << endl;
cout << "(Requires the information file created in menu option 1.)" << endl << endl;
cout << "3. View expense info" << endl;
cout << "(Requires the expenses file created in menu option 2.)" << endl << endl;
cout << "4. View report" << endl;
cout << "(Requires the expenses file.)" << endl << endl;
cout << "5. Exit" << endl << endl;
cout << "Please select: ";
cin >> option;
while (option != 5)
{
switch (option)
{
case 1:
EnterExpense();
break;
case 2:
Generate();
break;
case 3:
ViewInfo();
break;
case 4:
Report();
break;
default:
cout << "Sorry, no menu option in the system. Please select again." << endl;
break;
}
break;
}
} while (option != 5);
return 0;
}