-1

I am trying to write a program that reads characters (until end is reached) on N lines and stores the characters as decimal numbers in an array and then prints the array. Input of characters is just 0-9 or A-Z.

Here is my code:

#include <stdio.h>
#define SIZE 1000

int main()
{
    int c, N;
    int i,j,k, n;
    int x[SIZE];
    scanf("%d\n", &N);
    for(i = 0; i < N; i++){
        c = getchar(); 
        for(j = 0; c!=EOF; j++)
        {
            if(c<='9') x[j] = c - 48;
            else x[j] = c - 'A' + 10;
            n++;
            c = getchar();
        }
        for(k=0;k<n;k++) printf("%d ", x[k]);
    }

    return 0;
}
an example input:
2
ABC
ABD
The desire output:
10 11 12
10 11 13

But my code outputs:

10 11 12 -38 10 11 13 
10 11 12 -38 10 11 13 

I tried to write c!='\n' but it gives runtime error

UPDATE: I FIXED it it works now, I had to set n to 0 again.

#include <stdio.h>
#define SIZE 1000

int main()
{
    int c, N;
    int i,j,k, n=0;
    int x[SIZE];
    scanf("%d\n", &N);
    for(i = 0; i < N; i++){
        n = 0;
        for(j = 0; (c = getchar()) != '\n'; j++)
        {

            if(c ==EOF) break;
            if(c<='9') x[j] = c - 48;
            else x[j] = c - 'A' + 10;
            n++;

        }
        for(k=0;k<n;k++) {printf("%d ", x[k]); } printf("\n");

    }
    return 0;
}
OrangeBike
  • 133
  • 7

1 Answers1

1

Your code has several issues.

A few things explained:

  1. if(c<='9') x[j] = c - 48; - That is wrong. With that you subtract all ASCII characters below and equal to '9' by 48. You need to use (c <= '9' && c >= '0') to subtract only the ASCII numbers of integral numbers by 48.

  2. else x[j] = c - 'A' + 10; needs to be else if (c >= 'A' && c <= 'Z') x[j] = c - 'A' + 10; - The reason is kind of self-explaining. If c is any character but an integral value you substract its ASCII value by 'A', which is wrong. Only characters between 'A' and 'Z' should be treated this way.

  3. n is uninitialized. It´s use invokes undefined behavior.

  4. n needs to be reset at the end of the first loop.


Here is what I think you want. I hinted the differences as comments in the code.

#include <stdio.h>
#define SIZE 1000

int main (void)
{
    int c, N;
    int i, j , k, n = 0;          // initialize n to 0.
    int x[SIZE];

    scanf("%d", &N);
    getchar();                    // catching newline left in stdin.

    for (i = 0; i < N; i++) {

        for (j = 0; (c = getchar()) != EOF; j++) {      // Use getchar in the condition.  

            if (c == '\n')                   // If c is newline, break out of the loop.
            break;

            if (c <= '9' && c >= '0')                    // If c is between 0 and 9...
            {
                x[j] = c - 48;
            }

            else if (c >= 'A' && c <= 'Z')               // If c is between A and Z...         
            {
                x[j] = c - 'A' + 10;
            }

            n++;
        }

        for (k = 0; k < n; k++) {
            printf("%d ", x[k]);
        }

        printf("\n");                                   // Added newline.
        n = 0;                                          // Reset n.
    }

    return 0;
}

Input:

2
ABC
ABD

Output:

10 11 12
10 11 13