SORTING HELP

I want to be able to have someone enter a word and have those same letters printed out in a random order. Any ideas would be helpful.

closed account (48bpfSEw)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "stdafx.h"  // Microsoft Visual Studio

#include <vector>
#include <iostream>

#include <ctime>

#include <windows.h>
#include <conio.h>

#include <algorithm>

char getKey(void) {
	DWORD        mode;
	HANDLE       hstdin;
	INPUT_RECORD inrec;
	DWORD        count;
	char         result = '\0';

	/* Set the console mode to no-echo, raw input, */
	/* and no window or mouse events.              */
	hstdin = GetStdHandle(STD_INPUT_HANDLE);
	if (hstdin == INVALID_HANDLE_VALUE
		|| !GetConsoleMode(hstdin, &mode)
		|| !SetConsoleMode(hstdin, 0))
		return result;

	FlushConsoleInputBuffer(hstdin);

	/* Wait for and get a single key PRESS */
	do ReadConsoleInput(hstdin, &inrec, 1, &count);
	while ((inrec.EventType != KEY_EVENT) || !inrec.Event.KeyEvent.bKeyDown);

	/* Remember which key the user pressed */
	result = inrec.Event.KeyEvent.uChar.AsciiChar;

	/* Wait for and get a single key RELEASE */
	do ReadConsoleInput(hstdin, &inrec, 1, &count);
	while ((inrec.EventType != KEY_EVENT) || inrec.Event.KeyEvent.bKeyDown);

	/* Restore the original console mode */
	SetConsoleMode(hstdin, mode);

	return result;
}


// get key strokes until return-key
void getLine(std::string &strInput) {
	for (char c = getKey(); c != 13; c = getKey()) {
		std::cout << c;
		strInput += c;
	}
}


int GetRandom(int iMax)
{
	int iRandom;
	srand(static_cast<unsigned int>(time(NULL) * clock()));
	iRandom = (rand() / (RAND_MAX / iMax + 1));

	return iRandom;
}

int main()
{	
	std::cout << "Input: ";

	std::string strInput;
	getLine(strInput);
	std::cout << std::endl;

	int iLen = strInput.size();
	std::vector<int>	vNum;	// unique random numbers
	while (vNum.size() < iLen) {
		int iRandom = GetRandom(iLen);
		if (std::find(vNum.begin(), vNum.end(), iRandom) == vNum.end())
			vNum.push_back(iRandom);
	}

	std::cout << "Output: ";
	for (std::vector<int>::iterator it = vNum.begin(); it != vNum.end(); ++it)
		std::cout << strInput[*it];

	std::cout << std::endl;


	std::cout << "press any key";
	getKey();

	return 0;
}
That doesnt really help
keskiverto's suggestion, to use the shuffle algorithm (or maybe the older random_shuffle), would be the way to do it in practice.

If this is some sort of exercise, you could use rand() -- or one of the newer random number generators from <random> -- to randomly select letters yourself (tracking which ones you're already selected.)

What elements of C++ have you covered so far?

Andy
For now, I'm learning this on my own. But I just started with the random function and I'm pretty good with the basics like loops and if else kinda stuff.
closed account (48T7M4Gy)
That doesnt really help

Cheeky! So where do you need help or is that to be one of life's great mysteries?

keskiverto and Necip have gone way beyond what you would get from a lecturer or teacher and that's the best you can come up with?

What have you done? What ideas have you got?

I reckon the best idea is for you to have a look at the tutorials here.
Here it is all over again:


http://www.cplusplus.com/forum/beginner/195835/

@fivestar

Why have you started a new topic for this? All these experienced people have helped you quite a bit, so now you waste their time by doing this? A quick way of getting off-side.
Relax. I was using that topic to see if that would help me get an idea of how to approach this problem.
Topic archived. No new replies allowed.