首页 > 解决方案 > 尽管使用了动态数组 C++,但表达式未计算为常量

问题描述

我有一个程序要求用户输入他们正在学习的课程数量。它将它作为一个字符串接收,因此它可以检查输入是否为整数。然后 stoi 用于转换为 int 并创建一个数组,以便它可以包含所有课程名称。整数变量是这个数组的长度。我的问题是 MSBuild 告诉我表达式不会计算为常数。下面的链接告诉我改用动态数组,这就是我将其更改为的,但我仍然遇到同样的问题。

表达式未计算为常量- c++

方法.h

#pragma once
#include <iostream>
#include <string>

// Variables
int number_of_course_taken;

// Arrays
std::string* course_names = new std::string[number_of_course_taken];


// Methods
void course_input(std::string course_names[number_of_course_taken]);

main.cpp 片段

   // Taking amount of courses user is enrolled in
string courses = "";
int i = 0;
int count = 0;
while (true)
{
    // test condition to check for int status works for strings but doubles trick the system.
    cout << "How many courses do you want to take?\n If this is not the first time you are seeing this prompt,\n that means you entered a faulty input. Enter a number \n between 1 and 8.";
    cin >> courses;

    // Iteratres to every character checking if it is a digit or not
    // this deduces whether user inputted integer or something else
    for (i = 0; i < courses.length(); i++) 
    {
        if (isdigit(courses[i]) == false) 
        {
            count=1;
            break;
        }
        else
            count=0;
 
    } if(count==0)
    {
        cout << "Number inputted is an Integer\n";
        int temp = std::stoi(courses);
        number_of_course_taken = temp;
        if (number_of_course_taken >= 1 && number_of_course_taken <= 8)
        {
            break;
        }
    }

    else
        cout << "Number inputted is not an integer\n";
}
void course_input(string course_names[number_of_course_taken]);

方法.cpp

#include "methods.h"
#include <iostream>

using namespace std;

// Method that will take user input and store each into an array
void course_input(string course_names[number_of_course_taken])
{
    string course_name_input;
    int j = 0;
    // Run until user enters amount that coresponds with number of courses
    while (j!= number_of_course_taken)
    {
        j = j + 1;
        // error checking to make sure user inputs course name less than 20
        while (true)
        {
            cout << "Enter a course name. It must be greater than 0 characters and less than 20 characters.\n";
            cin.ignore();
            getline(cin,course_name_input);
            if (course_name_input.length() > 20 || course_name_input.length() <= 0)
            {
                cout << "You entered a course that either has 0 characters or more than 20. Please try again\n";
            }
            else
            {
                cout << "The course name fits the criteria.\n";
                course_names[j] = course_name_input;
                cout << "For testing purposes: " << course_names;
            }
        }
    }
}

输出消息和屏幕截图

  methods.h(13, 44): [C2131] expression did not evaluate to a constant
  methods.h(13, 44): [] see usage of 'number_of_course_taken'
  HonorRollProcedural.cpp(83, 43): [C2131] expression did not evaluate to a constant
  HonorRollProcedural.cpp(83, 43): [] see usage of 'number_of_course_taken'
  methods.h(13, 44): [C2131] expression did not evaluate to a constant
  methods.h(13, 44): [] see usage of 'number_of_course_taken'
  methods.cpp(7, 39): [C2131] expression did not evaluate to a constant
  methods.cpp(7, 39): [] see usage of 'number_of_course_taken'
  methods.cpp(28, 52): [C3863] array type 'std::string [number_of_course_taken]' is not assignable

报错信息截图

标签: c++arraysvisual-c++msbuildjetbrains-ide

解决方案


推荐阅读