Files problem
Mar 30, 2015 at 7:09pm UTC
I am trying to get this program to open the file and output the info onto the screen. I created a file on visual studio and another one on my desktop but both won't open. I keep getting the error message "Bad file". I even tried dragging the file onto the screen but it still keeps giving me the error. I am not sure what is causing the problem because I feel like I am inputting the file name correctly.
The file contains this information:
Joseph Kardian III
1500 4 6
Joe Karlton
3000.00 4 6
Nancy Brown
6780 6 -10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
string full_name, file_name;
cout << " Program Number 2" << endl;
cout << " Joe Blake" << endl;
cout << " Computer Science 1" << endl << endl;
ifstream fin;
cout << " Enter file name including the path: " ;
getline(cin, file_name);
fin.open(file_name.c_str());
if (fin.fail())
{
cout << "Bad file. " << endl;
exit(0);
}
getline(fin, full_name);
double initial_amount;
int years_wished, compound_yearly;
while (!fin.eof())
{
fin >> initial_amount >> compound_yearly >> years_wished;
double interestrate, final_amount, interest_earned;
if (years_wished >= 5)
interestrate = .045;
else if (years_wished >= 4)
interestrate = .04;
else if (years_wished >= 3)
interestrate = .035;
else if (years_wished >= 2)
interestrate = .025;
else if (years_wished >= 1)
interestrate = .02;
else
interestrate = .015;
cout << showpoint << fixed << setprecision(2);
final_amount = initial_amount * pow((double )(1 + (interestrate / compound_yearly)), (compound_yearly*years_wished));
interest_earned = final_amount - initial_amount;
cout << left << setw(15) << "\n\nName" << setw(10) << "Years" << setw(20) << "Deposit Amount" << setw(20) << "Interest Earned" << " Total" << endl;
cout << setfill('-' ) << setw(73) << "" << setfill(' ' ) << endl;
cout << setfill('-' ) << setw(73) << "" << setfill(' ' ) << endl;
cout << left << setw(15) << full_name << setw(8) << years_wished << "$" << setw(19) << initial_amount << "$" << setw(20) << interest_earned << "$" << final_amount << endl << endl;
fin.ignore(100, '\n' );
getline(fin, full_name);
}
cout << "Thank you for using tax program. Have a nice day." << endl << endl;
fin.close();
return 0;
}
Mar 30, 2015 at 7:40pm UTC
Put the file inside your project see if that helps. Also, you never want to do this
Edit: cool :)
Last edited on Mar 30, 2015 at 7:44pm UTC
Mar 30, 2015 at 7:42pm UTC
What I ended up doing was creating a text file in Visual Studio and saving it into my desktop. Then I ran the program and dragged the file from my desktop into the program and it worked!
Topic archived. No new replies allowed.