如何解决尽管使用了动态数组 C++,表达式并未计算为常量
我有一个程序,要求用户输入他们正在学习的课程数量。它将其作为字符串输入,以便检查输入是否为整数。然后使用 stoi 转换为 int 并创建一个数组,以便它可以包含所有课程名称。整数变量是这个数组的长度。我的问题是 MSBuild 告诉我表达式不计算为常量。下面的链接告诉我改用动态数组,这是我将其更改为的内容,但我仍然遇到完全相同的问题。
expression did not evaluate to a constant- 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): [] 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。