Hi,
I am in C++ class, and my problem is when collecting numbers from a file and assigning them to different arrays. Here is the part of the program with the issue:
int main()
{
int gas[100], elect[100], phone[100], rent[100], games[100], monthtotal[100];
int month = 1, monthamount, expensivemonth=0, gastotal=0, electtotal=0, phonetotal=0, renttotal=0;
int gamestotal=0, taxchoice=0, menuchoice = 0;
ifstream infile;
infile.open("input_table.txt");
cout << "Welcome to the budget tracker! \n"
<< "Do you want to 1. Add sales tax to your games or 2. Not add sales tax? \n"
<< "Enter number of choice: ";
cin >> taxchoice; //Need to insert validation here
while (taxchoice > 2 || taxchoice < 1)
{
cout << "Invalid choice. Please try again: ";
cin >> taxchoice;
}
It seems to be working for me. I suspect there's something wrong with a different part of the code. I think I might know what it is too, but you'll have to post the full code.
You can use compound operators when getting the totals.
Use this: gastotal += gas[month];
It means the same thing as what you have. You can also do the same thing with '-', '*', '/' and '%'.
I am pretty sure it is just that part of the code, because I tried using cout within the loop to see what's going on with the variables, and the weird numbers happen there.
#include <iostream>
#include <fstream>
using namespace std;
void table(int gas[], int elect[], int phone[], int rent[], int games[], int monthtotal[], int gastotal, int electtotal, int phonetotal, int renttotal, int gamestotal, int monthamount, int month);
void tablefile(int gas[], int elect[], int phone[], int rent[], int games[], int monthtotal[], int gastotal, int electtotal, int phonetotal, int renttotal, int gamestotal, int monthamount, int month);
int main()
{
int gas[100], elect[100], phone[100], rent[100], games[100], monthtotal[100];
int month = 1, monthamount, expensivemonth=0, gastotal=0, electtotal=0, phonetotal=0, renttotal=0;
int gamestotal=0, taxchoice=0, menuchoice = 0;
ifstream infile;
infile.open("input_table.txt");
cout << "Welcome to the budget tracker! \n"
<< "Do you want to 1. Add sales tax to your games or 2. Not add sales tax? \n"
<< "Enter number of choice: ";
cin >> taxchoice;
while (taxchoice > 2 || taxchoice < 1)
{
cout << "Invalid choice. Please try again: ";
cin >> taxchoice;
}
void table(int gas[], int elect[], int phone[], int rent[], int games[], int monthtotal[], int gastotal, int electtotal, int phonetotal, int renttotal, int gamestotal, int monthamount, int month)
{
cout << "\nYou spent a total of $" << gastotal << " on gas, $" << electtotal << " on electricity, \n"
<< "$" << phonetotal << " on phone service, $" << renttotal << " on rent, and $" << gamestotal
<< "\n on games.";
}
void tablefile(int gas[], int elect[], int phone[], int rent[], int games[], int monthtotal[], int gastotal, int electtotal, int phonetotal, int renttotal, int gamestotal, int monthamount, int month)
{
ofstream outfile;
outfile.open("output_table.txt");
Two small things will fix a lot:
1. When you display the monthly totals you should start at 1, not 0
2. Make sure that input_table.txt doesn't end with an extra empty line. Open it from notepad and delete the last empty line if it's there.
I'd also think about formatting the output to make it look nicer.