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