gpt4 book ai didi

c++ - 如何访问 C++ 字符矩阵的一行?

转载 作者:太空宇宙 更新时间:2023-11-04 15:30:32 24 4
gpt4 key购买 nike

经过多年的 matlab 学习后,我正在重新学习 C++。这是我写的一些代码

char  couts[3][20]={"Area of Rectangle: ","Area of Triangle: ","Area of Ellipse: "};
char C[20];
for (int i = 0; i < 3; i++) {
C=couts[i];
cout << C;
//code that calculates and couts the area
}

显然,这是打印那一行 couts 的错误方法,但在尝试了许多变体和谷歌搜索后,我无法弄清楚我做错了什么。 :(

最佳答案

你可能应该使用 C++ 特性而不是旧的 C 习语:

#include <iostream>
#include <array>
#include <string>

const std::array<std::string, 3> couts{ "Area of Rectangle: ","Area of Triangle: ","Area of Ellipse: " };

int main()
{
std::string C;
for (int i = 0; i < couts.size(); i++) {
C = couts[i];
std::cout << C << "\n";
//code that calculates and couts the area
}
}

关于c++ - 如何访问 C++ 字符矩阵的一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54533197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com