Just started C++ programing and doing an assingment for tafe.
Im making a program that does a few things but the part im stuck with is counting the number of longest lines entered so for e.g the user enters:
123
1234
1234
1234
12
I need it to be able to count that the number of longest lines is 3. I can make it count the longest line and get it to recognise that it would be 4 just cant get it to count the number of times it appears.
You are trying to count the number of the longest lines if I read that correctly.
You could use a string for the input variable and then get the length of the string. If the new string is longer than the any of the other inputted strings make count equal 1. I would try something like this
string str;
int longest = 0;
int count = 0;
while(true)
{
getline(cin, str);
if(str.length() > longest)
{
longest = str.length();
count = 1; // you have encountered one of the new longest strings
}
elseif(str.length() == longest)
{
count++;
}
// Make some function to break out of the loop
}
cout << "Longest line encountered = " << longest << endl;
cout << "It was encountered " << count << " time(s).\n";
return 0;