String Loops

How do you code a loop so it recalls information from the last time it ran through
i.e first time the longest sring is 28 characters long, second time the longest string is 14 characters long. How can I get it so it gets the information from the first runthru?
Store it in a variable?
I did, I thought that
1
2
3
4
5
6
7
8
9
10
11
12
13

      if (str1.length() > str2.length() && str1.length() > str4.length())
      {
         longest = str1;
      }
      if (str2.length() > str1.length() && str2.length() > str4.length())
      {
         longest = str2;
      }
      if (str4.length() > str1.length() && str4.length() > str2.length())
      {
         longest = str4;
      }     

would work but evidently not
So your trying to find the longest length of some strings?
Last edited on
yep. str1 is a string, str2 is a string, then str4 is a concatenation...so by definition str4 is going to be the largest in any given situation
;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<string> Str = {"Hello", "wow", "interesting", "salmon", "flipper", "there", "on", "american", "boat"};

    int Biggest = 0;

    for(int x = 0; x < Str.size(); x++)
    {
        if(Str[x].size() > Biggest) Biggest = Str[x].size();
    }

    cout << "The longest string is " << Biggest << " characters long."<< endl;

    return 0;
}
It took some playing around with your code with the (type) array command. This is the final & correct code
1
2
3
4
5
 for(int x = 0; x < (int)str4.length(); x++)
      {
         if((int)str4.length() > biggest) 
            biggest = str4.length();
      }
Last edited on
Glad I could help :)
Topic archived. No new replies allowed.