File open problem

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;
}

while (!infile.eof())
{
infile >> month >> gas[month] >> elect[month] >> phone[month]
>> rent[month] >> games[month];

if (taxchoice == 1)
games[month] = (.06*games[month]) + games[month];
monthtotal[month] = gas[month] + elect[month] + phone[month] + rent[month] + games[month];
gastotal = gas[month] + gastotal;
electtotal = elect[month] + electtotal;
phonetotal = phone[month] + phonetotal;
renttotal = rent[month] + renttotal;
gamestotal = games[month] + gamestotal;
}

It seems to get some of the numbers, but then it gets a lot of weird numbers too. I don't know what the problem is.

Thanks!
I don't see anything obviously wrong. Could you post "input_table.txt"?
If it's really big, then just post part of it.
1 65 180 68 475 45
2 63 185 68 475 57
3 68 178 73 475 52
4 85 175 68 475 42
5 125 155 55 505 37
6 145 120 58 505 47
7 152 117 55 505 45
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 '%'.

x = x * i is the same as x*=i
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;
}

while (!infile.eof())
{
infile >> month >> gas[month] >> elect[month] >> phone[month]
>> rent[month] >> games[month];

if (taxchoice == 1)
games[month] = (.06*games[month]) + games[month];
monthtotal[month] = gas[month] + elect[month] + phone[month] + rent[month] + games[month];
gastotal = gas[month] + gastotal;
electtotal = elect[month] + electtotal;
phonetotal = phone[month] + phonetotal;
renttotal = rent[month] + renttotal;
gamestotal = games[month] + gamestotal;
}

infile.close();

monthamount = month;

table(gas, elect, phone, rent, games, monthtotal, gastotal, electtotal, phonetotal, renttotal, gamestotal, monthamount, month);

while (menuchoice != 3)
{
cout << endl << "Do you want to \n " << "1. Add another month \n"
<< "2. Display the most expensive month\n" << "3. Exit \n"
<< "Choose number: ";

cin >> menuchoice;
while (menuchoice < 1 || menuchoice > 3)
{
cout << "Invalid choice. Please try again: ";
cin >> menuchoice;
}

if (menuchoice == 1)
{
month = monthamount;
month++;

cout << "Money spent on gas: ";
cin >> gas[month];
while (gas[month] < 0)
{
cout << "Invalid input. Try again: ";
cin >> gas[month];
}
gastotal = gastotal + gas[month];

cout << "Money spent on electricity: ";
cin >> elect[month];
while (elect[month] < 0)
{
cout << "Invalid input. Try again: ";
cin >> elect[month];
}
electtotal = electtotal + gas[month];

cout << "Money spent on phone service: ";
cin >> phone[month];
while (phone[month] < 0)
{
cout << "Invalid input. Try again: ";
cin >> phone[month];
}
phonetotal = phonetotal + phone[month];

cout << "Money spent on rent: ";
cin >> rent[month];
while (rent[month] < 0)
{
cout << "Invalid input. Try again: ";
cin >> rent[month];
}
renttotal = renttotal + rent[month];

cout << "Money spent on games: ";
cin >> games[month];
while (games[month] < 0)
{
cout << "Invalid input. Try again: ";
cin >> games[month];
}
gamestotal = gamestotal + games[month];
if (taxchoice == 1)
games[month] = (.06*games[month]) + games[month];

monthtotal[month] = gas[month] + elect[month] + phone[month] + rent[month] + games[month];

monthamount = month;

table(gas, elect, phone, rent, games, monthtotal, gastotal, electtotal, phonetotal, renttotal, gamestotal, monthamount, month);

}

else if (menuchoice == 2)
{
for (month = 1; month <= monthamount; month++)
{
if (monthtotal[month] > expensivemonth)
expensivemonth = monthtotal[month];
}

cout << "The most expensive month was $" << expensivemonth;
}

else if (menuchoice == 3)
{
tablefile(gas, elect, phone, rent, games, monthtotal, gastotal, electtotal, phonetotal, renttotal, gamestotal, monthamount, month);
return 0;
}
}



return 0;
}

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 << "\nMonth ";

for (month = 1; month <= monthamount; month++)
{
cout << month << " ";
}

cout << "\nGas ";

for (month = 1; month <= monthamount; month++)
{
cout << "$" << gas[month] << " ";
}

cout << "\nElectricity ";

for (month = 1; month <= monthamount; month++)
{
cout << "$" << phone[month] << " ";
}

cout << "\nRent ";

for (month = 1; month <= monthamount; month++)
{
cout << "$" << rent[month] << " ";
}

cout << "\nGames ";

for (month = 1; month <= monthamount; month++)
{
cout << "$" << games[month] << " ";
}

cout << "\nMonth total ";
for (month = 0; month <= monthamount; month++)
{
cout << "$" << monthtotal[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");

outfile << "\nMonth ";

for (month = 1; month <= monthamount; month++)
{
outfile << month << " ";
}

outfile << "\nGas ";

for (month = 1; month <= monthamount; month++)
{
outfile << "$" << gas[month] << " ";
}

outfile << "\nElectricity ";

for (month = 1; month <= monthamount; month++)
{
outfile << "$" << phone[month] << " ";
}

outfile << "\nRent ";

for (month = 1; month <= monthamount; month++)
{
outfile << "$" << rent[month] << " ";
}

outfile << "\nGames ";

for (month = 1; month <= monthamount; month++)
{
outfile << "$" << games[month] << " ";
}

outfile << "\nMonth total ";
for (month = 0; month <= monthamount; month++)
{
outfile << "$" << monthtotal[month] << " ";
}

outfile << "\nYou spent a total of $" << gastotal << " on gas, $" << electtotal << " on electricity, \n"
<< "$" << phonetotal << " on phone service, $" << renttotal << " on rent, and $" << gamestotal
<< "\n on games.";

outfile.close();
}
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.
There wasn't an empty line. I wonder if the problem is just with my computer or something. Thanks for the help!
Topic archived. No new replies allowed.