1

In my lab I'm trying to process a line that represents a destination on Earth. An example of this is ....

"152/N 200/E California Ave"

In my professor's notes, he said that if you did this ...

std::cin >> latitude >> latitudeDirection >> longitude >> longitudeDirection >> address

Which consumes all the way up until California, where from there on out the string is consumed one at a time at each white space. How do I make it so I consume the rest of the input? Here's how my variables look when assigned ...

latitude = 152
latitudeDirection = "/N"
longitude = 200
longitudeDirection = "/E"
address = "California"

address only holds "California" when I want it to hold "California Ave". Here's the code I have so far.

int numberOfLocations;
    std::cin >> numberOfLocations;
    std::cin.ignore();
    for (int x = 0; x < numberOfLocations; x++) {
        double longitude, latitude;
        std::string longitudeDirection, latitudeDirection, address;

        /*
        std::cin >> latitude >> latitudeDirection >> longitude >> longitudeDirection >> address;
        std::cout << latitude << latitudeDirection << latitude << latitudeDirection << address << std::endl;
        */
    }
Jacob Macallan
  • 959
  • 2
  • 8
  • 29
  • Check [c++ input with spaces](http://stackoverflow.com/questions/5838711/c-cin-input-with-spaces) – sam Oct 16 '15 at 00:39

2 Answers2

2

The ordinary >> as input operator splits the input into "words" separated by whitespace.

To avoid that, consider reading one item per line of input. You can use std::getline from <string> to read a line. Where you need a number you can parse the line with e.g. std::stoi.

If you have no choice but to deal with a string that contains multiple items of input, where at the end there's textual data that can contain spaces, then you can use a std::istringstream to read the items before the text.


Example 1 – reading one item per line:

#include <iostream>
#include <stdlib.h>         // exit, EXIT_FAILURE
#include <string>           // std::string
using namespace std;

void error( string const& s )
{
    cerr << "!" << s << endl;
    exit( EXIT_FAILURE );
}

auto get_input_line()
    -> string
{
    string line;
    getline( cin, line );
    if( cin.fail() ) { error( "Uh oh, ..." ); }
    return line;
}

auto main() -> int
{
    cout << "This program adds two numbers A and B." << endl;
    cout << "Number A, please? ";
    double const a = stod( get_input_line() );
    cout << "Number B, please? ";
    double const b = stod( get_input_line() );
    cout << a << " + " << b << " = " << a + b << "." << endl;
}

Example 2 – parsing a line of text
(IMHO not smart, but perhaps a requirement)

#include <iostream>
#include <stdlib.h>         // exit, EXIT_FAILURE
#include <string>           // std::string
#include <sstream>          // std::istrstream
using namespace std;

void error( string const& s )
{
    cerr << "!" << s << endl;
    exit( EXIT_FAILURE );
}

auto main() -> int
{
    string const data   = "152/N 200/E California Ave";

    cout << "This program parses the line '" << data << "'." << endl;
    cout << endl;
    cout << "Result:" << endl;

    istringstream stream( data );
    char    dummy_char;
    int     latitude;
    char    latitudeDirection;
    int     longitude;
    char    longitudeDirection;
    string  address;

    stream
        >> latitude >> dummy_char >> latitudeDirection
        >> longitude >> dummy_char >> longitudeDirection;
    if( stream.fail() ) { error( "Initial items extraction failed." ); }
    while( stream.peek() == ' ' ) { stream.get(); }
    getline( stream, address );

    cout << "> "
        << latitude << "/" << latitudeDirection << " "
        << longitude << "/" << longitudeDirection << " "
        << "'" << address << "'"
        << "<" << endl;
}
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

The problem here is that, you are trying to enter strings with space, therefore, as inputs via cin are delimited by a space ' ', you will have to use std::getline which will make the end of input delimeter as newline. Example Implementation:

#include <iostream>
using namespace std;
int main()
{
    string s;
    getline(cin,s);
    cout<<s;
    return 0;
}

with this you can enter string with spaces. (end of string will be after a n enter pressed by user, there for end of string delimiter is newline)

Pranay Mathur
  • 903
  • 2
  • 9
  • 19
  • Unfortunately, I also want to split the string into variable types of doubles and ints as well while at the same time consuming the rest of the string. Is there a convenient way to do that? – Jacob Macallan Oct 16 '15 at 00:50
  • again there must be some kind of symbols or some other strings on basis of which you have to split the strings, therefore those symbols becomes delimiter for splitting, find the delimeter by find function and and the use substr function. string s = "scott>=tiger"; string delimiter = ">="; string token = s.substr(0, s.find(delimiter)); //output: scott – Pranay Mathur Oct 16 '15 at 00:56
  • let's say in the above comment s="123>=tiger", then after splitting you will get token="123" then typecast it to integer or float by int x=int(token) – Pranay Mathur Oct 16 '15 at 01:08