这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号

This is my attempt at having a user input a value into an array. Then to covert it to an amount of asterisks printed

本文关键字:隐藏 然后 用户 输入 数组      更新时间:2023-10-16

然而,当试图将用户值分配给星数组,然后为每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