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);
}

Recommended Answers

All 4 Replies

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;
}

Thanks a million.It worked... cheers! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.