Hello
I have a problem where when if I call my 'Display' function from within my 'Menu' function it(the 'Display' function) does not work. But if I call my 'Display' function from within my 'Read'function it does work.
When I say it doesn't work, I mean the information extracted from a text file is not passed/sent to the 'Display' function, so the output of 'Display' is just all zeros?
Can you tell me why this occurs & how I can fix it? :)
Here is my program:
// In this example the 'Display' function is called within the 'Menu' function
// This example does not work; meaning the information grabbed from the text file
// is not output (passed) to the 'Display' function & I dont know why?
#include<iostream>
#include <iomanip>
#include<string>
#include<fstream>
using namespace std;
struct budget {
double expenditures[199];
double budget_limit;
double total_expenditure;
};
budget stats;
int counter = 0;
void menu();
void read(budget stats);
void display(budget stats, int counter);
int main()
{
/// Set Expenditures array elements to zero.
memset(stats.expenditures,0,199);
menu();
return 0;
}
void menu()
{
int option;
double purchase;
cout << "//////////Personal Budget\\\\\\\\\\" << endl;
cout << "\n(1) Adjust Budget\n(2) Change Budget Cap\n(3) Budget Summary\n";
cout << "Please choose your option(1-3): " << flush;
cin >> option;
switch(option) {
case 1:
read(stats);
display(stats, counter);
break;
default:
cout << "Incorrect input";
}
}
void read(budget stats)
{
ifstream infile;
infile.open("data.txt");
if (!infile)
{
cout << "Failed to open file";
}
infile >> stats.budget_limit;
// use for statement to take in all payments
while (infile) {
infile >> stats.expenditures[counter];
counter++;
}
counter = (counter-1);
infile.close();
}
void display(budget stats, int counter) {
cout << "\nBudget cap is: " << stats.budget_limit << endl;
for (int i=0; i<counter; i++)
{
stats.total_expenditure += stats.expenditures[i];
cout << "Purchase " << i+1 << ": " << stats.expenditures[i] << "\n";
}
cout << "----------------------------------------------\n";
cout << "Total expenses = " << stats.total_expenditure << endl;
cout << "----------------------------------------------\n";
if (stats.total_expenditure > stats.budget_limit) {
cout << "BUDGET OVERRUN!! by " << (stats.total_expenditure-stats.budget_limit);
}
else {
cout << "Congratulations you are within your budget cap by $";
cout << (stats.budget_limit-stats.total_expenditure);
}
}
Here is the program where it works; the text file contents are sent to display:
// The only difference is that 'Display' function is called within the 'Read' function
// This example works; meaning the information grabbed from the text file is output
// in the display function.
#include<iostream>
#include <iomanip>
#include<string>
#include<fstream>
using namespace std;
struct budget {
double expenditures[199];
double budget_limit;
double total_expenditure;
};
budget stats;
int counter = 0;
void menu();
void read(budget stats);
void display(budget stats, int counter);
int main()
{
/// Set Expenditures array elements to zero.
memset(stats.expenditures,0,199);
menu();
return 0;
}
void menu()
{
int option;
double purchase;
cout << "//////////Personal Budget\\\\\\\\\\" << endl;
cout << "\n(1) Adjust Budget\n(2) Change Budget Cap\n(3) Budget Summary\n";
cout << "Please choose your option(1-3): " << flush;
cin >> option;
switch(option) {
case 1:
read(stats);
break;
default:
cout << "Incorrect input";
}
}
void read(budget stats)
{
ifstream infile;
infile.open("data.txt");
if (!infile)
{
cout << "Failed to open file";
}
infile >> stats.budget_limit;
// use for statement to take in all payments
while (infile) {
infile >> stats.expenditures[counter];
counter++;
}
counter = (counter-1);
infile.close();
display(stats, counter);
}
void display(budget stats, int counter) {
cout << "\nBudget cap is: " << stats.budget_limit << endl;
for (int i=0; i<counter; i++)
{
stats.total_expenditure += stats.expenditures[i];
cout << "Purchase " << i+1 << ": " << stats.expenditures[i] << "\n";
}
cout << "----------------------------------------------\n";
cout << "Total expenses = " << stats.total_expenditure << endl;
cout << "----------------------------------------------\n";
if (stats.total_expenditure > stats.budget_limit) {
cout << "BUDGET OVERRUN!! by " << (stats.total_expenditure-stats.budget_limit);
}
else {
cout << "Congratulations you are within your budget cap by $";
cout << (stats.budget_limit-stats.total_expenditure);
}
}
Exact contents of text file are:
300
7.95
2.3
15