0

I've tried using this code to read the struct named packet, which includes priority(int), qty(float) and message (a sentence).

for(i = 0; i < 3; i++) {
  scanf("%d%f", &packet[i].priority, &packet[i].qty);
  gets(packet[i].message);
  printf("\n%d %.6f ", packet[i].priority, packet[i].qty);
  printf("%s\n", packet[i].message);
}

The problem is that I want it to print something like:

1 1 MESSAGE NUMBER ONE
2 1 MESSAGE NUM TWO 
3 4 MESS NO THREE

But instead it prints

1 1 
0 0 MESSAGE NUMBER ONE
2 1

Like the gets() doesn't execute when needed but instead delays the "for" by one step. Any ideas?

  • Seems to have a new line after the number of inputs.(E.G 1 1\n MESSAGE..\n) also Clearly different from the actual code. – BLUEPIXY Dec 02 '14 at 19:26
  • gets() is depreciated and is full of problems. use fgets() so the message field is not overflowed. depending on how the data is entered at the terminal, it may be necessary to consume a newline before calling fgets() – user3629249 Dec 02 '14 at 20:40
  • calls to input functions (i.e. scanf) need to check the returned value to assure the conversion(s) were successful – user3629249 Dec 02 '14 at 20:43
  • regarding this line: scanf("%d%f", &packet[i].priority, &packet[i].qty); with no separation between the two numbers, there is no way to determine were one number ends and the next begins. suggest: scanf(" %d %f", &packet[i].priority, &packet[i].qty); and the input needs this some kind of white space separator. Also, the scanf format string needs a leading ' ' to consume any leading white space, like the prior line's newline char – user3629249 Dec 02 '14 at 20:46
  • there is a '\n' at both ends of the printf sequence, That means the output will be double spaced. – user3629249 Dec 02 '14 at 20:48

3 Answers3

0

To debug, I suggest that you include single quotes in the printf output. You have enough combined scanf , gets and printf statements that it is not always clear where one starts in the input and output (as you have shown in the issue).

printf("\n(d %d) (f %.6f) (s %s)\n, packet[i].priority, packet[i].qty, packet[i].message);

would go a long way in helping you determine what is being printed, and where, as the output could easily look like

(d 0) (f 0) (s
)

and without the extra characters, you would easily confuse a newline output as an error in the very closely located input routines.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

try this

scanf("%d%f %[^\n]", &packet[i].priority, &packet[i].qty,
                     packet[i].message);
printf("\n%d %.6f ", packet[i].priority, packet[i].qty);
printf("%s\n", packet[i].message);
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

First and foremost: Why is the gets function so dangerous that it should not be used? This should clear up some confusion when using gets and why you should not be using it.

scanf will leave a line terminator \n\r in the input buffer so when you begin to use gets() it will pick up the line terminator. So it retrospect, the for loop is not 'delayed' gets() is consuming the line terminator and will pick up the next line whenever it occurs and assign it to message.

You could try and define message as a character array to be read into.

struct placeholder
{
  char message[25]; // Able to hold a message of length 24.
  // rest of your struct variables.
}PlaceHolder;

// using scanf is still appropriate for reading in the message

To read the message you will need to consume the newline character - look at this for more help: scanf: "%[^\n]" skips the 2nd input but " %[^\n]" does not. why?

Community
  • 1
  • 1
KillaBytes
  • 447
  • 3
  • 10