I wrote a program on CodeBlocks, everything works fine, but when I switch to Visual Studio I started to give an error "the expression must have a constant value. Tell me how to fix or what is the error, because with the designation of a constant another error occurs" the expression must be valid for the change to be left-handed value "

#include <iostream> #include <windows.h> #include <stdio.h> #include <iomanip> #include <conio.h> using namespace std; struct abonents { char family[20]; char name[20]; char phonenum[12]; }; FILE *f; const char* DOC = "abonents phone"; int main() { system("chcp 1251 > nul"); int T = 15, K = 15, i; const int n; char menu; do { f = fopen("DOC.dat", "rb"); fseek(f, 0, SEEK_END); int fn = ftell(f) / sizeof(abonents); fclose(f); cout << "1 - ΠŸΡ€ΠΎΡΠΌΠΎΡ‚Ρ€ всСго справочника" << endl; cout << "2 - Π”ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ Π½ΠΎΠ²ΠΎΠΉ записи" << endl; cout << "3 - Поиск Π°Π±ΠΎΠ½Π΅Π½Ρ‚Π° ΠΏΠΎ ΠΈΠΌΠ΅Π½ΠΈ ΠΈ Ρ„Π°ΠΌΠΈΠ»ΠΈΠΈ ΠΈ Π²Ρ‹Π²ΠΎΠ΄ Ρ€Π°ΠΏΠΎΡ€Ρ‚Π° Π² тСкстовый Ρ„Π°ΠΉΠ»" << endl; cout << "4 - ΠžΡ‡ΠΈΡΡ‚ΠΊΠ° \ созданиС Ρ„Π°ΠΉΠ»Π°" << endl; cout << "5 - Π£Π΄Π°Π»Π΅Π½ΠΈΠ΅ записи" << endl; cout << "6 - Π Π΅Π΄Π°ΠΊΡ‚ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ записи" << endl; cout << "7 - Π’Ρ‹Π²ΠΎΠ΄ справочника Π² тСкстовый Ρ„Π°ΠΉΠ»" << endl; cout << "8 - Π’Ρ‹Ρ…ΠΎΠ΄" << endl; cin >> menu; switch (menu) { case '1': { system("cls"); f = fopen("DOC.dat", "rb"); fseek(f, 0, SEEK_END); n = ftell(f) / sizeof(abonents); fseek(f, 0, SEEK_SET); abonents t[n]; fread(&t, sizeof(abonents), n, f); fclose(f); cout << "АбонСнт: " << n << '\n' << endl; cout << "|Ѐамилия: |Имя: | Π’Π΅Π»Π΅Ρ„ΠΎΠ½: |" << endl; cout << "| | | |" << endl; for (i = 0; i < n; i++) { cout << setiosflags(ios::left);; cout << "| "; cout << setw(20) << t[i].family << " | "; cout << setw(20) << t[i].name << " | "; cout << setw(15) << t[i].phonenum << " |" << endl; } cout << "| |" << endl; _getch(); system("cls"); break; break; } case '2': { system("cls"); abonents t; cout << "Ѐамилия: "; cin >> t.family; cout << "Имя: "; cin >> t.name; cout << "Π’Π΅Π»Π΅Ρ„ΠΎΠ½: "; cin >> t.phonenum; cout << '\n'; f = fopen("DOC.dat", "ab"); fwrite(&t, sizeof(abonents), 1, f); fclose(f); break; } case '3': { system("cls"); char name[20]; char family[20]; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Ѐамилию: "; cin >> family; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Имя: "; cin >> name; f = fopen("DOC.dat", "rb"); fseek(f, 0, SEEK_END); fn = ftell(f) / sizeof(abonents); fclose(f); f = fopen("DOC.dat", "r"); abonents t[n]; fread(&t, sizeof(abonents), n, f); fclose(f); f = fopen("SearchAbonents.txt", "wt"); for (i = 0; i < n; i++) if (strcmp(t[i].family, family) == 0 && strcmp(t[i].name, name) == 0) { fprintf(f, "Π’Π΅Π»Π΅Ρ„ΠΎΠ½ Π°Π±ΠΎΠ½Π΅Π½Ρ‚Π° "); fprintf(f, family); fprintf(f, " "); fprintf(f, name); fprintf(f, " -> "); fprintf(f, t[i].phonenum); } fclose(f); cout << '\n' << "ВсС Π½ΠΎΠΌΠ΅Ρ€Π° Π°Π±ΠΎΠ½Π΅Π½Ρ‚Π° " << family << name << " Π²Π²Π΅Π΄Π΅Π½Ρ‹ Π² Ρ„Π°ΠΉΠ» SearchAbonents.txt." << endl; system("notepad.exe SearchAbonents.txt"); cout << endl; break; } case '4': { int del; while (true) { system("cls"); cout << "Π’Ρ‹ ΡƒΠ²Π΅Ρ€Π΅Π½Ρ‹, Ρ‡Ρ‚ΠΎ Ρ…ΠΎΡ‚ΠΈΡ‚Π΅ ΡƒΠ΄Π°Π»ΠΈΡ‚ΡŒ запись?" << endl; cout << " 1 – Π”Π°" << endl; cout << " 0 – НСт" << endl; cin >> del; cout << endl; if (del == 1) { f = fopen("DOC.dat", "wb"); fclose(f); cout << "Π—Π°ΠΏΠΈΡΡŒ ΡƒΠ΄Π°Π»Π΅Π½Π°!\n" << endl; break; } if (del == 0) { cout << "Π£Π΄Π°Π»Π΅Π½ΠΈΠ΅ ΠΎΡ‚ΠΌΠ΅Π½Π΅Π½ΠΎ.\n" << endl; break; } } break; } case '5': { system("cls"); char name[20]; char family[20]; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ„Π°ΠΌΠΈΠ»ΠΈΡŽ: "; cin >> family; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ имя: "; cin >> name; f = fopen("DOC.dat", "rb"); fseek(f, 0, SEEK_END); fn = ftell(f) / sizeof(abonents); fclose(f); f = fopen("DOC.dat", "r"); abonents t[n]; fread(&t, sizeof(abonents), n, f); fclose(f); f = fopen("DOC.dat", "wb"); for (i = 0; i < n; i++) if (strcmp(t[i].name, name) != 0 && strcmp(t[i].family, family) != 0) fwrite(&t[i], sizeof(abonents), 1, f); else cout << endl << "НомСр Ρ‚Π΅Π»Π΅Ρ„ΠΎΠ½Π° ΡƒΠ΄Π°Π»Ρ‘Π½: " << t[i].phonenum << endl; fclose(f); cout << endl; break; } case '6': { system("cls"); f = fopen("DOC.dat", "rb"); fseek(f, 0, SEEK_END); fn = ftell(f) / sizeof(abonents); fclose(f); f = fopen("DOC.dat", "r"); abonents t[n]; fread(&t, sizeof(abonents), n, f); fclose(f); char name[20]; char family[20]; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Π°Π±ΠΎΠ½Π΅Π½Ρ‚Π°, запись ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ Π½ΡƒΠΆΠ½ΠΎ ΠΈΠ·ΠΌΠ΅Π½ΠΈΡ‚ΡŒ: " << '\n' << endl; cout << "Ѐамилия :"; cin >> family; cout << "Имя: "; cin >> name; for (i = 0; i < n; i++) if (strcmp(t[i].name, name) == 0 && strcmp(t[i].family, family) == 0) { while (true) { system("cls"); cout << "Π—Π°ΠΏΠΈΡΡŒ Π½ΠΎΠΌΠ΅Ρ€Π° " << i + 1 << endl; cout << "Ѐамилия: " << t[i].family << endl; cout << "Имя: " << t[i].name << endl; cout << "Π’Π΅Π»Π΅Ρ„ΠΎΠ½: " << t[i].phonenum << endl << endl; cout << "Π Π΅Π΄Π°ΠΊΡ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ запись?\n" << endl; cout << " 1 – Π”Π°" << endl; cout << " 0 – НСт" << endl; int edit; cin >> edit; cout << endl; if (edit == 0) break; if (edit == 1) { system("cls"); cout << "Π Π΅Π΄Π°ΠΊΡ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ запись ΠΏΠΎΠ΄ Π½ΠΎΠΌΠ΅Ρ€ΠΎΠΌ " << i + 1 << endl << endl; cout << "Π˜Π·ΠΌΠ΅Π½ΠΈΡ‚Π΅ Ρ„Π°ΠΌΠΈΠ»ΠΈΡŽ: " << endl << " " << t[i].family << " -> "; cin >> t[i].family; cout << "Π˜Π·ΠΌΠ΅Π½ΠΈΡ‚Π΅ имя: " << endl << " " << t[i].name << " -> "; cin >> t[i].name; cout << "Π˜Π·ΠΌΠ΅Π½ΠΈΡ‚Π΅ Ρ‚Π΅Π»Π΅Ρ„ΠΎΠ½: " << endl << " " << t[i].phonenum << " -> "; cin >> t[i].phonenum; cout << endl; f = fopen("DOC.dat", "wb"); fwrite(&t, sizeof(abonents), n, f); fclose(f); break; } } } break; } case '7': { system("cls"); f = fopen("DOC.dat", "rb"); fseek(f, 0, SEEK_END); fn = ftell(f) / sizeof(abonents); fclose(f); f = fopen("DOC.dat", "rb"); abonents t[n]; fread(&t, sizeof(abonents), n, f); fclose(f); f = fopen("abonents.txt", "wt"); fprintf(f, "Ѐамилия: |Имя: | Π’Π΅Π»Π΅Ρ„ΠΎΠ½:\n"); for (i = 0; i < n; i++) { for (int j = 0; j < 19 - strlen(t[i].family); j++) fprintf(f, " "); fprintf(f, t[i].family); fprintf(f, " | "); for (int j = 0; j < 15 - strlen(t[i].name); j++) fprintf(f, " "); fprintf(f, t[i].name); fprintf(f, " | "); for (int j = 0; j < 11 - strlen(t[i].phonenum); j++) fprintf(f, " "); fprintf(f, t[i].phonenum); fprintf(f, "\n"); } fclose(f); while (true) { int open; cout << "ΠžΡ‚ΠΊΡ€Ρ‹Ρ‚ΡŒ Ρ„Π°ΠΉΠ»" << endl; cout << " 1 – Π”Π°" << endl; cout << " 0 – НСт" << endl; cout << "Команда: "; cin >> open; cout << endl; system("cls"); if (open == 0) break; if (open == 1) { system("notepad.exe abonents.txt"); break; } } system("pause"); break; } case '8': { return 0; break; } } } while (menu != '8'); return 0; } 

Closed due to the fact that off-topic participants VTT , Enikeyschik , 0xdb , Jarvis_J , aleksandr barakin 28 December '18 at 9:47 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • β€œQuestions asking for help with debugging (β€œ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - VTT, Enikeyschik, 0xdb, Jarvis_J, aleksandr barakin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Remove the extra code, leaving only a piece with an error and give the error itself (as it is) - VTT
  • one
    And what do you think should happen with this declaration const int n; and such a command below n = ftell(f) / sizeof(abonents); ? Did CodeBlocks + gcc really not swear at this? - Vladimir
  • Why the question does not indicate specific lines of code in which errors occur? - AnT

2 answers 2

In C ++, it is not allowed to declare arrays with a size that is not a constant expression. Therefore your

 abonents t[n]; 

This is obviously not true and you cannot fix it by adding const anywhere. If you want the size of your array to depend on the file size - create your own array through new [] or, better, use std::vector .

    See - you have

     const int n; 

    But what is this n ? If this is a constant? You then write

     n = ftell(f) / sizeof(abonents); 

    but the constant can not be changed ...

    After that you declare

     abonents t[n]; 

    But C ++ does not allow declaring arrays of unknown size during compilation.

    If the last β€” with an array β€” GCC skips as an extension, then how you managed to assign a value to a constant is not clear.

    • In the question I wrote that when declaring a constant, I get an error and when I declare, via int, I get an error "the expression must have a constant value" - Max.
    • And what did I write to you about? The first error is a declaration of a constant without an initializer, the second is that, in fact, n not a constant, its value is unknown at compile time. What's wrong? - Harry