Hi knowledgeable C++ programmers!
I'm having problems with this program because it is giving me an error about how the file cannot read #include <ofstream>
please do take a look at the program...
// Program 16.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <ofstream>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main ( )
{
int i;
struct record
{
char name[15];
float amt;
};
record master[10] = {{"Helen", 10}, {"Julie", 20}, {"Lena", 30}, {"Alan", 40}, {"Annie", 50}, {"May", 60},
{"Lee", 70}, {"Sam", 80}, {"June", 90}, {"Bill", 100}};
record trans[7] = {{"Lena", 10}, {"Julie", 5.75}, {"Lee", 15.02}, {"Ed", 40}, {"Julie", 10.00}, {"Art", 5.00},
{"Bill", 7.32}};
ofstream foutmaster("master.dat");
ofstream fouttrans("trans.dat");
WHY do you think it should be able to find that header???
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010 In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
oh crap you're right. lol
okay that problem is solved.... but then I got 4 big errors telling me this...
error in the text file....
// Program 16.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
using std:fstream;
using std::ifstream;
using std::cout;
using std::cin;
using std::endl;
int main ( )
{
int i;
struct record
{
char name[15];
float amt;
};
record master[10] = {{"Helen", 10}, {"Julie", 20}, {"Lena", 30}, {"Alan", 40}, {"Annie", 50}, {"May", 60},
{"Lee", 70}, {"Sam", 80}, {"June", 90}, {"Bill", 100}};
record trans[7] = {{"Lena", 10}, {"Julie", 5.75}, {"Lee", 15.02}, {"Ed", 40}, {"Julie", 10.00}, {"Art", 5.00},
{"Bill", 7.32}};
ofstream foutmaster("master.dat");
ofstream fouttrans("trans.dat");
I just can't figure out how to solve this problem....
I am using Visual C++ 2005 edition.
I'm a high school student taking college courses to learn to program.
I am planning on taking computer Science as a major, but I'll need a lot of effort and help.
for (i=0; i<10; i++)
{
foutmaster << master.name[i] << master.amt[i] << endl;
}
I just can't figure out how to solve this problem....
The "master" which you defined is an array of struct record.
So, if you want to print the every element of master, I think you must do it like this:
fourmaster << master[i].name << master[i].amt << endl;
By the way, it is recommended that you move the definition of "record" out of the main()
Last edited by gfzhang; December 7th, 2008 at 10:00 PM.
ooohh okay. I fixed that.
Now I have to close the file.
I tried fouttrans.close();
foutmaster.close();
and fout.close();
but none of these worked....
how do I close these .dat files?
// Program 16.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
using std:fstream;
using std::ifstream;
using std::cout;
using std::cin;
using std::endl;
int main ( )
{
int i;
struct record
{
char name[15];
float amt;
};
record master[10] = {{"Helen", 10}, {"Julie", 20}, {"Lena", 30}, {"Alan", 40}, {"Annie", 50}, {"May", 60},
{"Lee", 70}, {"Sam", 80}, {"June", 90}, {"Bill", 100}};
record trans[7] = {{"Lena", 10}, {"Julie", 5.75}, {"Lee", 15.02}, {"Ed", 40}, {"Julie", 10.00}, {"Art", 5.00},
{"Bill", 7.32}};
ofstream foutmaster("master.dat");
ofstream fouttrans("trans.dat");
I am using Visual C++ 2005 edition.
I'm a high school student taking college courses to learn to program.
I am planning on taking computer Science as a major, but I'll need a lot of effort and help.
I am using Visual C++ 2005 edition.
I'm a high school student taking college courses to learn to program.
I am planning on taking computer Science as a major, but I'll need a lot of effort and help.
ofstream foutmaster("master.data", ... );
// read data, etc.
// when goes out of scope, it will close file automatically.
wait does that mean when the file doesn't close the VSC++ program brings up an error?
I am using Visual C++ 2005 edition.
I'm a high school student taking college courses to learn to program.
I am planning on taking computer Science as a major, but I'll need a lot of effort and help.
ofstream foutmaster("master.data", ... );
// read data, etc.
// when goes out of scope, it will close file automatically.
It is "recommend", but not necessary.
A good manner of coding, I think.
Open it before using it, close it after using it.
Otherwise, sometime in a big program, if another function want to access
the ***.data, and foutmaster has not been closed yet, maybe problem or trouble.
It is "recommend", but not necessary.
A good manner of coding, I think.
Open it before using it, close it after using it.
Otherwise, sometime in a big program, if another function want to access
the ***.data, and foutmaster has not been closed yet, maybe problem or trouble.
oh okay. thanks :]
I am using Visual C++ 2005 edition.
I'm a high school student taking college courses to learn to program.
I am planning on taking computer Science as a major, but I'll need a lot of effort and help.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.