hope anyone can help something with this code, well this code for the exercise 3.4 in Accelerated C++ book...

where
3-4. Write a program to report the length of the longest and shortest string in its input.

I thought my code was gonna going good, but it seems that it end up crashed.
btw, I just test with finding the longest string first..

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::max;
using std::string;
using std::vector;

int main()
{
    // output a string literal for user to react
    cout << "Please insert some sort of word, include end-of-file: ";

    // receiving user input, put the input into element in container type.
    string user_input;
    vector<string> words;

    while (cin >> user_input)
        words.push_back(user_input);

    // define a type for storing number of element(unsigned int)
    typedef vector<string>::size_type num_ele;
    num_ele input_size = words.size();

    // checking out any input from user
    if (input_size == 0)
    {
        cout << "You insert nothing, please try again";
        return 1;
    }

    // pointing device as a successsful counter and starting point.
    num_ele success = 0;

    // loop for maximum.
    for (num_ele current_index = 1; current_index <= input_size; ++current_index)
    {
        // storing bigger amount of character
        unsigned int bigger_amount;

        // declaring amount of character in current words
        string current_word = words[current_index];
        unsigned int current_element_total = current_word.size();

        // declaring an amount that success; bigger, to be compared
        string success_word = words[success];
        unsigned int success_element_total = success_word.size();

        // comparing variable's character amount
        unsigned int successful = max(success_element_total, current_element_total);

        // if first number bigger;
        if (successful = success_element_total)
        {
            bigger_amount = success_element_total;
        }

        // if second number bigger;
        if (successful = current_element_total)
        {
            bigger_amount = current_element_total;
            success = current_index;
        }


        // output the largest number
        // the if functionality detect that this part is last element.
        if (current_index == input_size)
        {
            string biggest_words = words[success];
            cout << "The biggest string of character is, " << biggest_words <<
                    " where the word contain, " << bigger_amount << " amount of character";
        }
    }

    return 0;

}

thnx 4 read...

additional info:-

program runs good when I insert nothing.

when I insert 1 string, program output weird character on the console

when I insert more than 1 string, program just crashed "directly"

btw, I'm using code::blocks, if any case it's the compiler problem....

Edited 5 Years Ago by Vasthor: n/a

Line 40...

for (num_ele current_index = 1; current_index <= input_size; ++current_index)

Indexes start at 0, not 1, and they must be LESS THAN the array / vector size. You simply got lucky when it didn't crash when you entered one string. It could have. You got garbage because there IS no element 1. There is an element 0. That's what you needed to access.

Edited 5 Years Ago by VernonDozier: n/a

Line 40...

for (num_ele current_index = 1; current_index <= input_size; ++current_index)

Indexes start at 0, not 1, and they must be LESS THAN the array / vector size. You simply got lucky when it didn't crash when you entered one string. It could have. You got garbage because there IS no element 1. There is an element 0. That's what you needed to access.

nah, that's not work, hmm... this gone harder than I thought, hope others can give a hand too..

update:-

line 57 fixed (from '=' to '==')
line 63 fixed (from '=' to '==')

still crashing...

>> nah, that's not work, hmm...

I didn't say it was the only problem, but it's a problem you have to fix. If you're accessing elements that aren't there, use the "at" function instead of [] and then catch the out_of_range exception. If there are no exceptions to catch, you'll know the problem is elsewhere.

oh good, now I'm confuse, how can this code works fine? with similar build...
btw, the code function is to find amount of each distinct word from user input...

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::sort;
using std::string;
using std::vector;

int main()
{
    // Ask for and read the words
    cout << "Please enter a few words, followed by end-of-file: ";

    vector<string> words;
    string word;

    // Invariant: words contains all of the words read so far
    while (cin >> word)
        words.push_back(word);

    typedef vector<string>::size_type vec_sz;
    vec_sz size = words.size();

    // Check that the user entered some words
    if (size == 0)
    {
        cout << endl << "You didn't enter any words. "
        "Please try again." << endl;
        return 1;
    }

    // sort the words
    sort(words.begin(), words.end());

    string current_word;
    int count;

    // Set the initial word to the first word in the vector
    current_word = words[0];

    // Set the initial count for the first word
    count = 1;

    // Invariant: we have counted current_index of the total words
    // in the vector
    for (vec_sz current_index = 1; current_index < size; ++current_index)
    {
        // Report the count for the current word if it does not match
        // the word at the current index in the vector, and reset the
        // count to zero so that it will one when the variable is
        // incremented outside the if statement.
        if (current_word != words[current_index])
        {
            cout << "The word \"" << current_word << "\" appears "
            << count << " timesssssssss." << endl;

            current_word = words[current_index];
            count = 0;
        }

        ++count;
    }

    // Report the count for the final word
    cout << "The word \"" << current_word << "\" appears "
        << count << " times." << endl;

    // We have reported the count of all the words in the vector, so exit.
    return 0;
}

btw, the 'at' thing is not taught yet until chapter 3, thnx 4 reply anyway...

update :-

does the cause of problem possibly from this code, if so, how to fix them?

where, at code for "comparing variable's character amount" does it even valid for compiler to read, because I just understand that max return the higher value, but don't know what type its returned.

// comparing variable's character amount
        unsigned int successful = max(success_element_total, current_element_total);

        // if first number bigger;
        if (successful == success_element_total )
        {
            bigger_amount = success_element_total;
        }

        // if second number bigger;
        if (successful == current_element_total)
        {
            bigger_amount = current_element_total;
            success = current_index;
        }

btw, 20 hours gone without reply, and I'm just at chapter 3 / 16, ugh, this not gonna be easy, T_T"

>> btw, 20 hours gone without reply

I don't know about anyone else, but I for one didn't know HOW to reply. It sounds like the code works now, or if it doesn't work, you don't say where. If you want help you need to clearly state precisely what is going on and what is wrong and where. The quote below is too vague.

>> oh good, now I'm confuse, how can this code works fine? with similar build...


And I told you what was wrong, or at least one thing, and it seems like you sort of blew me off.

ermm, let me check that...

arh my bad, thnx for fast reply, I think I need to re-read people post from now on *.*

Indexes start at 0, not 1, and they must be LESS THAN the array / vector size. You simply got lucky when it didn't crash when you entered one string. It could have. You got garbage because there IS no element 1. There is an element 0. That's what you needed to access.

lol, even you capitalise the LESS THAN I'm remember seeing it before... xD

after change the "<=" into "<" at line 40, the programs runs, but after input end-of-file symbol, it does nothing but return 0, maybe because the changes affect the expected result, need re-coding this program it seems, because, as shown in the code, the "success" code which is bigger string will be hold until it meet a bigger one and can't work the same anymore thanx to this vector picky problems...

=3="

This question has already been answered. Start a new discussion instead.