Nickool
Nickool

Reputation: 3702

read string of character and assign it to an array

I don't know how to work with scanf and get the input of it for the entry of the function readBigNum I want to make array until the user entered the Enter and also I want to write a function for assigning it into an array and return the size of the large number I want readBigNum to exactly have the char *n but I can not relate it in my function

#include <stdio.h>

int readBigNum(char *n)
{
    char msg[100],ch;
    int i=0;
    while((ch=getchar())!='\n')
    {
        if(ch!='0'||ch!='1'||ch!='2'||ch!='3'||ch!='4'||ch!='5'||ch!='6'||ch!='7'||ch!='8'||ch!='9')
            return -1;
        msg[i++]=ch;
    }
    msg[i]='\0';
    i=0;
    return i;
}

int main()       
{
    const char x;
    const char n;
    n=scanf("%d",x);
    int h=readBigNum(&n);
    printf(h);
}

Upvotes: 0

Views: 198

Answers (2)

dreamlax
dreamlax

Reputation: 95335

Remember that getchar() returns type int, not char. This is because the function may return EOF (which is defined as a negative integer with no particular value).

Also, for functions that deal with buffers, it is always a good idea to take an extra argument that describes the size of the array. This helps reduce buffer overruns because you know how far you can go. With your existing function, if the user types more than 100 characters, your buffer is overrun.

#include <stdio.h>
#include <ctype.h>

int readBigNum(char *n, size_t len)
{
    int ch;
    int i = 0;

    // we make sure 'i' is less than 'len - 1' to leave space for '\0'
    while((ch = getchar()) != EOF && i < (len - 1))
    {
        if (ch == '\n')         // stop on linefeed
            break;
        else if (!isdigit(ch))) // abort on invalid character
            return -1;
        else
            n[i++] = (char) ch;
    }

    msg[i] = '\0';
    return i;
}

int main(void)
{
    char buf[100];
    int result = readBigNum(buf, sizeof buf);

    if (result > 0)
        printf("Length %d : %s\n", result, buf);
    else
        printf("Invalid number!\n");
}

Upvotes: 0

DRH
DRH

Reputation: 8328

If I understand your question correctly, you want to implement a function that will read numbers from stdin storing them in a buffer. If a non-number is encountered, you want to return -1. If a new-line is encountered, you want to return the number of characters that were read. If that's correct, you'll probably want your code to look something like the following:

#include <stdio.h>

int readBigNum(char* n)
{
    char ch;
    int i=0;
    while ((ch = getchar()) != '\n') {
    if (ch < '0' || ch > '9') {
        return -1;
    }
        n[i++] = ch;
    }
    n[i] = '\0';
    return i;
}

int main(void) {
    char buf[100];
    int bytes = readBigNum(buf);
    printf("%s\n", buf);
    printf("%d\n", bytes);
};

The main differences from your implementation

  • The array to be populated is initialized in main and passed to the readBigNum function. This is a little simpler than having the function control the memory, in which case you would need likely need to deal with malloc and free. Even with this, you run the risk of a buffer overrun and will likely want to take additional precautions to prevent that.
  • The function does not set i to 0 before returning it. The original code could never return a value other than -1 (on error) or 0, which didn't appear to be the intent.
  • This code doesn't use scanf. Given your description of what you wanted to accomplish, using scanf didn't appear to be a good fit, however if you provide more information on why you were calling it might help to inform this answer.
  • The printf call was incorrect, it has been updated to print the number of bytes returned, and an additional printf call was added to print the updated buffer.

Upvotes: 1

Related Questions