0

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);
}
2
  • With the if(ch != ...) bit, it might be helpful to note that the ASCII codes for '0' through to '9' are in sequential numeric order. Commented Dec 15, 2011 at 6:32
  • yes then I should call them in a range of ascii Thanks,I got it Commented Dec 15, 2011 at 6:35

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much,I didn't know how to relate my functions *n,you are absolutely right scanf is not proper to call your change helped me.
0

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");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.