这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
This is my attempt at having a user input a value into an array. Then to covert it to an amount of asterisks printed
然而,当试图将用户值分配给星数组,然后为每1000人打印星号时,这就是我想到的,并且在尝试以这种方式分配时似乎遇到了错误。我对C++编程只有几周的时间,从事工业维护工作,我的编程知识主要来源于PLC。用这种方式给数组赋值有什么技巧吗。
#include<iostream>
#include<string>
using namespace std;
int pop[5], stars[5];
string out[] = { 0, 0, 0, 0, 0 };
int main()
{
cout << "nttPlease enter the population of 4 cities:";
cout << "nttPopulation of city 1: "; cin >> pop[1];
cout << "nttPopulation of city 2: "; cin >> pop[2];
cout << "nttPopulation of city 3: "; cin >> pop[3];
cout << "nttPopulation of city 4: "; cin >> pop[4];
stars[1] = pop[1] / 1000, stars[2] = pop[2] / 1000, stars[3] = pop[3] / 1000, stars[4] = pop[4], out[1].assign(stars[1], "*"), out[2].assign(stars[2], "*"), out[3].assign(stars[3], "*"), out[4].assign(stars[4], "*");
cout << "nntCity 1: " << out[1];
cout << "ntCity 1: " << out[2];
cout << "ntCity 1: " << out[3];
cout << "ntCity 1: " << out[4];
return 0;
}
首先,用分号分隔您的作业,每个作业使用一行。
stars[1] = pop[1] / 1000;
stars[2] = pop[2] / 1000;
// ...
应该使用字符(char
(而不是c字符串(char*
(作为std::string::assign
:的第二个参数
out[1].assign(stars[1], '*') // correct
out[1].assign(stars[1], "*") // wrong
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- boost::asio如何生成多个协同程序,然后加入它们
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 在std::thread中,joinable()然后join()线程安全吗
- 结构和双指针隐藏在其他结构中,多层混淆
- C++:如何读取分离变量,然后读取向量
- 为什么我的递归函数按降序打印,然后按升序打印?
- 等待整个 omp 块完成,然后再调用第二个函数
- CMake:如何将库 A 链接到库 B,然后将可执行文件链接到库 A
- 在 cpp 文件中隐藏采用模板参数引用的方法
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 参数包构造函数在类模板中隐藏用户定义的转换
- 如何使变量从 x 到 y,然后从 y 返回到 x 并始终重复该过程
- 如何等待窗口隐藏在Qt中?
- 图像在隐藏时消失,然后重新显示ListView
- Qt:隐藏然后显示QQuickView防止未来的鼠标事件
- Qt在运行时改变对话框的模式,而不是第一次隐藏它,然后再显示