0

When I try to make integers using the rand() function, I cannot include them in a string. These are the two codes that I have tried:

 int x1, x2;
    x1 = (rand() % 14 + 2);
    x2 = (rand() % 14 + 2);

    char c1 = char(x1);
    char c2 = char(x2);

    string Hand = {c1, c2};

    cout << Hand << endl;

I don't get any errors with this one, but it doesn't run. This is the one I thought would be correct:

int c1, c2;
    c1 = (rand() % 14 + 2);
    c2 = (rand() % 14 + 2);

    to_string(c1);
    to_string(c2);

    string Hand = {c1[0], c2[0]};

    cout << Hand << endl;

Yet it isn't. This must be really simple. How is it done?

Alec
  • 8,529
  • 8
  • 37
  • 63
  • 2
    What did hope to get from using `string Hand = {c1, c2};`? – R Sahu Mar 28 '17 at 06:35
  • 1
    Now that your question has been answered,I'd really recommend to read a book or a tutorial. You are missing some fundamental c++ principles. – MikeMB Mar 28 '17 at 06:53
  • Pick a book from the beginner section from http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Ferenc Deak Mar 28 '17 at 06:55
  • Give a few examples of what kind of output you expect. What is the goal? Describe a little context. A string/text with the digits of two random numbers? A string of length two, with two digits, from two random number the first digit each? A two-digit random number? A random number from the range 0-99? – Yunnosch Mar 28 '17 at 06:58
  • See [std::to_string](http://en.cppreference.com/w/cpp/string/basic_string/to_string) – Jesper Juhl Mar 28 '17 at 07:06
  • I just didn't realize that to_string didn't change the data type. – Alec Mar 28 '17 at 09:42
  • @alec935: There is no function in c++ that allows you to change the static datatype of a variable. That is what I meant with fundamental principles. Also your first example shows a lack of understanding for how `char` are working in c++. What is also not clear is why you create two random numbers between 2 and 15 inclusive and then only use the last digit of them (are you deliberately trying to slew the distribution in a particular manner). There is probably a simpler and / or better solution to your question, but for that people have to know what you try to achieve. – MikeMB Mar 29 '17 at 07:25

3 Answers3

2

Note that c1 and c2 are not vectors, so c1[0] make no sense. The to_string method returns a string, don´t turns the argument into one. That said, the code could work better like this:

int c1, c2;
 c1 = (rand() % 14 + 2);
 c2 = (rand() % 14 + 2);



    string Hand = {to_string(c2)[0], to_string(c1)[0]};

cout << Hand << endl;
  • thank you, this works. I didn't realize that to_string only returned the string value in that instance, and didn't convert it. – Alec Mar 28 '17 at 06:39
1

Try something like this:

#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <string>
#include <sstream>
using namespace std;

int main() {
    // your code goes here
    std::ostringstream ss;
    int x1, x2;
    x1 = (rand() % 14 + 2);
    x2 = (rand() % 14 + 2);
    ss << x1 << x2;
    std::string Hand = ss.str();

    cout << Hand << endl;

    return 0;
}
hasanzuav
  • 554
  • 5
  • 15
0

C++ is statically typed language. std::to_string won't change the type... It returns the string.

As for the first code, you're dealing with non-printable characters.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74