Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Character Array.

Options
  • 08-10-2004 11:28am
    #1
    Closed Accounts Posts: 7,230 ✭✭✭


    I'm using getch() for user input, which returns the ascii value of the key that was pressed (an int value).
    ENTER is defined as:
    #define ENTER 0x0d
    so i'm doing
    while(c = getch() != ENTER) {
           inputchar[count] = (char) c;
           ++count;
    }
    
    say the user pressed 1 then 2 then enter, the array contains 1,2.
    The problem that i'm having is i want to pass this 12 as an int to a subscript of another array. So how do i assign what's contained in the array, to an int?

    Apologies if what i'm trying to do is not clear. I'm severly hungover :rolleyes:


Comments

  • Closed Accounts Posts: 16,793 ✭✭✭✭Hagar


    I don't program in that language however these generic methods should work,

    Define an int var and set it to zero
    If the array starts at count=0 as each key is pressed multiply the val(char) by 10 raised to power count and add to int var.
    If it starts at count =1 multiply the val(char) by 10 raised to power count-1 and add to int var.
    Int var should now be what you want.


    or

    Define a char var and set it to ""
    As each key is pressed concatenate char var = char var + inputchar
    when finished the val of the char var should be what you want.


    Hope that helps. ;)


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    problem sorted. there seemed to be a problem with
    while (d=(char)getch() != ENTER) so i changed it to while(d != ENTER) and did
    d=(char)getch() in the while loop. anyway it's sorted, here's the fixed code.
    #include <winbgim.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define ENTER 0x0d
    
    int main(){
        int count=0;
        char input_char[256];
        int e;
        char d;
        initwindow(1200,700);
        while(d != ENTER)
        {
            d=(char)getch();
            input_char[count] = d;
            ++count;
        
        }
        
        e = atoi(input_char);
        printf("%d",e);
        getch();
    }
    
    so i can pass e to the array i want.


Advertisement