Hello. There is a task:

Given a list of at least 8 workers, enter the name, count. years of work experience, how many until retirement. Bring anyone left less than 15 years to retirement.

I wrote this code:

#include <stdio.h> struct raboczii { char imja[20]; char kolvolet[100]; char staz[50]; } raboczii; void main() { char str[30]; int p; raboczii working[8] = { { "Storoz Tolja", "45", 20 }, { "Teteruk Dima", "46", 25 }, { "Chirva Ura", "47", 29 }, { "Ivanov Vasja", "62", 32 }, { "Pupkin Aleksei", "65", 35 }, { "Bil Geits", "22", 2 }, { "Ivan Ivanov", "19", 1 }, { "Petrovicz Petja", "51", 21 }, }; for (p = 0; p < sizeof(working) / sizeof(working[0]); p++) { if (working[[p].kol - vo let > 60) printf(" %s %s %d\n", working[p].imja, working[p].kolvolet working[p].staz, ); } getch; } 

Writes an error in raboczii working[8] =

Tell me what's wrong?

  • You have an extra comma extra after "{" Petrovicz Petja "," 51 ", 21}" By the way, in "raboczii working [8] =" you can omit 8 and just write "raboczii working [] =" - the compiler according to the list of initializers itself will understand what size the array will be + You have an extra comma in ptintf at the end of the argument list + To call getch, write getch (); and since you are just an expression that does nothing. - dzhioev
  • corrected everything, but still writes an error in the line raboczii working [] =, how to fix it? I'm not thinking at all ( - Timi
  • Toshka already fixed) - Timi
  • Still not working? - Toshka
  • Toshka, not working - Timi

5 answers 5

Taco code

 struct raboczii { char imja[20]; char kolvolet[100]; char staz[50]; } raboczii; 

means to declare a variable raboczii which has the type of structure struct raboczii then it is necessary either to declare the variable working type of type struct raboczii, i.e. to write

 struct raboczii working[8] =... 

either (which is preferable), declare this structure a type, i.e.

 typedef struct { char imja[20]; char kolvolet[100]; char staz[50]; } raboczii; 
  • Only the question of typedef preference is highly controversial. If the goal is to type fewer characters, then yes. If you need to figure out what doesn't work in someone else's big program, then no. With a large number of typedefs, you start to get confused, where is the structure, where is the pointer (when pointers of different types are also typedefs). - avp
  • The usual declaration style is: typedef struct raboczii {...} raboczii, * praboczii; - renegator

And here’s another mistake: you specify char kolvolet[100] , and then you write if (working[[p].kol-vo let >60) dash too much.

    Everything is very simple - in the structure both kolvolet and staz are set as char, and for some reason they are initialized differently - kolvolet - as a string, and staz - as a number. That is, the data type does not coincide.

    I would define both kolvolet and staz as int, and in the assignment of initial values ​​I would remove the quotes.

    • anyway there is an error in the line raboczii working [] =, (( - Timi
    • And you have "{" and "}" all enough? Maybe it's the syntax? - Toshka
    • @Toshka, just enough) ... yes FIG knows what's up) - Timi
    • @Tim, hold on! Find a way out! )) - Toshka
    • Specifically, give the output of the compiler (and the program text is exactly for this output). - avp

    Declaring a structure array requires the default constructor. Declare such a constructor, create an array, and then fill the array with concrete. @Voldemarus made fair remarks + type mismatch in printf (...). Why do you need 100 characters in age and 50 characters in the experience? In my opinion, you have some misunderstandings with an array of type char [];

      Maybe so?

       typedef struct { char* imja; //Транслит смотрится ужасно, лучше name int kolvolet; //То же самое, лучше age int staz; //Назвал бы experience } raboczii; //А worker было бы лучше 

      In the second column, when defining an array, you must remove the quotes, and in the first argument of the printf call, pass "% s% i% in".

      "Bil Geits"

      Generally correct to write Bill Gates.