Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Character Array.

  • 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,786 ✭✭✭✭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