Task: There should be one string input and two double inputs. How would you sepe
ID: 3802496 • Letter: T
Question
Task: There should be one string input and two double inputs.
How would you seperate all the strings in the line into one string and all the of doubles into their respected fields.
I have got it to work however it does not take the input of the first double that it encounters.
This is an example of the input
This is what it outputs
Expected Output
Monticello => Monticello, 36.8297222, -84.8491667
Lexingtonlo => Lexingtonlo, 37.9886111, -84.4777778
Paducah => Paducah, 37.0833333, -88.6000000
Source Code:
//Code to handle double strings in input
while (getline(cin,line))
{
if (line.empty()) continue;
istringstream is(line);
string word;
double num;
while (is >> word)
{
cout << word << " ";
if (isdigit(word[0]))
{
is >> num;
cout << "This is the Number: " << num << endl;
if(num > 0){
latitude = num;
}
else{
longitude = num;
}
}
else
{
name += word;
}
}
// Duplicate Handler - name only right now
if (!cities.count(name)){
cities.insert(make_pair(name, new City(name, latitude, longitude)));
}
name = "";
}
Explanation / Answer
while (getline(cin,line))
{
if (line.empty()) continue;
istringstream is(line);
string word;
double num;
while (is >> word)
{
cout << word << " ";
if (isdigit(word[0]))
{
//commented this line
//is >> num;
//added this below line
num=atof(word.c_str()); //atof is used to conver C type string to float
cout << "This is the Number: " << num << endl;
if(num > 0){
// latitude = num;
}
else{
//longitude = num;
}
}
else
{
name += word;
}
}
// Duplicate Handler - name only right now
if (!cities.count(name)){
cities.insert(make_pair(name, new City(name, latitude, longitude)));
name = "";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.