String class problem

Hi,

I try to learn string class, resolving some problem, but i have some dificulties.
The is ok, but when i print the longest string it does'n print on the same line.

I enter the number of string, after that i enter the first string until i introduced from keyboard "#" character. I enter the second string and so on.
Look at these example :
For i = 3;

Text[0] : I learn class String#
Text[1] : I dont learn class String#
Text[2] : String#

It print me like that :

Text[1] :
I dont learn class String


More than that look at the next example :

For i = 3;

Text[0] : I learn class String#abcdef
Text[1] : I dont learn class String#
Text[2] : String#

You see that in the first sentence i have continue to introduce some characters after # character and look what is happened :


Text[1] : abcdef
I dont learn class String



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<string>

using namespace std;

int main()
{
    string text[100], cuvant;
    int i, j, m, lung = 0;
    cout << "\n Give a value to i : ";
    cin >> i;
    cout << "\n Enter a text : \n";
    for(j=0; j<i; j++)
    {
        cout << "\n Text["<< j <<"] : ";
        getline(cin, text[j], '#');
        if(text[j].length() > lung)
        {
            lung = text[j].length();
            m = j;
            cuvant = text[j];
        }
    }
    cout << "\n Longest string : \n ";
    cout << "\n Text["<<m<<"] : ";
    cout << cuvant << endl;
    return 0;
}
Line 16: You specified # as the delimiter. That means that cin reads only until it finds a # character. The characters after the # are left in the cin buffer.

When you do the next cin, it will start with the next character in the buffer (the characters after the #). To avoid this, call cin.ignore (1024) to discard any remaining characters in the cin buffer before you invoke the next cin statement.
http://www.cplusplus.com/reference/istream/istream/ignore/
> call cin.ignore (1024) to discard any remaining characters in the cin buffer
¿is that the size limit of the input buffer?
¿or is the user too eager to press <Return>?
The default delimiter is EOF. You should specify '\n' as the delimiter by std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Topic archived. No new replies allowed.