【发布时间】:2020-01-16 08:14:07
【问题描述】:
我正在尝试将 X(x = 用户输入)数字打印到字符串数组(Alpha)的行中,如果该行中有“A”,它应该在该行中打印一些内容(字符串或随机数),例如“这条线有 "A" " 。我无法弄清楚我的代码有什么问题,就像代码中的这一行无法正常工作: (Alpha[dist(gen)] == Alpha[0])
它正在打印出 ("This line has "A" ) 在有其他字符串 Alphabets (S,PC etc) 的行中
#include <climits>
#include <ctime>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
using namespace std;
int main() {
int num;
const int SIZE = 6;
cout << "How many iterations: ";
cin >> num;
string Aplha[SIZE] = {"A", "D", "P", "E", "C", "S"};
random_device randNum;
mt19937 gen(randNum());
uniform_int_distribution<> dist(0, 5);
uniform_int_distribution<> distRange(INT_MIN, INT_MAX);
for(int i = 0; i < num; i++) {
cout << Alpha[dist(gen)];
if(Alpha[dist(gen)] == Alpha[0]) {
cout << " this line has A " << distRange(gen);
}
cout << endl;
}
return 0;
}
结果应该是……
如果一行有“A”那么:
A this line has A 123231315
D
P
D
S
A this line has A 845421541
C
我得到的结果是这样的......
How many iterations: 10
S
S this line has A 1950029843
S
C this line has A -145672138
A
C
D
C
E
S
【问题讨论】:
-
请提供预期的输出和您得到的输出,以便更轻松地提供帮助。不清楚是什么(在我的书中)。
-
dist(gen) 在你的循环中被使用了两次,这是一个做什么的函数?
-
@TedLyngmo hi ted,我刚刚编辑了我的原始帖子,请看最后一项,这就是我得到的结果..
-
@Areeb 太棒了!好多了