Swerve
March 17th, 2008, 09:00 PM
Hi!
I've wrote a program which placed the contents of three array's into a txt file.
#include <iostream>
#include <fstream>
using namespace std;
const int MAXCHARS = 20;
const int MAXITEMS = 10;
int main()
{
char ItemName[MAXITEMS][MAXCHARS] = { "Caviar", "Sprouts", "Salmon", "Eggs", "Truffles",
"Quail", "Champagne", "Bread", "Brioche", "Apples"};
double Price [MAXITEMS] = {12.90, 0.80, 6.50, 0.75, 7.29, 5.55, 21.90, 0.80, 1.20, 1.10};
bool Luxury [MAXITEMS] = {false, false, true, false, true, true, false, false, true, false};
ofstream mystream("myfile2.txt");//create and open a file called 'myfile'through a stream 'mystream'.
if(!mystream)//if the above fails, cout the text
{
cout << "phail " << endl;
}
for(int i = 0;i<MAXITEMS;i++)
{
mystream << ItemName[i] << " " << Price[i] << " " << Luxury[i] << endl;
}
mystream.close();//close the stream 'mystream' and it's associated file ('myfile').
system ("pause");
return 0;
}
The 3 arrays held:-
char string (food's name)
double (food's price)
bool (is food's classed as 'luxury')
(A total of 10 different items, image of the txt file is attached).
Now the next step is reading the txt file into an array of structs, and this is where I'm having difficulty.
What I have so far:-
#include <iostream>
#include <fstream>
using namespace std;
const int MAXCHARS = 20;
const int MAXITEMS = 10;
struct mystruct
{
char itemname[MAXCHARS];
double price;
bool luxury;
};
int main()
{
mystruct mystructname;
mystruct thestructsarrayname[MAXITEMS];
ifstream mystream("myfile2.txt");
if(!mystream)
{
cout << "phail " << endl;
}
for(int i = 1;i < 10;i++)
{
cout >> thestructsarrayname[i].price;
}
mystream.close();
system ("pause");
return 0;
}
I've been trying to do this for two days solid now (seriously) and whilst I know it's easy to some people, I'm finding it hard to figure out as I've never done it before, and whenever I ask people for help they either say something I don't understand, or go wayy over my head saying use vectors, or overload this and that.
If anyone could PLEASE just help me out with reading from the txt file so that the first food item goes into the first struct I would be EXTREMLY grateful. I appreciate just giving someone the answers isn't helpful, but being stuck with one issue for this long is not helpful either. I know my code may not be 'optimised', or have other issues I'm not aware of, but that's because I'm new to all this and trying to teach myself.
Sorry for the long post, but I've become really frustrated with this, because I know that once I do have a working program, I'll be able to go through it slowly with the debugger step by step, and that way I'll get it into my head, it's just the way my brain works.
Many many thanks to anyone who can help me with this.
Swerve :)
P.S. I've attached a screen shot of the txt file, because I don't understand how it knows which bit to read.
I've wrote a program which placed the contents of three array's into a txt file.
#include <iostream>
#include <fstream>
using namespace std;
const int MAXCHARS = 20;
const int MAXITEMS = 10;
int main()
{
char ItemName[MAXITEMS][MAXCHARS] = { "Caviar", "Sprouts", "Salmon", "Eggs", "Truffles",
"Quail", "Champagne", "Bread", "Brioche", "Apples"};
double Price [MAXITEMS] = {12.90, 0.80, 6.50, 0.75, 7.29, 5.55, 21.90, 0.80, 1.20, 1.10};
bool Luxury [MAXITEMS] = {false, false, true, false, true, true, false, false, true, false};
ofstream mystream("myfile2.txt");//create and open a file called 'myfile'through a stream 'mystream'.
if(!mystream)//if the above fails, cout the text
{
cout << "phail " << endl;
}
for(int i = 0;i<MAXITEMS;i++)
{
mystream << ItemName[i] << " " << Price[i] << " " << Luxury[i] << endl;
}
mystream.close();//close the stream 'mystream' and it's associated file ('myfile').
system ("pause");
return 0;
}
The 3 arrays held:-
char string (food's name)
double (food's price)
bool (is food's classed as 'luxury')
(A total of 10 different items, image of the txt file is attached).
Now the next step is reading the txt file into an array of structs, and this is where I'm having difficulty.
What I have so far:-
#include <iostream>
#include <fstream>
using namespace std;
const int MAXCHARS = 20;
const int MAXITEMS = 10;
struct mystruct
{
char itemname[MAXCHARS];
double price;
bool luxury;
};
int main()
{
mystruct mystructname;
mystruct thestructsarrayname[MAXITEMS];
ifstream mystream("myfile2.txt");
if(!mystream)
{
cout << "phail " << endl;
}
for(int i = 1;i < 10;i++)
{
cout >> thestructsarrayname[i].price;
}
mystream.close();
system ("pause");
return 0;
}
I've been trying to do this for two days solid now (seriously) and whilst I know it's easy to some people, I'm finding it hard to figure out as I've never done it before, and whenever I ask people for help they either say something I don't understand, or go wayy over my head saying use vectors, or overload this and that.
If anyone could PLEASE just help me out with reading from the txt file so that the first food item goes into the first struct I would be EXTREMLY grateful. I appreciate just giving someone the answers isn't helpful, but being stuck with one issue for this long is not helpful either. I know my code may not be 'optimised', or have other issues I'm not aware of, but that's because I'm new to all this and trying to teach myself.
Sorry for the long post, but I've become really frustrated with this, because I know that once I do have a working program, I'll be able to go through it slowly with the debugger step by step, and that way I'll get it into my head, it's just the way my brain works.
Many many thanks to anyone who can help me with this.
Swerve :)
P.S. I've attached a screen shot of the txt file, because I don't understand how it knows which bit to read.