file i/o

trying to read and write data to /from files

Page 1 of 1

9 Replies - 645 Views - Last Post: 16 December 2010 - 12:58 PM Rate Topic: -----

Topic Sponsor:

#1 kaiser0792  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 15-December 10

file i/o

Posted 15 December 2010 - 10:30 PM

I'm working on a problem that is introducing reading and writing data to and from files. I'm new to doing this in c++ so any help would be greatly appreciated. This is my first post on this forum, so feel free to give me suggestions not only to solve my problem, but also to become a better forum member.

Mortgage Payment Calculator

#include <iostream>
#include <fstream> // Access ifstream, ofstream
#include <cmath> // Access pow()
#include <iomanip> // Access setprecision()

using namespace std;

int main()
{
// Input variables
float loanAmount; // Amount of the loan
float yearlyInterest; // Yearly interest rate
int numberOfYears; // Number of years
ifstream inData; // Input stream variable
ofstream outData; // Output stream variable

// Local variables (Used in calculations)
float monthlyInterest; // Monthly interest rate
float payment; // Monthly payment
int numberOfPayments; // Total number of payments

// Open files
inData.open("loan.in");
outData.open("loan.out");

// Read values
inData >> loanAmount >> yearlyInterest >> numberOfYears;

// Calculate values
monthlyInterest = yearlyInterest / 12;
numberOfPayments = numberOfYears * 12;
payment = (loanAmount * pow(1 + monthlyInterest, numberOfPayments)
* monthlyInterest) /
(pow(1 + monthlyInterest, numberOfPayments) - 1);

// Output results
outData << fixed << setprecision(2) << "Loan Amount: "
<< loanAmount << endl << setprecision(4) << "Interest Rate: "
<< yearlyInterest << endl << "Number of Years: "
<< numberOfYears << endl;
outData << fixed << setprecision(2) << "Monthly Payment: "
<< payment << endl;

return 0;
}



The problem instructions say a file named loan.in should first be created and saved. Its contents are as follows:

50000.00 0.0524 7


The resulting output file named loan.out should produce the following output:

Loan Amount: 50000.00
Interest Rate: 0.0524
Number of Years: 7
Monthly Payment: 712.35


The problem I'm having is that my textbook gives no detail as to how to create the loan.in file or where to save it. I use Bloodshed's C++ IDE and tried to add a file to the project in the IDE. I also tried using Notepad to create the file, but it adds a .txt to the filename. The compiler automatically creates a loan OUTfile, but when I compile and run the program and then open the load.out file I get the following output:


Loan Amount: 0.00

Is This A Good Question/Topic? 0
  • +

Replies To: file i/o

#2 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: file i/o

Posted 15 December 2010 - 10:53 PM

Good job reading the forum rules.
inData.open("loan.in");
outData.open("loan.out");


These lines are invalid. Try to look at this first I/O Files

Here is probably a better link fstream. I tried to find a tutorial from within D.I.C. in the first link. This second one will be more informative.
Was This Post Helpful? 1
  • +
  • -

#3 kaiser0792  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 15-December 10

Re: file i/o

Posted 15 December 2010 - 11:22 PM

View PostseeP+, on 15 December 2010 - 09:53 PM, said:

Good job reading the forum rules.
inData.open("loan.in");
outData.open("loan.out");


These lines are invalid. Try to look at this first I/O Files

Here is probably a better link fstream. I tried to find a tutorial from within D.I.C. in the first link. This second one will be more informative.


SeeP+, thanks for the tutorial info, but I'm afraid they are way ahead of where I am currently. Too many constructs that I don't yet understand. I'm not afraid to work for it or dig it out, but I need some basic answers, like why is the code snippet that you pulled out of my submitted code invalid? This was a textbook example, and I know that they contain mistakes, but it was given as a very straightforward example and now I'm not sure at all about how to get input from a file and write the results of my program to a file. I'm not looking for a homework answer, I'm trying to learn this on my own and I'm stuck. I can usually dig this stuff out for myself, but when an example is given and it clearly doesn't work, I need to know why and I really need a simpler explanation, if you will. Thanks for responding!

My apologies SeeP+. I only checked out your first recommendation. I just opened your second recommendation and it looks like it might be a better tutorial. Thanks for taking the time. I might need further help on the subject, but I won't bother you again until I exhaust the tutorials. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#4 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: file i/o

Posted 15 December 2010 - 11:26 PM

Quote

The problem instructions say a file named loan.in should first be created and saved.

It is because you call..
inData.open("loan.in");


before the file is created. Plus ".in" is not a valid extension. It takes time to get better at things. I gave you the links because it will help you further your c++ knowledge. When faced with a problem don't just think of, "how do I fix this problem so I can move on", think of it as, "how do I fix this problem so I know how to do it right the next time."
Was This Post Helpful? 1
  • +
  • -

#5 kaiser0792  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 15-December 10

Re: file i/o

Posted 15 December 2010 - 11:38 PM

Well said. I should add that I have basic c++ knowledge and am trying to "cram" somewhat before my next semester, where I will be expected to be further along than I am currently. I taught Mathematics for 10 years at the Secondary and Post-Secondary levels and I couldn't agree with you more. I want to know "why" when I ask a question. If I sound impatient, its not because I take that approach to learning, but its because I'm somewhat limited by time constraints. I agree totally with your philosophy though.

I wondered about the .in extension as well. It didn't make sense. I attempted to create the file before running the program so how does it know whether or not the file has been created prior to run-time. I also tried it as loan.txt when I created it in Notepad. Also, does it matter where the file is saved? How does the program know where to look for the file? Does it just have to reside int the same folder as the .cpp, .o, and .exe files, etc.?
Was This Post Helpful? 0
  • +
  • -

#6 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: file i/o

Posted 15 December 2010 - 11:51 PM

Quote

Also, does it matter where the file is saved? How does the program know where to look for the file? Does it just have to reside int the same folder as the .cpp, .o, and .exe files, etc.?

It doesn't because as the programmer you can tell it where to look for it. By default it ofstream will create the file where the .cpp resides. If you have it somewhere else then just tell it the extension of where the file is.

Here is an example:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
	string words;
	fstream file("data.txt", ios::out);
	file<<"I don't like to program\n";
	file.close();
	file.open("data.txt", ios::in);
	getline(file, words, '\n');
	cout<<words;
	cin.ignore();
}//end of function


After running the code the file will be created. If the file already exists, you know the path, and all you want to do is read the file then ios::out or ofstream wont be needed.
Was This Post Helpful? 1
  • +
  • -

#7 kaiser0792  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 15-December 10

Re: file i/o

Posted 16 December 2010 - 12:01 AM

Thank you so much for the specific help. I'm gonna call it a night, late here. In the morning I'm going to study your posts and try to get a full handle on this topic. You really have been a help. I'm brand new to this forum, so if there is a way for me to help you, please let me know. Again, thanks so much.
I hope that I can hook up with you in the future on other topics as needed. Is there a way to aim questions to preferred "experts?"

Merry Christmas!
Was This Post Helpful? 1
  • +
  • -

#8 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: file i/o

Posted 16 December 2010 - 12:11 AM

Quote

Is there a way to aim questions to preferred "experts?"

Not that I know of. Helping others solve their problems helps me and others to become better. There are many experts here at Dream In Code, I am not one of them. But hopefully someday, I'll be able to say that I am.
Was This Post Helpful? 1
  • +
  • -

#9 JackOfAllTrades  Icon User is online

  • No Sugar Coding Here!
  • member icon

Reputation: 4673
  • View blog
  • Posts: 20,340
  • Joined: 23-August 08

Re: file i/o

Posted 16 December 2010 - 05:24 AM

View PostseeP+, on 16 December 2010 - 12:26 AM, said:

Quote

The problem instructions say a file named loan.in should first be created and saved.

It is because you call..
inData.open("loan.in");


before the file is created. Plus ".in" is not a valid extension.


Extensions don't matter a whit except to the operating system in what it determines is the appropriate executable to use to open the file. To a programmer, files are files; it's all in how you open them and read from them.

EDIT: Creating the "loan.in" file in Notepad:

Open Notepad
Create file
Choose File->Save As
Change to the directory where your executable is
Change Save As Type dropdown to All Files
Enter loan.in as the filename

This post has been edited by JackOfAllTrades: 16 December 2010 - 06:09 AM

Was This Post Helpful? 2
  • +
  • -

#10 Guest_kaiser0792*


Reputation:

Re: file i/o

Posted 16 December 2010 - 12:58 PM

Thanks to all for the help. I've got it working now. Sometimes, you need a hand and also to walk away and take a fresh look later.
Thanks again.
Was This Post Helpful? 0

Page 1 of 1