Hi!
I'm trying to create an array of structs from the contents of a .txt file, but am having a 'few' problems.
Code:
#include <iostream>
#include <fstream>
using namespace std;
const int MAXCHARS = 20;
const int MAXITEMS =10;
struct structname
{
char itemname;
double price;
int luxury;
};
int main()
{
structname mystruct;
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};
for(int i = 0;i<10;i++)
{
mystruct.itemname = ItemName[i];
mystruct.price = Price[i];
mystruct.luxury = Luxury[i];
fout << mystruct.itemname << " " << mystruct.price << " " << mystruct.luxury << endl;
}
system("pause");
return 0;
}
I realise this is quite a mess right now, I wouldn't normally ask for such help with such a 'mess', but I think with a couple of good suggestions, I'll be able to fix it up.
Also (embarrassed.jpeg) I created an empty project in Visual Studio to attempt this, and am unsure where to place the .txt file. The 'default'/typical location for within a project is what I'm after.
The attached screenshot shows where it is located right now (in with main.cpp):-
If anyone could offer me a point or two in the right direction, I'd be extremely grateful.
Many thanks!