Help with a while loops

closed account (3qX21hU5)
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".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>

using namespace 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
You are doing very good! The keyword you are looking for is break.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Uncomment the lines I've added to see a solution:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <string>
using namespace 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 note that the loop will stop if the user enters CTRL+Z. This special character represents the end of a stream in the Windows shell.

http://en.wikipedia.org/wiki/End-of-file

Try it!
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.
Last edited on
Topic archived. No new replies allowed.