c - Loop that inputs characters in an array of structures does not work properly -


The dictionary should read the word (s) that user input "word" or "definition" The only problem is that for the first example of the loop, the readline function is not being called, and it only happens when the word should be stored in [0]. This gives the term the user input to the entry # 1.

How can I fix this?

  // Enter the words with their corresponding definitions #include & lt; Stdio h & gt; Structure entry {four words [15]; Four definitions [50]; }; Int main {zero} {int numberEntries; Zero Input Entry (Structure Entry Dictionary [], Int Number Numbers); Printf ("How many dictionary entries you want to enter ?. \ N"); Scanf ("% i", and numbers); Structure entry dictionary [numbers]; InputEntry (dictionary, numbers); Return 0; } Zero input entry (structure entry dictionary [int], int number entries) {zero readline (four buffers []); Int i; (I = 0; i & lt; numerals; i ++) for {printf ("entry #% i: \ n", i + 1); Printf ("word:"); ReadLine (dictionary [i] .word); Printf ("Definition:"); ReadLine (dictionary [i] .definition); Printf ("\ n"); } (I = 0; i & lt; numerals; i ++) {printf ("\ n% s", dictionary [i] .word); Printf ("\ n% s", Dictionary [i] .definition); }} // Get a string and it has an array zero readline (four buffers []) {char character; Int i = 0; Do {character = getchar (); Buffer [i] = character; I ++; } While (character! = '\ N'); Buffer [i - 1] = '\ 0'; }    

You can enter scanf and Code> which confuses things scanf only will read the integer reads, and then enter the next getchar press the key; A \ n .

In your case, the easiest solution is to use scanf instead of readLine .

You can consider using the standard function fgets instead of writing your own readLine because fgets is better Your function does not force the buffer parameter, the buffer leads to overruns if you type a lot of characters in a line.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -