小编Tiv*_*ivi的帖子

C++ 我应该用什么代替#define

我想用 cin 运算符从控制台给出一个值,而不是使用 #define N 6。我试过了,但我收到“表达式必须具有常量值”错误消息。我应该如何通过其他方式做到这一点?

谢谢你的回答!

示例代码:

#include <iostream>
#include <string>
#include <cstdlib> 
#include <ctime>
#define N 6
using namespace std;

typedef struct person {
    int roll;
    string name;
} Person;

int main() {
    int numberofperson;
    cout << "Number of people: "; cin >> numberofperson;
    srand(time(NULL));
    Person people[N];
    int i;
    for (i = 0; i < numberofperson; i++)    {
        cout << "Write the " << i + 1 << ". name of the person: ";
        cin  >> people[i].name;
        people[i].roll …
Run Code Online (Sandbox Code Playgroud)

c++ expression constants

3
推荐指数
1
解决办法
156
查看次数

标签 统计

c++ ×1

constants ×1

expression ×1