Results 1 to 13 of 13
  1. #1
    alvaritos's Avatar
    Join Date
    May 2008
    Gender
    male
    Posts
    234
    Reputation
    9
    Thanks
    73
    My Mood
    Amazed

    C++ Array [Solved]

    Hello I have this code
    Code:
    srand ( time(NULL) );
    	int bad1 = rand() % 9 + 1;
    	int bad2 = rand() % 9 + 1;
    	int bad3 = rand() % 9 + 1;
    	int position = rand() % 9 + 1;
    	int table1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table4[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	table1[position] = position;
    	table2[bad1] = bad1;
    	table3[bad2] = bad2;
    	table4[bad3] = bad3;
    But I prefer to use chars for example

    Code:
    table1[position] = "D";
    But that gives me an error.. any help to use char on a int array?

  2. #2
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,676
    My Mood
    Mellow
    "D" isn't a char. 'D' is. Double quotes mean a char array (string), regardless of whether there is only 1 char in the array.

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  3. #3
    alvaritos's Avatar
    Join Date
    May 2008
    Gender
    male
    Posts
    234
    Reputation
    9
    Thanks
    73
    My Mood
    Amazed
    So should be
    Code:
    table2[bad1] = 'D';
    or ? didnt understand some wordscause im not english.. can you explain?

  4. #4
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    Quote Originally Posted by alvaritos View Post
    So should be
    Code:
    table2[bad1] = 'D';
    or ? didnt understand some wordscause im not english.. can you explain?
    Yes. That's right.

  5. #5
    alvaritos's Avatar
    Join Date
    May 2008
    Gender
    male
    Posts
    234
    Reputation
    9
    Thanks
    73
    My Mood
    Amazed
    Code:
    	srand ( time(NULL) );
    	char *num;
    	int bad1 = rand() % 10;
    	int bad2 = rand() % 9 + 1;
    	int bad3 = rand() % 9 + 1;
    	int position = rand() % 9 + 1;
    	int table1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table4[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	table1[position] = position;
    	table2[bad1] = 'D';
    	table3[bad2] = bad2;
    	table4[bad3] = bad3;
    Using that code just gives me all numbers and the letter 'D' becomes a big number ex : 68 ...

  6. #6
    Nico's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Location
    Germany :D
    Posts
    15,918
    Reputation
    1121
    Thanks
    8,617
    Quote Originally Posted by alvaritos View Post
    Code:
    	srand ( time(NULL) );
    	char *num;
    	int bad1 = rand() % 10;
    	int bad2 = rand() % 9 + 1;
    	int bad3 = rand() % 9 + 1;
    	int position = rand() % 9 + 1;
    	int table1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table4[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	table1[position] = position;
    	table2[bad1] = 'D';
    	table3[bad2] = bad2;
    	table4[bad3] = bad3;
    Using that code just gives me all numbers and the letter 'D' becomes a big number ex : 68 ...
    Yea, because that's the ascii value of that char.

  7. #7
    alvaritos's Avatar
    Join Date
    May 2008
    Gender
    male
    Posts
    234
    Reputation
    9
    Thanks
    73
    My Mood
    Amazed
    How I convert it to letter?

  8. #8
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    Quote Originally Posted by alvaritos View Post
    How I convert it to letter?
    Use the char data type instead of int.

    e.g; [highlight=c++]char lalala = 'D';[/highlight]
    Last edited by Hassan; 09-08-2011 at 11:18 PM.

  9. #9
    alvaritos's Avatar
    Join Date
    May 2008
    Gender
    male
    Posts
    234
    Reputation
    9
    Thanks
    73
    My Mood
    Amazed
    Code:
    srand ( time(NULL) );
    	char num = 'D';
    	int bad1 = rand() % 10;
    	int bad2 = rand() % 9 + 1;
    	int bad3 = rand() % 9 + 1;
    	int position = rand() % 9 + 1;
    	int table1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table4[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	table1[position] = position;
    	table2[bad1] = num;
    	table3[bad2] = bad2;
    	table4[bad3] = bad3;
    Continue getting number.

    Code:
    srand ( time(NULL) );
    	char num = 'D';
    	int bad1 = rand() % 10;
    	int bad2 = rand() % 9 + 1;
    	int bad3 = rand() % 9 + 1;
    	int position = rand() % 9 + 1;
    	int table1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int table4[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    	table1[position] = position;
    	table2[bad1] = num;
    	table3[bad2] = bad2;
    	table4[bad3] = bad3;
    Continue getting number.
    Last edited by alvaritos; 09-08-2011 at 01:24 PM.

  10. #10
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    because of

    table2[bad1] = num;

    table2 is an array of ints..

    You should read the basics. .___.

    Variables. Data Types. - C++ Documentation

    And that code seems really messy...
    Last edited by 'Bruno; 09-08-2011 at 01:55 PM.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  11. #11
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,676
    My Mood
    Mellow
    Make your arrays char arrays then, or typecast the elements.

  12. #12
    alvaritos's Avatar
    Join Date
    May 2008
    Gender
    male
    Posts
    234
    Reputation
    9
    Thanks
    73
    My Mood
    Amazed
    Ok, solved thanks

  13. #13
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    /Marked Solved.

Similar Threads

  1. [Help] Char Arrays [Solved]
    By kibbles18 in forum C++/C Programming
    Replies: 8
    Last Post: 09-23-2011, 04:28 PM
  2. [Help] Intializing Large Character Array [Solved]
    By Braco22 in forum C++/C Programming
    Replies: 26
    Last Post: 09-01-2011, 10:43 PM
  3. [Help] Intializing a Boolean Array [Solved]
    By Braco22 in forum C++/C Programming
    Replies: 9
    Last Post: 08-31-2011, 03:22 PM
  4. [HELP] Finding the averages on a 2D array (Diagonally)[Solved]
    By SERG in forum Visual Basic Programming
    Replies: 5
    Last Post: 12-19-2010, 11:30 AM
  5. [Help]Isolate new ID in a Int32 array[Solved]
    By topblast in forum Visual Basic Programming
    Replies: 3
    Last Post: 12-18-2010, 05:35 PM