Notices
Results 1 to 11 of 11

Thread: What's wrong wit my program? Please Help attempt #2

  1. #1 What's wrong wit my program? Please Help attempt #2 
    Forum Freshman
    Join Date
    Jan 2009
    Location
    new york
    Posts
    13
    I'm trying to find the longest word in the input. This code doesn't work. Can you please tell me what I'm doing wrong or missing? Thanks!!!

    My code:

    // 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()
    {
    cout <<"Input some strings." << endl;

    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;
    // clear longest vector
    longest.clear();
    // add new longest in vector
    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;

    }



    [/u]


    Reply With Quote  
     

  2.  
     

  3. #2  
    WYSIWYG Moderator marnixR's Avatar
    Join Date
    Apr 2007
    Location
    Cardiff, Wales
    Posts
    5,810
    again, what language ?


    "Reality is that which, when you stop believing in it, doesn't go away." (Philip K. Dick)
    Reply With Quote  
     

  4. #3  
    Forum Ph.D. Darius's Avatar
    Join Date
    Jan 2009
    Posts
    817
    Um. This is in C++. Shouldn't that be obvious with the use of cout <<?
    Om mani padme hum

    "In dishonorable things we are not bound to obey any man." - The Book of the Courtier [1561], pg 99 (144 in pdf)
    Reply With Quote  
     

  5. #4  
    WYSIWYG Moderator marnixR's Avatar
    Join Date
    Apr 2007
    Location
    Cardiff, Wales
    Posts
    5,810
    only if you happen to be familiar with C++, which i'm not
    "Reality is that which, when you stop believing in it, doesn't go away." (Philip K. Dick)
    Reply With Quote  
     

  6. #5  
    Forum Ph.D. Darius's Avatar
    Join Date
    Jan 2009
    Posts
    817
    I am familiar enough with the syntax to know what his program is doing but not enough to give a proper answer. Sorry.
    Om mani padme hum

    "In dishonorable things we are not bound to obey any man." - The Book of the Courtier [1561], pg 99 (144 in pdf)
    Reply With Quote  
     

  7. #6  
    Forum Freshman
    Join Date
    Jan 2009
    Location
    new york
    Posts
    13
    Yes it's c++ again sorry
    Reply With Quote  
     

  8. #7  
    WYSIWYG Moderator marnixR's Avatar
    Join Date
    Apr 2007
    Location
    Cardiff, Wales
    Posts
    5,810
    see my reply in the other thread
    "Reality is that which, when you stop believing in it, doesn't go away." (Philip K. Dick)
    Reply With Quote  
     

  9. #8 ditto 
    Forum Freshman
    Join Date
    Jan 2009
    Location
    new york
    Posts
    13
    see reply in my other thread ^ ^
    Reply With Quote  
     

  10. #9  
    Forum Freshman
    Join Date
    Feb 2009
    Location
    Yangon
    Posts
    9
    Hello, I am very proud to solving your problem. However, would you mind to tell me the algorithm of your program. Is your program to find the longest word in a string or find the longest string from input,etc. Please,reply about this.
    Reply With Quote  
     

  11. #10  
    Forum Freshman
    Join Date
    Jan 2009
    Location
    new york
    Posts
    13
    Quote Originally Posted by Steve_Mauna
    Hello, I am very proud to solving your problem. However, would you mind to tell me the algorithm of your program. Is your program to find the longest word in a string or find the longest string from input,etc. Please,reply about this.
    It is to find the longest string from an input !
    Reply With Quote  
     

  12. #11  
    Suspended
    Join Date
    Apr 2008
    Posts
    2,176
    Quote Originally Posted by ggordita
    Quote Originally Posted by Steve_Mauna
    Hello, I am very proud to solving your problem. However, would you mind to tell me the algorithm of your program. Is your program to find the longest word in a string or find the longest string from input,etc. Please,reply about this.
    It is to find the longest string from an input !
    You are probably going to have to create separate loops. Or public and private variables. That are evaluated each time you run over them.

    The compiler plays dumb. Or does not take and process information in the order you enter it. Except for "cout and cin".
    It just does some routine on the information and spits out something. If you want something else. You have to force it to analyze the information the way you want it to be analyzed.

    If you are not enjoying "C" and I don't see how anyone could. You will have to go through the rather hard to remember nonsense every time you pick it up. I will not stick "C" into my mind or memory as something important or good. Every time I write a "C" program, I cannot wait for the memories to go away.

    It is the compilers, they are not smart. Or at least not smart enough to follow the order of entry. They claim it is because the order could cause a huge program to be written. And I agree.

    However, if there was an agreed upon, form of entry and a more common knowledge of how the compiler worked, a really great language could be created. There would also be little security from violent minority types. Ha-ha.

    At least that is how I feel about it.


    Sincerely,


    William McCormick
    Reply With Quote  
     

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