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