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; }
const int n;and such a command belown = ftell(f) / sizeof(abonents);? Did CodeBlocks + gcc really not swear at this? - Vladimir