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...