LOOP HELP

Aug 13, 2016 at 10:57pm
i want to add a loop that takes in 6 names and prints it out in a random order. i just need to add a loop somewhere.

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

int main() {

	const int g = 6;

	srand(time(0));

	string letters[6];

	

	for (int i = 0; i < g; i++) {
		int index = rand() % g;
		//swap letters[i] with letters[index]
		string temp = letters[i];
		letters[i] = letters[index];
		letters[index] = temp;
		
	}

	for (int i = 0; i < g; i++) {

		cout << letters[i] << endl;
	}


	
	system("pause");
	return 0;
}
Aug 14, 2016 at 12:14am
The for loop on lines 17-24: Looks to me like you wanted to shuffle the letters array. Change the loop variable to just go from 0 to 500 or maybe 0 to 1000, and I would consider renaming the loop variable to something like iteration. Then inside the loop create random integers i and j with rand() % g, then swap letters[i] with letters[j]. After 500 or 1000 iterations of swapping two random locations it should be shuffled.

Edit: g is not a very informative variable name; you should consider renaming it to something like letters_size.

Edit Edit: See Thomas1965's post about random_shuffle

Edit3: See JLBorges' post about shuffle
Last edited on Aug 14, 2016 at 4:23pm
Aug 14, 2016 at 10:29am
Aug 14, 2016 at 1:19pm
Good thinking Thomas1965, I had not noticed that the OP was already including <algorithm>.
Aug 14, 2016 at 2:00pm
Avoid std::random_shuffle - deprecated in C++14, removed in C++17
Use std::shuffle instead.

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
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <random>
#include <iterator>

int main() {

    const std::size_t N = 6;

    std::string names[N];

    // for each string str in names : http://www.stroustrup.com/C++11FAQ.html#for
    for( std::string& str : names ) std::cout << "name? " && std::cin >> str ; // or: std::getline( std::cin, str ) ;
    
    std::cout << "\n-------- names in random order ------------\n" ;

    // shuffle the names randomly: http://en.cppreference.com/w/cpp/algorithm/random_shuffle
    // http://en.cppreference.com/w/cpp/iterator/begin
    // http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
    std::shuffle( std::begin(names), std::end(names), std::mt19937( std::time(nullptr) ) ) ;
    
    for( const std::string& str : names ) std::cout << str << '\n' ;
}

http://coliru.stacked-crooked.com/a/6afdb8694951d845
Aug 14, 2016 at 5:46pm
Still Confused
Aug 14, 2016 at 6:04pm
Can you break down what particularly confuses you in the:
std::shuffle( std::begin(names), std::end(names), std::mt19937( std::time(nullptr) ) ) ;
Aug 14, 2016 at 6:10pm
im not sure if that was addressing my question. when i make an array of six names it prints out in random order everytime with no problem. i wanted to be able to take input of six names and print that out. and i cant tell if thats what their code does.
Aug 14, 2016 at 6:11pm
closed account (2UD8vCM9)
You could do something like
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
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;

int main() {

	const int g = 6;

	srand(time(0));

	string strings[6];

	cout << "Enter 6 strings." << endl;

	//get the strings from user input
	for (int i = 0; i < g; i++)
	{
		getline(cin, strings[i]); //Fill each string in the array with user input
	}

	cout << "Randomizing string order..." << endl;

	//Randomly swap the strings in the array to randomize string positions
	for (int i = 0; i < g; i++)
	{
		int index = rand() % g;
		swap(strings[i], strings[index]);
	}

	cout << "Printing strings..." << endl;

	//Print out the strings
	for (int i = 0; i < g; i++) {

		cout << strings[i] << endl;
	}

	system("pause");
	return 0;
}
Aug 14, 2016 at 6:20pm
Thanks Pindrought. but why didnt you use a while loop for the inputs.
Aug 14, 2016 at 7:12pm
closed account (2UD8vCM9)
The better question is why would you use a while loop for the inputs?

You only have an array of size 6, so that's why I used a for loop since you can only input 6 strings. If I used a while loop, it could go out of bounds.
Last edited on Aug 14, 2016 at 7:14pm
Aug 14, 2016 at 10:04pm
If I used a while loop, it could go out of bounds.

It would go out of bounds only if your condition is bad.

The while and for are almost the same:
1
2
3
4
5
6
7
8
9
10
11
12
int i = 0; // init
while ( i < g ) { // cond
  std::cout << strings[i] << '\n';
  ++i; // incr
}

// OR
for ( int i = 0; // init
      i < g; // cond
      ++i ) { // incr
  std::cout << strings[i] << '\n';
}


The choice between the two may depend on the tiny differences, or what impression the coder wants to give to the reader.

The 'for' tends to imply that we know how many times we will iterate.
The 'while' tends to imply that we do not know how many times we will iterate.

The ranged for syntax
for ( const auto & word : strings ) std::cout << str << '\n' ;
iterates exactly every item in a range (unless the body has a conditional break ...)
Aug 14, 2016 at 10:26pm
closed account (2UD8vCM9)
It would go out of bounds only if your condition is bad.


Hence why I said could. ^^

There is no reason to use a while loop just to copy what a for loop is already made to perform.
Topic archived. No new replies allowed.