Numbers outputting differently

Ok so in my program I have 3 random numbers being created and each number need to be seperately used to create pet characteristics and then they are all lined up to create the ID number so when the player loads the game again they can get the same pet. Now the problem is, is that the numbers outputted in num1 num2 and num3 are different than in the pet ID, so three random numbers are created and they are put together as one ID but that isnt working, here is one output i got

number1 2
number2 0
number3 2
Pet ID Number: 148 <-- this should be 202

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
void PetMaker(Player &player, Pet &pet)
{
    int numbers, num1, num2, num3, sum;

    srand(time(0));

    //Create random ID numbers
    num1 = numbers = rand() % 8;
    num2 = numbers = rand() % 2;
    num3 = numbers = rand() % 6;

    //Output random numbers for debugging
    cout << "Number1 " << num1 << endl;
    cout << "Number2 " << num2 << endl;
    cout << "Number3 " << num3 << endl;

    //Put the 3 numbers in a line
    numbers = num1 + '0' + num2 + '0' + num3 + '0';

    //Assign the numbers to playerPetID
    pet.playerPetID = numbers;

    //Output pet id for debugging
    cout << "Pet ID Number: " << pet.playerPetID << endl;

    cout << '\n';

    //This switch will create the first set of animal traits
    //based on the numbers
    switch(num1)
    {
        case 0:
            cout << "" << endl;
    }
}
Maybe it's a bit late now but let's see what you did there:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int numbers; // This is just one field holding one number (just because u named it numbers instead of num doesn't mean it can now hold more than one number ^.^

num1 = numbers = rand() % 8;
// this is what you do here:
numbers = rand() % 8; //numbers holds now your random number
num1 = numbers; // now num1 holds your random number and numbers still does as well

//What you wanna do is just:
num1 = rand() % 8;  //The obove is just one assignment more you do to "numbers" that you're doing for no reason

numbers = num1 + '0' + num2 + '0' + num3 + '0';
// this is doing the following '0' is a character and when you add something like this
// num1 + '0' it doesn't append 0 to the number in num1 it actually calculates an addition in the following way:
// it looks into an ascii table and sees '0' is represented by the number 48
// This means our expression looks like this:
numbers = 2 + 48 + 0 + 48 + 2 + 48  // considering num1 = 2, num2 = 0 and num3 = 2
// => numbers = 148 


Summary: this approach is not going to work this way
You have to use a string (or char Array) as pet ID number
and build your ID out of your integer variables into a string and pass that to pet.playerPetID
Last edited on
ok awesome thank you i got it working, i had no idea it was actually adding 48 to it!! but all i really did was change pet id from an int to string and the num 1 2 3 and number to string so how does C++ treat that differently from the int? Also when i output num 1 2 3 it shows like this

0
01
013

i need to just get one number at a time because im going to feed each number into its own switch statement, unless there is a string function or something i can use to just search through each string and get each number without having to go through that hassle of separating them and then joining them again, then i wouldnt even need num 1 2 3. so how would i do that??
Last edited on
Well there are lots of different implementations of the "+"-operator depending on what datatypes or classes or structs you want to "add"

1
2
3
4
5
6
7
8
9
10
int b = 5 + 'A'; //since there is a constant number "5" involved 
//it calculates 5 + 65 (65 = ascii dec for A)

std::string str = 5 + "abc"; // this is undefined nobody programmed what it is supposed to do now 
//(has weird undefined behaviour
std::string str2 = "abc";
str2 = '0' + str2; // this is not looking up the decimal number for 0 it just takes the "0" and 
// appends whatever is in str2
// => str2 should be now: 0abc


What you're supposed to understand is that + doesn't always mean the same. It depens on what is left and right from +. ^.^
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std;

int main() {
	int num1 = 2;
	int num2 = 0;
	int num3 = 2;
	
	int total = num1*100 + num2*10 + num3;
	std::cout << total << std::endl;
}
Topic archived. No new replies allowed.