Thread: not sure how to handle variables...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    5

    not sure how to handle variables...

    Hi,

    I'm writing some code that involves moving letters around a grid that is printed out into the command line of a terminal window on Linux. The grid is set up as a number of variables (i.e. A1, A2, A3, B1, B2....etc) and the letter needs to be moved to the coordinate on the grid that the user has selected. I'm reading the user's input (which is simply the coordinate that they want the letter moved to, 'A1') into a separate variable(char Y) using scanf, and the problem I've got is that i'm not sure how to change the coordinate variable once it's been read into the variable Y....what i'm trying to say is, i don't know how to get the inputted coordinate out of the variable Y to change it.

    I realise i'm not explaining this very well at all, i'll post a little snippet of code incase that helps:

    Code:
    printf("Enter the co-ord to move the letter to: ");
    scanf("%c", &Y);
    printf("\n");
    /* function to change coordinate variable */
    Sorry for the poor explanation...hopefully some of you can work out what i'm trying to say!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I would suggest you read up on arrays.

    As in
    int A[3];
    rather than
    int A1, A2, A3;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Even better, you need to take the next logical leap and realize that just because the user is going to type "A1" into the program, you don't need to have a variable called A1, or even A[1]. You can parse that input into coordinates, such as [0][0] and use a two-dimensional array to hold your letters that you're printing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with adding strings into ComboBox using Window Handle
    By jasperleeabc in forum Windows Programming
    Replies: 7
    Last Post: 05-05-2009, 04:48 PM
  2. Get handle by name
    By System_159 in forum Windows Programming
    Replies: 7
    Last Post: 03-16-2009, 08:45 AM
  3. What types of files can C++ file processing handle?
    By darsunt in forum C++ Programming
    Replies: 9
    Last Post: 10-28-2008, 11:33 AM
  4. global variables
    By shadovv in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2005, 02:21 PM
  5. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM