Newbie: An Array of Structs from a .txt file?

This is a discussion on Newbie: An Array of Structs from a .txt file? within the C++ Programming forums, part of the General Programming Boards category; Hi! I'm trying to create an array of structs from the contents of a .txt file, but am having a ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Newbie: An Array of Structs from a .txt file?

    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!
    Attached Images Attached Images  

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    1. You never declare what "fout" is.
    2. You have a single char itemname in your struct, but you're trying to pass an array of chars to it.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I'm trying to create an array of structs from the contents of a .txt file, but am having a 'few' problems.
    Your code is doing the opposite. It's trying to create an array of structs filled with data in the code, not the data in the file. It is then trying to output that data, not input it.

    You need to start smaller and clarify for yourself what you are doing. If you are doing input, that means you already have a file with data in it. Is that correct? If so, write a program that opens the file and see if you can get it to compile and run. Then add one step at a time testing as you go.

    >> I created an empty project in Visual Studio to attempt this, and am unsure where to place the .txt file.
    There's no need to add the text file to the project. I don't.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    to store the strings use std::string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Replies: 5
    Last Post: 10-02-2005, 12:15 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21