2

I know C++ but I am trying to write my first C program and I'd like to be able to get one line of input from a user and convert it to 10 different numbers. For example, a user will be prompted to enter some numbers and they will enter up to 10 numbers:

Input up to 10 numbers:

> 34412 12455 435234 44 199 4735890 034001 154595

Then I'd like to store them an in array and do stuff with them later. I've searched for how to get input but most of what I've found isn't clear to me. Any help would be appreciated, thanks!

1
  • You can use either fgets for reading whole lines or scanf/fscanf for reading single numbers. Commented Feb 3, 2013 at 0:00

3 Answers 3

2

If you are sure that the user is always going enter integers separated by spaces/tabs and newlines, and not more than 10 of them, you can use this one liner to do the trick:

int arr[10];
int count = 0;

while(scanf("%d",&(arr[count++])))
   ;

It takes advantage of the fact that scanf returns number of items matched.

Sign up to request clarification or add additional context in comments.

3 Comments

This covers situation when user enters 10 numbers entered within more than just 1 line. Also this scanf causes that reading will will stop only if 10 numbers are entered or if at least 1 sequence of non-white characters that cannot be read as a number is entered.
A bit safer would be to use (count < 10) && scanf("%d", &arr[count++]) as condition.
Good that you explicitly listed the requirements, "If you are sure that the user is always going enter . . .not more than 10". I would like to add, "If the user decides to enter more than 10 numbers, you will have a buffer overflow allowing the user to execute arbitrary code." (Basically, after a carefully constructed preamble, the user can enter integer representation of machine instructions that will be executed when the function attempts to return.) @vondbrand's safety check is well worth it and a great habit to get into. For more information see en.wikipedia.org/wiki/Buffer_overflow
1

You have a bit of reading to do about few C functions:

  1. fgets() to read a whole line. Remove the newline at the end.
  2. strtok() to tokenize the line.
  3. strtol() to convert each string into an integer.
  4. Store them in an array.

If you are not worried about invalid inputs, you can use sscanf() to read from C-string.

8 Comments

Or he can directly use scanf to read from standard input and parse at the same time too if he's not worried about invalid inputs.
Well, is there a better way to do it than "read a line"? I thought that was the best way to approach it but I guess I may be wrong?
@user209306 scanf() is notoriously bad when invalid input is given. More simply, you can use fget() in a loop and convert each string into an integer and validate as and when you read it rather reading all numbers in a single line & parsing them.
@KingsIndian, scanf(3) isn't "notoriously bad", you just have to check how many of the input items matched (the return value).
@vonbrand It's not that simple. If you input a string when scanf expects an int(%d), the input will still be there in the input and can't proceed without clearing it.
|
1

You can read line from users input, tokenize it and then directly retrieve numbers from tokens by using sscanf:

// array big enough to hold 10 numbers:
int numbers[10] = {0};

// read line from users input:
char inputString[200];
fgets(inputString, 200, stdin);

// split input string into tokens:
char *token = strtok(inputString, " ");

// retrieve a number from each token:
int i = 0;
while (token != NULL && i < 10) {
    if (sscanf(token, "%d", &numbers[i++]) != 1)
        ; // TODO: ERROR: number hasn't been retrieved
    token = strtok(NULL, " ");
}

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.