now i have have a certain function that calls for it and i have written the function but i am having some error, well i have two and i don't know how to procedd from there.
so what i was doing was what you would when you a file with one thing in each line and read the file and store it in a vector but noe each lline no longer has just one thing in it.
so here is my complete code i have already tested the class and it runs i know everything other than this read file function which i haven't been able to figure out from two days now and i read two books still i am stuck and wheni run this it tells me fname not initilized.
/*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>
// 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;
}
//city.cpp ends here
#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 << " =- assignment 4 -= " << 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
so what should I do, i am really stuck here
This post has been edited by iamadumbchild: 31 March 2011 - 03:04 AM

New Topic/Question
Reply



MultiQuote






|