Hi all,
Im having a little trouble. I have to write a program that will input a string of positive integers terminated by a negative integer and then sort it.I have done the sorting part and it works fine.But the part that the string continues to get integers unless a negative integer entered is causing problem. Here is my code which i wrote to get the string..Please tell me what is the problem with my code?

void getLine(string line)
{   
    getline(cin,line);
    for (int m = 0 ; m < line.length() ; m++)
    {
        if (str[k] == '-')
        {
                        line = line.substr(0,k-1);
            return;
        }
    }   
    getString(line);
}

Edited 3 Years Ago by pritaeas: Fixed formatting

well you can use getline with '-' as the delimiter so you don't need to check for the negative. also your function is returning void. are you sure you want to do that? I believe you would want to return the string to the function that called this function.

I did not get your point of using '-' as a delimiter.Can you please show me how would it be done in the code I wrote?I will really appreciate it. :)

the getline function uses a delimiter in when determining when to end the input. the default is '\n' but you can change it to whatever you want.

string getLine(string line)
{
	getline(cin, line, '-');
	return line;
}
This question has already been answered. Start a new discussion instead.