Special character and number chars printing as weird values

I am passing chars into a string and jumbling the letters in the string using a function however the chars grabbed from the arrays at the top of main are printing as an accented u for symbols and a % sign for numbers, the letters however are unaffected. Help appreciated :)

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

using namespace std;

void scrambleString(string str)
{
	int x = str.length();
	for (int y = x; y > 0; y--)
	{
		int pos = rand() % x;
		char tmp = str[y - 1];
		str[y - 1] = str[pos];
		str[pos] = tmp;
	}
	cout << str;
}

int main()
{
	// Arrays for RNG to work with //
	char letters[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
		'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
		's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

	char symbols[] = { '!', '£', '$', '%', '&', '*', '@', '~' };

	char numbers[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	// Other Variables //
	int letterIn;
	int specialIn;
	int numberIn;
	string randomPass;
	string holdString;

	cout << "No of Letters:  ";
	cin >> letterIn;
	cout << "No of Special char:  ";
	cin >> specialIn;
	cout << "No of Numbers:  ";
	cin >> numberIn;

	// Letters
	for (int i = 0; i < letterIn; i++)
	{
		int RNG = rand() % 26;
		char holdChar = letters[RNG];
		// Convert to String
		holdString.insert(i, 1, holdChar);

	}
	// Symbols
	for (int i = 0; i < specialIn; i++)
	{
		int RNG = rand() % 8;
		char holdChar = symbols[RNG];
		// Convert to String
		holdString.insert(letterIn, 1, holdChar);
	}
	// Numbers
	for (int i = 0; i < numberIn; i++)
	{
		int RNG = rand() % 8;
		char holdChar = symbols[RNG];
		// Convert to String
		holdString.insert(letterIn+specialIn, 1, holdChar);
	}
	cout << holdString << endl;
	scrambleString(holdString);
}
Hello KingpinKeys,

Starting you question with a green check on the thread tell the others that you have an answer and are finished. I am not sure if you can edit your message and change the icon or if it would be better to delete this and re-post without the green check.

What I have noticed is that when the British pound "£" is stored in the array it shows a numerical value of "-93" which by my ASCII table should be "163". So I am thinking that when the program prints to the screen the "-93" is printing the "ú" that you and I are seeing in the output. Not sure how to fix this, but maybe someone who does will see this.

Sorry not much help right now,

Andy
The table I looked at says
163 is funny u
and
156 is brit pound "L"

at a guess the text editor you are using and the ascii code are not matching, so when you compile it, you get a different result than expected. Maybe your machine has a locale setting that is converting in the text editor...?

the fix is to do this:

char symbols[] = { '!', 156, '$', '%', '&', '*', '@', '~' };
Last edited on
Hello KingpinKeys,

After some testing I arrived at the same answer as jonnin suggested.

If you have not found it yet the reason your numbers look wrong is because the for loop for numbers is a 99% copy of the for loop for symbols. Look at what needs to be changes in lines 64 and 65.

Hope that helps,

Andy
Topic archived. No new replies allowed.