I am working on small program that reads in a string consisting of minimum two words and a space between them. My dilemma is that i keep getting an error message. Can anyone twlm me what it is that i a ding wrong?
here is my code:

#include <iostream>
#include <string>
using namespace std;

// Prtototype
void stringmod(string, string&, string&);

int main()
    string string_in;
    string string1;
    string string2;

    cout << "Please enter the string with spaces: " << endl;
    cin >> string_in;
    stringmod(string_in, string1, string2);
    cout << string1 << string2;
    return 0;


// Function Definition

void stringmod( string string_in, string& string1, string& string2)
    string positionSpace, length;

    positionSpace = string_in.find (' ');
    string1 = string_in.substr(0,positionSpace);
    string2 = string_in.substr(positionSpace,string_in.length()-1);
9 Years
Discussion Span
Last Post by vmanes

It really helps if you tell us what the error message is, or at least when/where it occurs (complier, linker, runtime?).

Even if you don't understand the message, it probably points you to where the problem exists, in this case, it's this line

positionSpace = string_in.find (' ');

positionSpace is a string, but the find( ) method returns a number, the index of where your target is found.

Once you fix that, you're going to have a runtime problem. Your input method (cin >> ) only gets one word - it stops at the first occurrence of whitespace after it's begun getting a string. So your function will never work, as there's no blank space to find.

You need to use the getline method for input - that will get any amount of words up till the newline (or other delimiter you specify.)

getline( cin, string_in );

There's one more small problem in your function, I'll leave it to you to find it once the rest is working.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.