Thread: Noob question(User input Arrays)

  1. #1
    Registered User wildex999's Avatar
    Join Date
    Feb 2006
    Posts
    15

    Noob question(User input Arrays)

    Hello, I have started with C++ and have read some tutorials, so now I'm trying to make something, but i got a problem, and it's probaly easy to fix, but I can't do it.

    I try to place a value on a multidimension array, where user input select where to put:

    Code:
    int board[25][25];
    
    char pinp[3]; //Player input
    cin>>pinp;
    cin.ignore();
    board[(int)pinp[0]][(int)pinp[2]]=1; //Input is 1.3
    But the program just get unexpected error when i have pressed enter at input :S
    Last edited by wildex999; 02-19-2006 at 03:27 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    cin<<pinp;
    should be:
    Code:
    cin>>pinp;
    cin or cout will always be on the left hand side, and the arrows point in the direction data is flowing.

  3. #3
    Registered User wildex999's Avatar
    Join Date
    Feb 2006
    Posts
    15
    Fixed tha, but that was just me writing error on this post, the error is still there, somewhere.

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    you are reading data in as type char. look up the ascii table and
    you will see that the ascii value of numerals is not the same as
    their actual numeric values: digit number 1 = ascii 49. so those
    casts you make, you get different values to what you are
    expecting.

    EDIT
    your code only allows you to read in numbers between 0-9.
    personally i'd read in the coordinates separately
    Last edited by Richie T; 02-19-2006 at 03:37 PM. Reason: wrong ascii code :-(
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    EDIT
    Beaten

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You should also use a C++ cast, which has a very ugly looking syntax which stands out in your code, and serves to make it very clear a cast is happening:

    static_cast<int>(someVar)

    And, you have to be aware that casting '1' to an int will not give you 1. Everything stored in a computer's memory has to be stored as a number(i.e. in binary code). So, all the char's including '1' are assigned secret numerical codes, which are stored in the computer's memory to distinguish them from the numerical values 1,2,3 etc. It so happens that the secret code for '1' is 49. So, when you cast the char '1' to an int, you get 49--which is out of bounds for your array and will result in undefined behavior.

    If you want to see all the secret codes for all the characters, look at an ascii table.
    Last edited by 7stud; 02-19-2006 at 03:38 PM.

  7. #7
    Registered User wildex999's Avatar
    Join Date
    Feb 2006
    Posts
    15
    Thanks for quick reply.
    Yea, I remember now, char can't be converted to int.
    I also tried to use string and substr to get out the numbers i want, but:
    Code:
    no match for 'operator[]' in 'game.game_functions::board[std::basic_string<_CharT, _Traits, _Alloc>::substr(typename _Alloc::size_type, typename _Alloc::size_type) const [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](0u, 1u)]'
    Anny idea?

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    that sounds to me to be a misuse of string, possibly a syntax
    error or a misinterpretation of how to use c++ strings. without
    any code/compiler details of what you have done, its hard to
    tell...
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    Registered User wildex999's Avatar
    Join Date
    Feb 2006
    Posts
    15
    Seems like it doesn't like me to use string inside [], and neither can i make it char or int. Damn, I just want to go back to GML where it was do it or not, not like do 3 things just to be able to do the other 100 things(Converting, casting etc.)

    I'm using Dev-C++ by the way.

  10. #10
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    well i ask: why do you need to read in the co-ordinates into an
    array? for user defining a location in a two dimensional array,
    i typically use two separate ints to locate the position. is it a
    requirement that you read them in separated by a '.' ?
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  11. #11
    Registered User wildex999's Avatar
    Join Date
    Feb 2006
    Posts
    15
    Much easier on the user, I'm gonna use them to enter a value for a tic tac toe game(First thing i make with C++) and I make them enter X and Y, ofcourse I can do in two inputs, but... I just want to know how to do it like this, as it may be usefull later on.(+, if he enter X cord, and then change mind, how can he go back to change the X cord? Command would work, but...)

  12. #12
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    fair enough. its not something i've experimented with myself, but
    there are functions which can be used to valiate user input
    (isdigit, isalpha, ispunct). what i would do is read in as a C style
    string as you have already. then using a loop, examine the
    elements of the string and convert them to their integer
    equivalents using: X = (somechar) - '0'. thats a cheap way to
    convert between chars 0-9 and their int values. then once the
    '.' is hit, save in a Y variable.

    the only problem with this approach is that it runs into difficulties
    with numbers greater than 9 (since each digit entered is stored in
    a separate elemeny in the string). this is just one approach,
    there's probably better ways, but thats just a quick idea i had. i
    cant help but this how much easier it is to do this type of thing in
    C:

    Code:
    scanf ("%d.%d", &x, &y);
    two numbers read in separated by a '.' sorry i cant help more,
    maybe someone else put there will be more inspired...
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  13. #13
    Registered User wildex999's Avatar
    Join Date
    Feb 2006
    Posts
    15
    Well, I tryed to make something out of the multi useri nput model that was suggested, and here it is.
    What is missing:
    Scoring, It doesn't check to see if someone have won
    Error checking, entering number above 9, or not a number at all, WILL give error.
    Some fine tuning on code.

    Enjoy(Support 2 playr, YAY! ^_^)
    Tic Tac Toe V0.1
    Tic Tac Toe V0.1 CODE

    Thanks for the help so far ^_^
    Last edited by wildex999; 02-19-2006 at 04:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input on arrays and pointers
    By cjohnman in forum C Programming
    Replies: 2
    Last Post: 05-01-2008, 01:57 PM
  2. Arrays of pointers (input)
    By tyskater in forum C Programming
    Replies: 10
    Last Post: 12-17-2003, 12:00 AM
  3. Sorting Arrays - Even and Odd Input
    By deedlit in forum C Programming
    Replies: 19
    Last Post: 11-20-2003, 04:35 AM
  4. invalid input
    By sworc66 in forum C Programming
    Replies: 8
    Last Post: 11-11-2003, 05:41 AM
  5. Reading the input file into integer arrays
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 12-06-2002, 07:04 PM