So trying to figure out a exercise questions and having a problem with a while loop in it. What I need the loop to do is while the user is inputing words the loop will run and make all the words lowercase and store each word in the vector. The problem im having is I dont know how to stop the loop and move on with the code when the user types "stop".
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
usingnamespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> text;
string word;
// Prompt user to enter 16 words or more and then converts all words to lowercase letters.
// The loop stops when the user enters "stop" <--- Need help with this part
cout << "Please enter some words (Must be longer then 16 words) : " << endl;
while (cin >> word)
{
if(word == "stop")
{
// What to put so when the input = "stop" the loop stops and moves on?
}
else
{
for(string::size_type index = 0; index != word.size(); ++index)
word[index] = tolower(word[index]);
text.push_back(word);
}
}
Thanks in advance for the help and let me know if im going about this all wrong and there is a better way to do it. Thanks
// Uncomment the lines I've added to see a solution:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <string>
usingnamespace std;
#include <tchar.h>
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
vector<string> text;
string word;
// Prompt user to enter 16 words or more and then converts all words to lowercase letters.
// The loop stops when the user enters "stop" <--- Need help with this part
cout << "Please enter some words (Must be longer then 16 words):\n";
while (cin >> word)
{
//transform(word.begin(), word.end(), word.begin(), tolower);
if (word == "stop")
{
// the keyword you are looking for is break;
//break;
}
else
{
// this is equivalent to the transform up top
/*for(string::size_type index = 0; index != word.size(); ++index)
word[index] = tolower(word[index]);*/
text.push_back(word);
}
}
//cout << "To lowercase: ";
//copy(text.cbegin(), text.cend(), ostream_iterator<string>(cout, " " ));
//if( text.size() < 16 )
//{
// cout << "\nThat wasn't more than 16 words.";
//}
}
Also some more advice since I see you're using VS 2012:
When you create a new project, make sure that "Empty project" is selected under "Additional options" if you're just making a standard c++ application. That way you can avoid the extra clutter and the "stdafx.h" header.