I work with a method that displays only one car by its registration number. My structure:

struct car { char registration[10]; char model[30]; char colour[30]; int num; bool reserved; long amount; }; struct LinearNode { struct car *element; struct LinearNode *next; }; 

I call the method in the menu as follows:

 char reg[10]; printf("Input car registration:"); scanf("%s", reg); viewSpecificNodes(reg); 

And then the method itself:

 void viewSpecificNodes(char *reg) { int string; struct LinearNode *current, *previous; bool notFound = true; if (isEmpty()) printf("Error - there are no cars in the list\n"); else { current = front; while (current != NULL) { string = strcmp(reg, current->element->registration); if (string = 0){ printf("Registration: %s\n", current->element->registration); printf("Model: %s\n", current->element->model); printf("Colour: %s\n", current->element->colour); printf("Number of previous owners: %d\n", current->element->num); if(current->element->reserved==0){ printf("Car is unreserved\n"); }else{ printf("Car is reserved\n"); } printf("Reserve amount: %li\n", current->element->amount); } else { printf("This car does not exist"); } 

current = current-> next; }}} // end viewSpecificNode

I decided that my string string == 0 is an error, because right at the beginning I have the following expression:

 #define bool int //defining my own verion of boolean #define true 1 #define false 0 

and changed the line to string == 1 . In the end, nothing matters.

This method simply does not work and causes nothing.

  • After updating the code, there is a problem with looping - it's easier to remove the last else. Another one appeared: not enough} closing for. Although the for loop itself is not needed here, because it is hardly necessary to display one element SIZE once, rather, it was intended to number the found elements or to display the ordinal number of the found element in the list. - paulgri
  • Rather, the idea was to number the items found. Thanks, I will fix the problem with the bracket now - Alex
  • In the if line (string = 0) there probably was a warning - assignment instead of comparison. Therefore, the comparison does not work. - paulgri

2 answers 2

The main problem is that the current pointer is not initialized (it does not have a pointer to the first element of the list before the loop ), so either the loop does not work, or goes over garbage values ​​(and there is garbage on the screen, a default is possible).

The @Harry remark about the reg variable is quite true, but when this error is fixed, the function will hang on the first element found (there is no transition to the next). As for i, it is really “trivial”, it does not affect the performance of the function, but very ugly.

  • Yes, I didn’t notice the elephant :) Or - the forest behind the trees ... Thank you! - Harry
  • Thanks a lot for your help. I corrected my shortcomings. Now apparently the error is that I incorrectly use the variable reg. But I want reg not to be one character, but a whole line of characters, so I use scanf ("% s", reg). - Alex
  • I think there are no problems with my function. I just do not correctly pass a variable - reg - Alex

How the reg variable is declared here: scanf("%s", reg); ? Judging from the fact that void viewSpecificNodes(char reg) is just a char ? But if you want to read a single character (it’s not clear from the code, the LinearNode structure is not described for you) - then you need to scanf("%c",®) .

Well, and on trivialities - the variable i in the function is not initialized and does not change, so that when it is output - random garbage will be displayed in square brackets, the same for all output records.

  • If reg is a string of characters, does this mean that you need to use scanf ("% s", reg)? - Alex
  • @Alex Yes, but then it should be passed to the function as a pointer to a char , and not compared using the == operator, but strcmp or stricmp , for example. And when reading a line using scanf may be trouble overflowing the buffer. However, if this is a training task without protection from fools and burglars, then okay :) - Harry
  • Thanks, now I will try to do everything. You are right, the task "with protection for fools", so it is worth all the work - Alex