随机密码生成器的C++循环错误"以非零状态退出"
C++ Loop Error "Exited With Non-Zero Status" for Random Password Generator
因此,我正在尝试在C 中学习循环和条件,因此我决定编写一个为用户生成随机密码的程序。由于某种原因,该代码可能工作1/5次,但其余时间才使我"以非零状态退出"。谢谢,evin
#include <iostream>
#include <cstdlib>
#include <chrono>
#include <thread>
#include <time.h>
using namespace std;
int main()
{
using namespace std::this_thread;
using namespace std::chrono;
// Vars
string lett;
int input;
string password("");
string lettArray [] = {"a", "b", "c", "d", "e","f", "g", "h", "i", "j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
// Prompt
cout << "How long would you like your password to be?";
cin >> input;
// Loop
for(int i = 0; i < input; i++)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
srand((time_t)ts.tv_nsec);
int random = (rand() % 26 + 1);
lett = lettArray[random];
password = password + lett;
sleep_for(milliseconds(10));
cout << "." << endl;
if (random == 0 )
break;
}
// Output
cout << password << endl;
return 0;
}
此程序随机失败(双关语)是因为您使用的是c-arrays,并且索引范围错误。
c-array,例如lettarray,请勿检查是否违反了数组界限。在您的示例中,如果运行此程序,lettArray[26]
将为您提供分割故障,因为该内存地址中没有string
元素。C阵列没有检查界限,因此很难知道出了什么问题。这在复杂的程序中可能会变得特别棘手,因为如果碰巧在该内存地址中有某些东西,您可以获得毫无意义的结果。
更好的实现是使用std::vector
:
#include <iostream>
#include <cstdlib>
#include <chrono>
#include <thread>
#include <time.h>
#include <vector>
using namespace std;
int main()
{
// You don't need these two lines
//using namespace std::this_thread;
//using namespace std::chrono;
// Vars
string lett;
int input;
string password("");
// Vector index range: 0-25
vector<string> lettArray = {"a", "b", "c", "d", "e","f", "g", "h", "i", "j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
// You only need to initialise the seed once.
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
srand((time_t)ts.tv_nsec);
// Prompt
cout << "How long would you like your password to be?";
cin >> input;
// Loop
for(int i = 0; i < input; i++)
{
int random = (rand() % 26 ); // You get results between 0-25
// Using lettArray.at(26) will result in a crash with a clear
// message that bounds are violated
lett = lettArray.at(random);
password = password + lett;
cout << "." << endl;
// Don't see a reason for this if statement
// if (random == 0 ){
// continue;
//}
}
// Output
cout << password << endl;
return 0;
}
我还将随机种子移到循环外(您只需要一次),也没有理由将10毫秒的暂停。您会看到您的结果是正确随机的。
相关文章:
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- 为什么系统函数总是在C++中返回已转移的退出状态?
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 试图在崇高中奔跑. 错误 已发生: 收集2.exe: 错误: ld 返回 1 退出状态
- collect2:错误:ld 返回 1 个退出状态未定义的引用
- C++ collect2:错误:ld 返回 1 退出状态
- 找不到 -llibmysqld Collect2.exe:错误:ld 返回 1 退出状态
- 每次我要使用外部资源时,我都会得到:collect2。exe:错误 ld 返回 1 退出状态
- 当出现错误ld return 1退出状态时,如何编译程序
- 无法访问 Arduino 结构字段。错误"退出状态 1。xxxx 不命名类型"
- "Extern"对象问题:错误:Id 返回 1 个退出状态
- c++ (C::B) 错误:ld 返回 1 个退出状态
- 在 c++ 中捕获 shell 脚本退出状态
- 对"displayForStudent(int, int const*, double const*, int)"的未定义引用 collect2.exe:错误:ld 返回 1 个退出状态
- 使用类[错误] LD返回1退出状态
- 如何解决退出状态 1 错误
- 错误 (Id) 返回 1 个退出状态
- 退出状态 1 令牌之前的预期主表达式'.'
- 错误:LD返回1个退出状态-CodeBlocks