Thread: Problem using getchar

  1. #1
    Registered User
    Join Date
    Sep 2022
    Posts
    3

    Problem using getchar

    Hello,
    On my program I have 9 char named c1,...c9.
    During the program, I write: gtch=getchar() (I should give a number between 1 and 9)

    How can I modifiy the "value" of the character c1,...9 according to what number I enter?

    For example, c4= 'x'. Then, I enter the number 4, so gtch='4'. How can I transform c4 to 'y'.

    PS: I'm a begginer on C...

    Thank you !

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > On my program I have 9 char named c1,...c9.
    Rather than doing
    c1, c2, c3 etc

    you use an array instead.
    Code:
    char c[9];
    This gets you 9 chars labelled c[0] to c[8];

    So perhaps for an easier life in this case
    Code:
    char c[10];
    This gets you 10 chars labelled c[0] to c[9];


    > During the program, I write: gtch=getchar() (I should give a number between 1 and 9)
    This will get you a character '1' to '9'

    To transform a single digit character containing a number into an integer, all you do then is
    gtch = gtch - '0';

    You can then do
    c[gtch] = 'y';
    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
    Registered User
    Join Date
    Sep 2022
    Posts
    3
    Thank you for your quick answer !
    I still have a small error:
    Here's what I write (whithout details):

    Code:
    char cases[10]={'x0','x1',...'x9'};
    int main (void)
        ...
        char gtch;
        gtch=getchar();
        gtch=gtch-'0';
        cases[gtch]='y';   --> in this line I have an error: array suscrpit has type char , so when I run my program no 'y' is visible.


    Thank you for your time !

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well getchar() actually returns an int, so you should really have
    Code:
    int gtch;
    gtch=getchar();
    if ( gtch != EOF ) {
        gtch=gtch-'0';
        cases[gtch]='y';
    }
    Also, things like 'x0' are not characters, since you have two chars between single quotes.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2022
    Posts
    3
    I understand the problem, Thank you !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF and getchar() problem
    By Aitra in forum C Programming
    Replies: 3
    Last Post: 06-20-2013, 11:57 PM
  2. problem with getchar()
    By blob84 in forum C Programming
    Replies: 2
    Last Post: 08-23-2010, 04:54 PM
  3. Problem with getchar()
    By SasDutta in forum C Programming
    Replies: 3
    Last Post: 03-31-2010, 10:33 AM
  4. A getchar problem
    By ch4 in forum C Programming
    Replies: 2
    Last Post: 10-29-2007, 03:39 AM
  5. getchar() problem.
    By caroundw5h in forum C Programming
    Replies: 11
    Last Post: 06-17-2004, 07:10 PM

Tags for this Thread