Hi, so i was told that i could use the scanf() function, validating it with a space, instead of an "ENTER".

scanf ( "% [^] d", & a);

The person told me to use that.
I couldn't get it working.
Do you know what is wrong?

Extra info: I am trying to solve a problem, in wich i have to give the program the ammount of lines i want to use, and then in each line i have first a number (for example A) that says how many numbers that line is going to get, and then with those numbers i need to find the 2 that are the most close and present the diference between them (example, in {1, 2, 5, 7} the answer would be 1.


you can probably do it with scanf() in a complex and obscure and difficult-to-understand manner.

i don't know how.

but i do know how to do it with fgets() and atoi() in a simple, straightforward and easily-understood maner..

could you explain it to me how to do it? or give me a link with some documentation about it.

tanks :D

before i start trying to explain it, i need to understand what exactly the requirements are.

i see you tried to explain, above, but i apologize that i don't fully understand what you said,

Sorry for the late answer, i was a little busy in the weekend.

So, i'll try to explain it more properly to you.

I need to solve a problem that goes like this:

The user gives you a number, lets say N, then N lines follow, in wich, the user gives another number K, that determines the number of numbers the user is going to input in that line, (ex.: K = 3, the line will look like this "3 number number number").

The objective of the pogram is to determine wich is the least difference between 2 numbers in each of those lines. (ex.: a line like "3 4 1 5" the answer would be 1, 'cause 5-4 = 1)

there won't be any negative numbers, there won't be repeated numbers, and the difference can't be negative.

The problem is, i have a input/outpud that has to be just like i was told, and i don't know how to ask for K, and after that ask for K numbers, without leaving the same line.

Input/Output Example:

you can try something with loop, kind of:

scanf("%d", &k);
for (int i=0; i<k; i++) {
  printf(" ");
  scanf("%d", &k_number);

that wouldn't work, 'cause scanf() changes the line, because it needs an enter to validate, so wouldn't do for me.

don't get me wrong, if i could have any output i wanted, i knew how to solve this problem, i just can't do the part when it asks me for K and then K numbers in the same line...

Well... then you can read line by line as string and parse that line with strtok(), something like:

scanf("%s", current_line);
  char * pch = strtok (current_line, " ");
  while (pch != NULL)
    sscanf(pch,"%d", &current_K);
    pch = strtok (NULL, " ");

Reading the whole line using fgets(), then using either strtok() or sscanf() to parse the line would be my suggestion.

Trying to spot the \n with scanf() would be messy.

so, if i understood correctly, you would neet strtok to cut the string that i read with fgets into different variables.

how would i assign each value to each differente variable, or to an array, so that i could use later?


It's how 0x69 posted in post #8, only using fgets() to read the line in place of scanf.

But you've had 2 days and 10 replies, so now it's time to focus.

sorry it took me so long to get back to this.. You should learn how to use strtol(). each time you call strtol, it finds the first numeric value in a string, converts it to a long int and returns it. it sets the pointer argument to the very next character in that string

if non-numeric characters (alpha or symbols) are found, zero is returned as the value and the pointer continues to point at the start of the string that was being looked at. this indicates that it failed to find a numeric in the string.

(Note, strtol() will ignore and skip over any whitespace found before a value. whitespace after an int value indicates the end of that value.)

i'm going to give you this snippet because i think seeing an example like this in action is really instructive.

// ... set up your variables

fgets(single_line, sizeof(single_line), stdin)

numberOfEntries = strtol(single_line, &ptr, 10)

for (i = 0; i < numberOfEntries ; i++)

   value[i] = strtol(ptr, &ptr, 10) 

// ... do the rest

some people may think i'm giving you too much, so you're going to need to figure out from here how to set it up properly within the context of your program.

Be advised this performs no error checking whatsoever; erroneous input may cause this code to behave quite badly. all good code should have error checking.

for reference:


Thanks for everything guys, i guess you didn't see that i marked the thread as solved, 'cause i happened to get it going with scanf, in a messy way but it worked, but thanks, beacause all of you guys i now know how to work with fgets and strtok.

Thanks ;)