Thread: readind file c++ problem

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    readind file c++ problem

    hello fellow programers,


    so here is my dilemma, i am trying to do this with a read fiel function

    The user should be prompted to enter the name of a file of cities. The file's contents should be read into the program's city vector, replacing its contents, if any. If the file does not exist or is formatted incorrectly the program's city vector should not be changed and the user should be prompted for a new file name.

    The first function will read a file of cities into a vector of cities and should have a string parameter that represents the name of the file to be opened and should return a vector of cities (vector <City>). This function should throw errors if the file cannot be opened or if it is formatted incorrectly. The second function would be responsible for requesting the file name and handling any errors.

    File Format

    Each city is recorded in a single line of the file with its name, latitude and longitude data separated by spaces. Here is the data from a small sample file,
    Calgary 51.1 -114.0167
    Vancouver 49.1833 -123.167
    Winnipeg 49.9 -97.2333
    Ottawa 45.3167 -75.6667
    Moose_Jaw 50.3333 -105.55



    and here is the code but the read file function is wrong i can it all just help me here the class is written by me and is tested and works :|

    Code:
    /*City.h Begins here */
     
    //city.h : include file for standard system include files,
    // or project specific include files that are used frequently, but
    // are changed infrequently
    //
    #pragma once
    #include<iostream>
    #include <exception>
    #include <stdexcept>
    #include <string>
     
    const double PI= 3.14159265358979;
    const double earth_radius= 6371;
     
    class City{
    private:
        std::string nm;
        double latitude;
        double longitude;
       
        bool check_latitude(double lati );
        bool check_longitude(double longi);
     
    public:
            // Constructors and Destructor
            // Default constructor, sets latitude and longitude to 0
            City();
            // Constructor, sets height and width to parameters w and h
            // Throws an invalid_argument exception if log or lat are <= 0
            // The exception's string indicates the error condition
            City(std::string nm,double lati, double longi);
       
     
            // Getters and Setters
            std::string getCityname(){return nm;};
            double getLatitude(){return latitude;}; //inline method
            double getLongitude(){return longitude;}; //inline method
            // The setter methods throw an invalid_argument exception
            // if log or lat are <= 0, the exception's string indicates the
            // error condition
            // Sets latitude to parameter lat
            void setLatitude(double lati);
            // Sets longitude to parameter log
            void setLongitude(double longi);
            // set the city nametion
            void setCityname(std::string nm);
            double distance(const City & c) const;
        friend std::ostream& operator<<(std::ostream& os, const City& c);
    };
     
    //city.h ends here
     
     
    // city.cpp : Defines the entry point for the console application.
    //
    /*City.cpp Begins here */
    // city.cpp : Defines the entry point for the console application.
    //
     
    #include"city.h"
    #include<iostream>
    #include<cmath>
    using namespace std;
    // constructors
    City::City() : nm(""), latitude(0.0), longitude(0.0) {}
     
    City::City(std::string nm,double lati, double longi){
       City::setCityname(nm);
       City::setLatitude(lati);
       City::setLongitude(longi);
    }
     
    bool City::check_latitude(double lati)
    {/* Range of latitude is [0,90]*/
        return lati >= -90 && lati<=90;
    }
     
    bool City::check_longitude(double longi)
    { /* Range of longitude is [0,180]*/
        return longi >=-180 && longi <= 180;
    }
     
    //Setters
    void City::setLatitude(double lati)
    {
       if(!check_latitude(lati)){
         throw std::invalid_argument("latitude value out of bounds");
       }
         
       latitude=lati;
    }
     
    void City::setLongitude(double longi)
    {
       if(!check_longitude(longi)){
           throw std::invalid_argument("longitude value out of bounds");
       }
       
       longitude= longi;
    }
    void City::setCityname(std::string nm)
    {
        this->nm = nm;  
    }
     
    double City::distance(const City & c) const
    {
        double lati1  = this->latitude * (PI / 180);
        double lati2  = c.latitude * (PI/180);
        double longi1 = this->longitude * (PI/180);
        double longi2 = c.longitude * (PI/180);
       
       double dist =  acos( sin(lati1) * sin(lati2) + cos(lati1) * cos(lati2) * cos(longi2-longi1) ) * earth_radius;
       return dist;
    }
     
    std::ostream& operator<<(std::ostream& os, const City& c){
            char lati_direction;
            char longi_direction;
     
            if (c.latitude < 0){
                    lati_direction = 'S';
            }
            else{
                    lati_direction = 'N';
            }
     
            if (c.longitude < 0){
                    longi_direction = 'W';
            }
            else{
                    longi_direction = 'E';
            }
       os <<c.nm<<" " << c.latitude<< lati_direction << " " << c.longitude<<longi_direction; //
       return os;
    }
     
    #include "city.h"
    #include <iostream>
    #include <fstream>
    #include <vector>
    using namespace std;
     
     
    void interFace();
    string cityData(string);
    vector<City> cityFile(ifstream & ist);
     
    int main(){
     
            interFace();
           
     
     
    }
     
    void interFace(){
     
            char option = 'q';
                    do{                                             // a do loop that runs the interface program
                                                                    // gives the user a set of options to choose from
                                    cout << "          =-  hello -=   " << endl << endl;
                                    cout << "Please enter an option: " << endl;
                                    cout << "1) Open city data file in a vector" << endl;
                                    cout << "2) add city to vector" << endl;
                                    cout << "3) Print the list of cities " << endl;
                                    cout << "4) Search for a city and print its latitude and longitude" << endl ;
                                    cout << "5) Find the distance between two cities " << endl;
                                    cout << "6) Find the central, or hub, city from the list of cities   "<< endl << endl;
                                    cout << "q) Quit" << endl;
     
                                                                    // prompts the user to intput one of the above options
                                    cin >> option;
     
                    if(option != 'q'){
                            if(option == '1'){
                                                                    ////////////Open citydata file in a vector////////////
                                    cout << cityData(fname);
     
                                    cout << endl << endl;
                            }else if(option == '2'){
                                                                    ////////////add city to vector////////////
     
     
                                    cout << endl << endl;
                            }else if(option == '3'){
                                                                    ////////////Print the list of cities////////////
     
     
                            }else if(option == '4'){
                                                                    ////////////Search for a city and print its latitude and longitude////////////
     
     
                            }else if(option == '5'){
                                                                    ////////////Find the distance between two cities ////////////
     
     
                            }else if(option == '6'){
                                                                    ////////////Find the central, or hub, city from the list of cities  ////////////
                            }
                    }
            }while(option != 'q');
     
                                                                    //////////// end of interface////////////
     
           
            cout << endl << endl;
    }
     
     
     
    // interface
    string Data(string)
    {
            string fname;
                    cout << "please enter the name of the file: ";
                            cin >> fname;
                    ifstream ist(fname.c_str());
            while (!ist){
                    cerr << "error opening file " << endl;
                    ist.clear();
                    cout << "enter the name of the file" << endl;
                            cin >> fname;
                    ist.open(fname.c_str());
            }
     
            return 0;
    }
     
    // opens file
    vector<City> CityFile(ifstream & ist)
    {
            vector <City> vec;
            int cityData;                                                   // CANNOT CONVERT FROM INT TO "CONST CITY &"
            while (ist >> cityData)
            {
                    vec.push_back(cityData);
            }
     
            return vec;
    }
     
     
    //file vector

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Quote Originally Posted by sathiya157 View Post
    This is an forum meant for beginners who want an introduction to C++ Programming Language. In this article you'll be given an introduction to C++ programming from the ground level which will help you in further learning of the language.
    Israel tours
    well that is alright maybe then someone can explain me how to read line by line

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by avnis View Post
    well that is alright maybe then someone can explain me how to read line by line
    sathiya157 appears to be an advertising 'bot which matches some text to keywords. Ignore it (hopefully one of the board moderators will notice).

    Quote Originally Posted by avnis View Post
    so here is my dilemma, i am trying to do this with a read fiel function

    and here is the code but the read file function is wrong
    Code:
    // opens file
    vector<City> CityFile(ifstream & ist)
    {
            vector <City> vec;
            int cityData;                                                   // CANNOT CONVERT FROM INT TO "CONST CITY &"
            while (ist >> cityData)
            {
                    vec.push_back(cityData);
            }
     
            return vec;
    }
    Let's think about this a bit:
    1) Considering the nature of the file in your spec, why would you use an int to put this data into?
    2) Considering "vec" is a vector of objects, why would you push an int onto it?

    What you probably want to do is replace "ist" with a string representing one line from the file. You break this into three parts and construct a City object with it, then push that onto your vector (alternately, you could change the constructor to accept a single string as per the file format, and extract the three pieces of info in the constructor).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM