C++ Code Help please, Longest string?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: C++ Code Help please, Longest string?

  1. #1
    Join Date
    Jan 2009
    Posts
    2

    Question C++ Code Help please, Longest string?

    Hi, so I'm having problems finding the longest word in a given input. This code doesn't work, and I'm pretty stuck ; ; I want to include the size(); or length(); operations, but I'm having trouble. What am I doing wrong and what am I missing? Please HELP!


    // includes
    #include <iostream>
    #include <string>
    #include <vector>

    // using statements
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    using std::vector;

    int main()
    {
    vector<string> longest;
    string s;

    typedef vector<string>::size_type str_sz;
    str_sz str_length;
    str_sz str_longest;


    // read input
    cin >> s;

    while(cin >> s)
    {
    // store strings in vector
    longest.push_back(s);

    if (str_length == str_longest)
    {
    longest.push_back(s);
    }
    // find the longest string
    else if(str_length > str_longest)
    {
    str_length = str_longest;
    longest.clear();
    longest.push_back(s);
    }
    }
    //print longest string

    cout << "Longest string is: " << endl;

    for (str_sz i = 0; i < longest.size(); ++i)
    {
    cout << longest[i] << endl;
    }

    return 0;

    }

  2. #2
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: C++ Code Help please, Longest string?

    should be

    Code:
    getline(cin, s);
    Thanks for your help.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,446

    Re: C++ Code Help please, Longest string?

    Please use code tags when posting code.

    Quote Originally Posted by ggordita View Post
    Hi, so I'm having problems finding the longest word in a given input. This code doesn't work, and I'm pretty stuck ;
    Have you used your compiler's debugger?
    I want to include the size(); or length(); operations, but I'm having trouble.
    You don't randomly throw things together. You must know each and every step of what you're doing.
    What am I doing wrong and what am I missing?
    First, you should learn to diagnose your own problems by learning to use the debugger. It is a mandatory tool to learn if you want to write any program and diagnose errors.
    Code:
    if (str_length == str_longest)
    So what is the value of str_longest and str_length the first time this statement is executed? It isn't any known value, since you didn't initialize them. They could be 0, 435, -4323, 32432432, who knows. Variables don't magically get their values, you have to set them to something.

    Regards,

    Paul McKenzie

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: C++ Code Help please, Longest string?

    Quote Originally Posted by Peter_APIIT View Post
    should be

    Code:
    getline(cin, s);
    Not if he's interested in word length, it shouldn't.

    One problem I do see is that you're adding s to the "longest" vector twice in one case. I doubt that's what you want. Otherwise, it's a fairly good approach to the problem once you replace those meaningless variables with the appropriate member functions.

  5. #5
    Join Date
    Jan 2009
    Posts
    2

    Re: C++ Code Help please, Longest string?

    I'm sorry I'm new to c++,!

  6. #6
    Join Date
    Apr 1999
    Posts
    27,446

    Re: C++ Code Help please, Longest string?

    Quote Originally Posted by ggordita View Post
    I'm sorry I'm new to c++,!
    That really isn't an excuse to not know to initialize variables to a value. That should have been covered long before you attempted this assignment.

    When learning C++, you must understand everything you've learned up to the point you write the program. If the C++ book you're using discusses variables, how they're assigned, etc. then you must understand everything about that before writing a program using that concept. This means understanding all the example programs given to you before you start writing your own.

    You're using vectors, but do not know about assigning variables. Using STL vectors assumes you know the basics of assigning variables.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: C++ Code Help please, Longest string?

    Your approach is, erm, sub optimal. You donīt need a vector at all, simply store the longest string so far. Furthermore youīre comparing strings lexicographically instead of comparing string lengths:

    Code:
    str_length > str_longest
    is most probably not what you wanted to do.

    All you need is something like this:

    Code:
    std::string Candidate = "";
    std::string Input;
    while( getline( cin, Input ) )
    {
       if( Input.length() > Candidate.length() )
       {
          Candidate = Input;
       }
    }

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: C++ Code Help please, Longest string?

    Still thinking getline() is the wrong tool here.

  9. #9
    Join Date
    Feb 2009
    Posts
    56

    Re: C++ Code Help please, Longest string?

    I totally agree!
    Because getline surely will fail on long scripts, I would pick looping till I reach the end of line

  10. #10
    Join Date
    Feb 2009
    Posts
    1

    Resolved Re: C++ Code Help please, Longest string?

    Hi ggordita. I have been working on finding the longest string, and on finding the shortest string also, at the same time. Hope this code helps!
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center