range for question

Write your question here.
Hello, I am trying to use a 'range for' to print the elements of a vector to the screen.

The first for loop came straight out of C++ Primer and works perfectly.

When I run the program, it works until it gets to the last for loop and completely stops when it try's to execute the last cout.

One question I have is that on the cin where I am using the while loop to input the elements from the keyboard, I will 1) type the last word, 2)hit enter to bring cursor down to the next line, then 3)hit Ctrl-break to stop the input loop. I'm not sure if this is the best way to do that or it could be the problem. Using the debugger, everything looks fine up to the last for loop.

Thanks for your help and I hope it makes sense.
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
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	string str("some string");

	// print the characters in str one character at a time
	for (auto c : str)
		cout << c << endl;


	// NOW GET CHARACTERS FROM KEYBOARD
	string myStr;
	vector<string> myV;
	cout << "Please enter strings: " << endl;
	while (cin >> myStr)
	{
		myV.push_back(myStr);
	}

	auto int2 = myV.size();

	for (auto i : myV)
	{
		cout << "The current value is:" << i << endl;
	}

	return 0;
}  Put the code you need help with here.
Try Ctrl+Z instead of Ctrl+Break.
Topic archived. No new replies allowed.