I have a promblem with following case : i have a properties.txt that included:
10 Napier St.
Palm Cove
350000
47 Darkien Cl.
Smithfield Heights
265000
i want to write: On start-up, the program is to prompt the user for the property listings filename (the sample file provided is called properties.txt). The file contains the street address, suburb and price (in whole dollars) of each property listed by the agent. Once opened, the program is to read the data for each property from the file into a single array of structs. thanks i am stuck with that promblem to do other case .. what should i do ?

First declare a structure that contains elments for street address, suburb, and price.

Then useing ifstream open and read the file.

I could write the code for you, but I won't. I wouldn't want to deprive you of the joy of doing that yourself :)

String Stream is probably better for this kind of function.

int main() {
    int OptionChoice;
    const int maxCount = 50;
    string PropertyName;
    string address[maxCount],suburb[maxCount],price[maxCount];
    cout << "Welcome to CPproperty service Company"<<endl;
    cout << "Enter Property file name:";
    getline (cin, PropertyName);
    ifstream fin;
    fin.open(PropertyName.c_str());
    cout <<endl;
    
    int i= 0;
     if(!fin.is_open())
            {
                    cout<<"Sorry, No file found"<<endl;
            }
            else{
                    while (!fin.eof())
                    {
                          getline(fin,address[i]);
                                                  if(address[i]=="")
                                                  {
                                                                    break;
                                                  }
    getline(fin,suburb[i]);
    getline(fin,price[i]);
    i++;
                   }
               }
    fin.close();
    cout << endl;

I tried to write the program shown in above .. it worked out normally ..but i want to read data from .txt file into single array of struct .. I learn from everywhere and some of the example doesn't clear.. struct is for what good used? and is there any changes in output? just only as a data warehouse ? please show me how to put into sigle array of struct ? i gladly learn from here

If you make a structure you can store all this data.
so what you need is a structure which contains many strings

struct fulladdress {
  string propertyname;
  string addressline1;
  string addressline2;
  int postcode; //zip code or depends what you call it.
 (type) blah;

};

when you declare a structure you create enough space to store 1 address. so if you make this into an array using something like.

*p = new fulladdress[200];

this will allow you to store 200 addresses in your array of structures. It will return a pointer (*p) to the first element in the structure array.
You can acess this data using the -> operator.

remember if you create this on the heap, (which is advisable because the amount of records in your file could vary) then you will need to delete it when you are finished.

hello Dazaa, could you enhance my program shwon in above.. actaully i am a newbie of C++, i still learning about structure .. i am afraid that struct could effect on readind data from file .. :)

thanks you daza.. let me try my best.. :)

It would look somewhat Like this:

I have not compiled it yet, but hopefully you get the idea.

#include <iostream>
#include <string>
using namespace std;

struct property {  
    string address;  
    string suburb;  
    string price;
    //modify this accordingly, just use standard data types like a string or something doesnt need to be fancy. 
};

int main() {
    //ill assume you dont have more than 50 addresses in the file.
    const int maxCount = 50;
    struct property[maxCount];
    string PropertyName;
    cout << "Welcome to CPproperty service Company"<<endl;   
    cout << "Enter Property file name:"<<endl;
    int i= 0;
    cin.getline (PropertyName, 80);
    ifstream fin;
    fin.open(PropertyName.c_str());
    if(!fin.is_open()) {
        cout<<"file problem";
        exit(1);
    }
    while (fin.is_good() && i < maxCount) {
        getline(fin,property[i].address);
        if(property[i].address=="") {
            break;
        }
        getline(fin,property[i].suburb);    
        getline(fin,property[i].price);    
        i++;
    }
    fin.close();

    //to access all the data you need to use the . operator... 
    //if your file was massive and you had lots of entries .e.g more than 50 I would suggest using the 
    //pointer. so instead of declaring struct property[maxCount], I would use (*pointer = new struct property[maxCount];) and if the array
    //got too big e.g more than 50 inputs, i would write some code to expand the array. 
    //if you want to access the data you use pointer[i]->address; this is the same as (*pointer[i]).address
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.