program outputs word vertically

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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;

int main()
{
    vector<string> words;
    words.push_back("hello");
    words.push_back("computer");
    words.push_back("book");
	

	srand(static_cast<unsigned int>(time(0)));
	random_shuffle(words.begin(), words.end());
	string RAND_WORD = words[0];

	for(int i = 0; i < RAND_WORD.length(); ++i)
	{
		cout << RAND_WORD[i] << endl;
	}

    cin.get();
    cin.get();
}

Why does the program I made out the random word vertically?
It outputs like this:
h
e
l
l
o
It's because of the endl on line 22.

Move it outside the loop:

1
2
3
4
5
	for(int i = 0; i < RAND_WORD.length(); ++i)
	{
		cout << RAND_WORD[i];
	}
	cout << endl;


(your code is saying. print a char, newline, print next char, newline, ...)

Andy

PS Are you using Visual C++ ??
Last edited on
Yes, sorry for the late reply. I forgot about this post, and why?
Topic archived. No new replies allowed.