随机密码生成器的 C++ 循环错误 "Exited With Non-Zero Status"
C++ Loop Error "Exited With Non-Zero Status" for Random Password Generator
所以我正在尝试学习 C++ 中的循环和条件,所以我决定编写一个程序来为用户生成随机密码。由于某种原因,代码可能工作了 1/5 次,但其余时间它只给了我 "Exited With Non-Zero Status"。
谢谢,
埃文
#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 数组,并且索引范围有误。
C 数组,例如 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 毫秒的暂停。无论如何,您都会看到您的结果是正确随机化的。
所以我正在尝试学习 C++ 中的循环和条件,所以我决定编写一个程序来为用户生成随机密码。由于某种原因,代码可能工作了 1/5 次,但其余时间它只给了我 "Exited With Non-Zero Status"。 谢谢, 埃文
#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 数组,并且索引范围有误。
C 数组,例如 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 毫秒的暂停。无论如何,您都会看到您的结果是正确随机化的。