Home c++ Expression must have constant value

Expression must have constant value

Author

Date

Category

I wrote a program on CodeBlocks, everything works fine, but when switching to Visual Studio I started to give an error “the expression must have a constant value. Tell me how to fix it or what is the essence of the error, because when a constant is designated, another error occurs” the expression must be valid to change the left-handed value “

# include & lt; iostream & gt;
#include & lt; windows.h & gt;
#include & lt; stdio.h & gt;
#include & lt; iomanip & gt;
#include & lt; conio.h & gt;
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 & gt; 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 & lt; & lt; "1 - View the entire directory" & lt; & lt; endl;
  cout & lt; & lt; "2 - Add new entry" & lt; & lt; endl;
  cout & lt; & lt; "3 - Search for a subscriber by first and last name and output the report to a text file" & lt; & lt; endl;
  cout & lt; & lt; "4 - Cleanup \ Create File" & lt; & lt; endl;
  cout & lt; & lt; "5 - Delete entry" & lt; & lt; endl;
  cout & lt; & lt; "6 - Edit Entry" & lt; & lt; endl;
  cout & lt; & lt; "7 - Output of the directory to a text file" & lt; & lt; endl;
  cout & lt; & lt; "8 - Exit" & lt; & lt; endl;
  cin & gt; & gt; 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 (& amp; t, sizeof (abonents), n, f);
    fclose (f);
cout & lt; & lt; "Subscriber:" & lt; & lt; n & lt; & lt; '\ n' & lt; & lt; endl;
    cout & lt; & lt; "| Last name: | First name: | Phone: |" & lt; & lt; endl;
    cout & lt; & lt; "| | | |" & lt; & lt; endl;
    for (i = 0; i & lt; n; i ++)
    {
      cout & lt; & lt; setiosflags (ios :: left) ;;
      cout & lt; & lt; "|";
      cout & lt; & lt; setw (20) & lt; & lt; t [i] .family & lt; & lt; "|";
      cout & lt; & lt; setw (20) & lt; & lt; t [i] .name & lt; & lt; "|";
      cout & lt; & lt; setw (15) & lt; & lt; t [i] .phonenum & lt; & lt; "|" & lt; & lt; endl;
    }
    cout & lt; & lt; "| |" & lt; & lt; endl;
    _getch ();
    system ("cls");
    break;
    break;
  }
  case '2': {
    system ("cls");
    abonents t;
    cout & lt; & lt; "Surname: "; cin & gt; & gt; t.family;
    cout & lt; & lt; "Name: "; cin & gt; & gt; t.name;
    cout & lt; & lt; "Telephone: "; cin & gt; & gt; t.phonenum;
    cout & lt; & lt; '\ n';
    f = fopen ("DOC.dat", "ab");
    fwrite (& amp; t, sizeof (abonents), 1, f);
    fclose (f);
    break;
  }
  case '3': {
    system ("cls");
    char name [20]; char family [20];
    cout & lt; & lt; "Enter your Surname:"; cin & gt; & gt; family;
    cout & lt; & lt; "Enter your name: "; cin & gt; & gt; 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 (& amp; t, sizeof (abonents), n, f);
    fclose (f);
    f = fopen ("SearchAbonents.txt", "wt");
    for (i = 0; i & lt; n; i ++)
      if (strcmp (t [i] .family, family) == 0 & amp; & amp; strcmp (t [i] .name, name) == 0)
      {
fprintf (f, "Subscriber's phone number");
        fprintf (f, family);
        fprintf (f, "");
        fprintf (f, name);
        fprintf (f, "- & gt;");
        fprintf (f, t [i] .phonenum);
      }
    fclose (f);
    cout & lt; & lt; '\ n' & lt; & lt; "All Subscriber Numbers" & lt; & lt; family & lt; & lt; name & lt; & lt; "entered in the SearchAbonents.txt file." & lt; & lt; endl;
    system ("notepad.exe SearchAbonents.txt");
    cout & lt; & lt; endl;
    break;
  }
  case '4': {
    int del;
    while (true)
    {
      system ("cls");
      cout & lt; & lt; "Are you sure you want to delete the entry?" & lt; & lt; endl;
      cout & lt; & lt; "1 - Yes" & lt; & lt; endl;
      cout & lt; & lt; "0 - No" & lt; & lt; endl;
      cin & gt; & gt; del;
      cout & lt; & lt; endl;
      if (del == 1)
      {
        f = fopen ("DOC.dat", "wb");
        fclose (f);
        cout & lt; & lt; "Entry deleted! \ N" & lt; & lt; endl;
        break;
      }
      if (del == 0)
      {
        cout & lt; & lt; "Deletion canceled. \ N" & lt; & lt; endl;
        break;
      }
    }
    break;
  }
  case '5': {
    system ("cls");
    char name [20]; char family [20];
    cout & lt; & lt; "Enter your surname:"; cin & gt; & gt; family;
    cout & lt; & lt; "Enter your name: "; cin & gt; & gt; 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 (& amp; t, sizeof (abonents), n, f);
    fclose (f);
    f = fopen ("DOC.dat", "wb");
    for (i = 0; i & lt; n; i ++)
if (strcmp (t [i] .name, name)! = 0 & amp; & amp; strcmp (t [i] .family, family)! = 0)
        fwrite (& amp; t [i], sizeof (abonents), 1, f);
      else
        cout & lt; & lt; endl & lt; & lt; "Phone number deleted:" & lt; & lt; t [i] .phonenum & lt; & lt; endl;
    fclose (f);
    cout & lt; & lt; 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 (& amp; t, sizeof (abonents), n, f);
    fclose (f);
    char name [20]; char family [20];
    cout & lt; & lt; "Enter the person whose entry you want to change:" & lt; & lt; '\ n' & lt; & lt; endl;
    cout & lt; & lt; "Surname :"; cin & gt; & gt; family;
    cout & lt; & lt; "Name: "; cin & gt; & gt; name;
    for (i = 0; i & lt; n; i ++)
      if (strcmp (t [i] .name, name) == 0 & amp; & amp; strcmp (t [i] .family, family) == 0)
      {
        while (true)
        {
          system ("cls");
          cout & lt; & lt; "Record number" & lt; & lt; i + 1 & lt; & lt; endl;
          cout & lt; & lt; "Last name:" & lt; & lt; t [i] .family & lt; & lt; endl;
          cout & lt; & lt; "Name:" & lt; & lt; t [i] .name & lt; & lt; endl;
          cout & lt; & lt; "Phone:" & lt; & lt; t [i] .phonenum & lt; & lt; endl & lt; & lt; endl;
          cout & lt; & lt; "Edit entry? \ N" & lt; & lt; endl;
          cout & lt; & lt; "1 - Yes" & lt; & lt; endl;
          cout & lt; & lt; "0 - No" & lt; & lt; endl;
          int edit;
          cin & gt; & gt; edit; cout & lt; & lt; endl;
          if (edit == 0) break;
          if (edit == 1)
          {
system ("cls");
            cout & lt; & lt; "Edit record numbered" & lt; & lt; i + 1 & lt; & lt; endl & lt; & lt; endl;
            cout & lt; & lt; "Change your last name:" & lt; & lt; endl & lt; & lt; "" & lt; & lt; t [i] .family & lt; & lt; "- & gt;"; cin & gt; & gt; t [i] .family;
            cout & lt; & lt; "Change the name:" & lt; & lt; endl & lt; & lt; "" & lt; & lt; t [i] .name & lt; & lt; "- & gt;"; cin & gt; & gt; t [i] .name;
            cout & lt; & lt; "Change phone:" & lt; & lt; endl & lt; & lt; "" & lt; & lt; t [i] .phonenum & lt; & lt; "- & gt;"; cin & gt; & gt; t [i] .phonenum;
            cout & lt; & lt; endl;
            f = fopen ("DOC.dat", "wb");
            fwrite (& amp; 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 (& amp; t, sizeof (abonents), n, f);
    fclose (f);
    f = fopen ("abonents.txt", "wt");
    fprintf (f, "Last name: | First name: | Phone: \ n");
    for (i = 0; i & lt; n; i ++)
    {
      for (int j = 0; j & lt; 19 - strlen (t [i] .family); j ++) fprintf (f, "");
      fprintf (f, t [i] .family);
      fprintf (f, "|");
      for (int j = 0; j & lt; 15 - strlen (t [i] .name); j ++) fprintf (f, "");
      fprintf (f, t [i] .name);
      fprintf (f, "|");
      for (int j = 0; j & lt; 11 - strlen (t [i] .phonenum); j ++) fprintf (f, "");
      fprintf (f, t [i] .phonenum);
      fprintf (f, "\ n");
    }
    fclose (f);
while (true)
    {
      int open;
      cout & lt; & lt; "Open file" & lt; & lt; endl;
      cout & lt; & lt; "1 - Yes" & lt; & lt; endl;
      cout & lt; & lt; "0 - No" & lt; & lt; endl;
      cout & lt; & lt; "Command: "; cin & gt; & gt; open;
      cout & lt; & lt; 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;
}

Answer 1, authority 100%

The C++ language does not allow the declaration of arrays with a size that is not a constant expression. Therefore your

abonents t [n];

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 size of the file – create your array via new [] or, better, use std :: vector .


Answer 2, authority 90%

Look – you have

const int n;

But what is n equal to? If it’s a constant? You then write

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

but the constant cannot be changed …

Then you declare

abonents t [n];

But C++ does not allow to declare arrays of unknown size at compile time.

If the latter – with an array – GCC passes as an extension, then how you contrived to assign a value to a constant is unclear.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions