(printf) Garbled Output.

Hi Guys!!

I have the below code:

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

#include<windows.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <random>
#include <string>
#include <iterator>
#include <algorithm>
#include <chrono>
using namespace std;


static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

int stringLength = sizeof(alphanum) - 1;

char genRandom()
{
    return alphanum[rand() % stringLength];
}

int main()
{
    srand(time(0));
    string Str;
    for (unsigned int i = 0; i < 20; ++i)
    {
        Str += genRandom();

        char nick[20];
        Str.copy(nick, 20, 0);

        printf(nick);

    }
    cout << Str << endl;
    Sleep(5000);
}



The console output is here:

b╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠bG╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠bGH╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠bGHF╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠bGHFk╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠bGHFkv╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠bGHFkvU╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠bGHFkvUw╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠bGHFkvUwD╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠bGHFkvUwDN╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ bGHFkvUwDNS╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
bGHFkvUwDNSge0!╠╠╠╠╠╠╠╠╠╠╠╠╠bGHFkvUwDNSge0!V╠╠╠╠╠╠╠╠╠╠╠╠bGHFkvUwDNSge0!V╠╠╠╠╠╠╠╠╠╠╠bGHFkvUwDNSge0!V1.729162E-307╠╠╠╠╠╠╠╠╠╠bGHFkvUwDNSge0!V1.729162E-307Z╠╠╠╠╠╠╠╠╠bGHFkvUwDNSge0!V1.729162E-307ZK╠╠╠╠╠╠╠╠bGHFkvUwDNSge0!V%EZK


The problem I am having is the the output from "printf(nick);" is garbled. I have included the output from the console. The desired character are at the tail end of the above console output. Any help and ideas will be warmly welcomed!!

Happy New Year!!

Googie.
Probably the issue is:
"The function does not append a null character at the end of the copied content."
http://www.cplusplus.com/reference/string/string/copy/

Simplest fix (not tested) is to zero-initialize the array.
char nick [20] { };

But there's still an issue when your string is 20 characters long, because there will be no room for the null char. I would make your array 21 characters long... or just avoid char arrays.
Last edited on
If you are going to copy a C++ string to a C string you should get the C string data of the C++ string and use strcpy or strncpy:
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
#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>

static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

int stringLength = std::strlen(alphanum);

char genRandom()
{
   return alphanum[rand() % stringLength];
}

int main()
{
   srand(time(0));
   std::string Str;

   for (unsigned int i = 0; i < 20; ++i)
   {
      Str += genRandom();

      char nick[21] { };
      std::strcpy(nick, Str.c_str());

      printf("%s\n", nick);

   }

   std::cout << '\n' << Str << '\n';

   Sleep(5000);
}

g
gJ
gJv
gJvU
gJvUk
gJvUkT
gJvUkTT
gJvUkTTH
gJvUkTTHQ
gJvUkTTHQH
gJvUkTTHQHg
gJvUkTTHQHgt
gJvUkTTHQHgtW
gJvUkTTHQHgtWD
gJvUkTTHQHgtWDo
gJvUkTTHQHgtWDod
gJvUkTTHQHgtWDod4
gJvUkTTHQHgtWDod41
gJvUkTTHQHgtWDod41b
gJvUkTTHQHgtWDod41br

gJvUkTTHQHgtWDod41br

Mixing C and C++ strings is fraught with potential problems. Use one or the other.

Pure C++ string manipulation/C++ random numbers is MUCH easier:
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
#include <windows.h>
#include <iostream>
#include <string>
#include <random>

char genRandom(const std::string& alphanum)
{
   // create a default C++ random engine, seeding it with random_device
   static std::default_random_engine rng(std::random_device {} ());

   // create a distribution from 0 to the size of the passed string
   static std::uniform_int_distribution<int> dis(0, alphanum.size() - 1);

   return alphanum[dis(rng)];
}

int main()
{
   std::string alphanum =
      "0123456789"
      "!@#$%^&*"
      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      "abcdefghijklmnopqrstuvwxyz";

   std::string Str;

   for (unsigned int i = 0; i < 20; ++i)
   {
      Str += genRandom(alphanum);

      std::cout << Str << '\n';
   }

   std::cout << '\n' << Str << '\n';

   Sleep(5000);
}

Why static in the function? So the random engine and distribution objects are not created with each function call. Repeated object creation is resource wasteful.


Without the custom function:
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 <windows.h>
#include <iostream>
#include <string>
#include <random>

int main()
{
   std::string alphanum =
      "0123456789"
      "!@#$%^&*"
      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      "abcdefghijklmnopqrstuvwxyz";

   // create a default C++ random engine, seeding it with random_device
   std::default_random_engine rng(std::random_device {} ());

   // create a distribution from 0 to the size of the string
   std::uniform_int_distribution<int> dis(0, alphanum.size() - 1);

   std::string Str;

   for (unsigned int i = 0; i < 20; ++i)
   {
      Str += alphanum[dis(rng)];

      std::cout << Str << '\n';
   }

   std::cout << '\n' << Str << '\n';

   Sleep(5000);
}
Last edited on
Topic archived. No new replies allowed.