For loop and string help

The first for loop is to read through the sentence, but i want to have another one for loop within it that can read through each word. Im not sure how to do that.
By the way, im trying to make a pig latin translator.

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
#include <iostream>
#include <string>


using namespace std;




int main() {
	
	string word;
	string y = "ay";
	cout << "Enter a word" << endl;
	getline(cin, word);
	
	for (int i = 0; i < word.length(); i++) {
		
	}
	
	
	system("pause");
	return 0;
}

1
2
3
4
5
6
7
8
9
std::vector<std::string> words{};
for( std::string word{}; std::cin >> word; ) {
    if( word == "quit" ) break;
    words.push_back( word );
}

for( std::string& w : words ) {
    // translate
}
Im not exactly sure how that works. I was thinking more along the lines of this.

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
#include <iostream>
#include <string>
#include <cctype>
#include <ctime>

using namespace std;




int main() {
	
	string vowels = ("a", "e");
	string y = "ay";
	string word;
	cout << "Enter a word" << endl;
	getline(cin, word);
	
	for (int i = 0; i < word.length(); i++) {
		if (word[0] = vowels) {
			cout << word.substr(1) + y;

		}
		
	}
	
	
	system("pause");
	return 0;
}

1
2
3
4
for( std::string word{}; std::cin >> word; ) {
    if( word == "quit" ) break;
    words.push_back( word );
}

is functionally equivalent to
1
2
3
4
5
6
std::string word{};
while( std::cin >> word ) {
    if( word == "quit" ) break;
    words.push_back( word );
    word.erase( );
}


1
2
3
for( std::string& w : words ) {
    // translate
}

This is known as a range based for loop.
http://en.cppreference.com/w/cpp/language/range-for
It is equivalent to
1
2
3
for( std::size_t i{}; i < words.size( ); i++ ) {
    // translate
}


if (word[0] = vowels)
Use two equal signs for equality comparison.

Looks like your code only translates a word rather than a sentence.
Topic archived. No new replies allowed.