Despot Despondency
November 16th, 2008, 05:18 PM
Hi, I'm just trying to write a simple program to calculate the length of the smallest string in the input. However, when I try to read the strings in a while loop the loop never ends. Here's my code
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
cout << "Please enter your text: ";
typedef string::size_type string_size;
vector<string_size> word_lengths;
string x;
while(cin >> x)
word_lengths.push_back(x.size());
sort(word_lengths.begin(),word_lengths.end());
cout << "The smallest word in the text was of length " << word_lengths[0] << endl;
return 0;
}
Any help would be appreciated. Cheers.
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
cout << "Please enter your text: ";
typedef string::size_type string_size;
vector<string_size> word_lengths;
string x;
while(cin >> x)
word_lengths.push_back(x.size());
sort(word_lengths.begin(),word_lengths.end());
cout << "The smallest word in the text was of length " << word_lengths[0] << endl;
return 0;
}
Any help would be appreciated. Cheers.