First of all, Thank you for the help and I hope I'm following your guidelines correctly, if I'm not, I'd be more than happy to correct it.
I need some help with an assignment I have. After finally getting it to run, my program won't open the files I need. The program should open a file named prog10in.txt and output it into a file named prog10out.txt However, I put a line in it that would tell me if it failed and that keeps coming up. I am doing this in Visual Studio. I need to understand how to properly open the file whether my error is in the code or in Visual Studio. The program compiles without error.
The error should be in getAccountInfo() or outputFile().
Source.cpp:
#include <iostream>
#include <string>
#include "Account.h"
#include "Bank.h"
#include <cstdio>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream inStream;
int main(int argc, char* argv[])
{
Account accounts[MAXIMUM_ACCOUNTS];
Bank Customer;
int AccountNumber = 0;
int ID;
string lastName;
string firstName;
double balance;
Customer.getAccountInfo(argv, AccountNumber, ID, lastName, firstName,balance, accounts);
Customer.sort(AccountNumber, accounts);
Customer.outputFile(argv, AccountNumber, accounts);
}
There is an Account.h and Account.cpp file that holds the account information class.
Bank.cpp:
#include <iostream>
#include <string>
#include "Account.h"
#include "Bank.h"
#include <cstdio>
#include <fstream>
#include <iomanip>
void Bank::getAccountInfo(char* argv[], int& accountNumber, int& accountID, std::string& accountLastName, std::string& accountFirstName, double& accountBalance, Account Customers[MAXIMUM_ACCOUNTS])
{
std::ifstream fin;
int accountIndex = 0;
fin.open(argv[1]);
if (fin.fail())
{
std::cout << "Input file did not open" << std::endl;
system("pause");
exit(1);
}
while (!fin.eof() && accountIndex < MAXIMUM_ACCOUNTS)
{
std::cout.setf(std::ios::fixed | std::ios::showpoint | std::ios::right);
std::cout.precision(2);
fin >> accountID >> accountLastName >> accountFirstName >> accountBalance;
fin.ignore(1, ' ');
Customers[accountIndex]._accountID = accountID;
Customers[accountIndex]._accountLastName = accountLastName;
Customers[accountIndex]._accountFirstName = accountFirstName;
Customers[accountIndex]._accountBalance = accountBalance;
accountIndex++;
accountNumber++;
}
std::cout << "Clients: " << accountIndex << std::endl;
}
void Bank::sort(int accountNumber, Account Customers[MAXIMUM_ACCOUNTS])
{
int nextSmallest;
int IDtemp;
std::string lastNameTemp;
std::string firstNameTemp;
double balanceTemp;
for (int accountIndex = 0; accountIndex < accountNumber; accountIndex++)
{
nextSmallest = accountIndex;
for (int accountIndex2 = accountIndex + 1; accountIndex2 < accountNumber; accountIndex2++)
{
if (Customers[accountIndex2]._accountID < Customers[nextSmallest]._accountID)
{
nextSmallest = accountIndex2;
}
IDtemp = Customers[accountIndex]._accountID;
Customers[accountIndex]._accountID = Customers[nextSmallest]._accountID;
Customers[nextSmallest]._accountID = IDtemp;
lastNameTemp = Customers[accountIndex]._accountLastName;
Customers[accountIndex]._accountLastName = Customers[nextSmallest]._accountLastName;
Customers[nextSmallest]._accountLastName = lastNameTemp;
firstNameTemp = Customers[accountIndex]._accountFirstName;
Customers[accountIndex]._accountFirstName = Customers[nextSmallest]._accountFirstName;
Customers[nextSmallest]._accountFirstName = firstNameTemp;
balanceTemp = Customers[accountIndex]._accountBalance;
Customers[accountIndex]._accountBalance = Customers[nextSmallest]._accountBalance;
Customers[nextSmallest]._accountBalance = balanceTemp;
}
}
}
void Bank::outputFile(char* argv[], int accountNumber, Account Customers[MAXIMUM_ACCOUNTS])
{
int outFileIndex;
std::ofstream fout;
fout.open(argv[2]);
if (fout.fail())
{
std::cout << "Output file did not open" << std::endl;
system("pause");
exit(1);
}
if (fout.fail())
{
std::cout << "Output failed" << std::endl;
exit(0);
}
fout.setf(std::ios::fixed | std::ios::showpoint | std::ios::right);
fout.precision(2);
for (outFileIndex = 0; outFileIndex < accountNumber; outFileIndex++)
{
fout << std::setw(6) << Customers[outFileIndex]._accountID;
fout << std::setw(10) << Customers[outFileIndex]._accountLastName;
fout << std::setw(10) << Customers[outFileIndex]._accountFirstName;
fout << std::setw(10) << Customers[outFileIndex]._accountBalance;
fout << std::setw(10) << std::endl;
}
}
Again, thank you guys for the help.