为什么我的C ++程序只打印一个字符?

时间:2015-12-10 20:47:19

标签: c++

此程序假设生成密码并与用户输入的内容进行比较,如果匹配则会中断while循环并输出用户的输入,但由于某种原因,生成的密码只是一个字符。我是C ++的新手,我刚开始就像上周五一样。

#include <iostream>
#include <unistd.h>
using namespace std;

int main()
{
    string Password, Passwords;

    cout << "Enter a password: ";
    getline(cin, Password);
    sleep(.7);
    system("clear");

    while(Password.compare(Passwords)!= 0)
    {
        for (int x = 0; x <= Password.length(); x++)
        {
            for (char Alpha = 'a'; Alpha <= 'z'; Alpha++)
            {
                if(Alpha == 'z')
                {
                    Alpha = 'a';
                }
                for(int I=0; I <= 10; I++)
                {
                    Passwords = Alpha + I;
                    system("clear");
                    sleep(.7);
                    cout << Passwords <<endl;
                }

            }

        }


    }

    cout << "Password found: " << Passwords <<endl;
    return 0;
}

2 个答案:

答案 0 :(得分:0)

在评论中经过漫长的回顾和前进后,OP解释了他的目的是什么。生成与输入大小相同的随机单词,并在与输入匹配时停止。

此代码可以满足您的需求。它位于c++14中,因此您需要最近的编译器并设置c++14选项。请注意实际使用随机

#include <iostream>
#include <string>
#include <random>
#include <algorithm>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

class RandomCharGenerator {
private:
  static std::string s_chars_;

private:
  std::random_device rd_{};
  std::default_random_engine r_eng_{rd_()};
  std::uniform_int_distribution<int> char_dist_{
      0, static_cast<int>(s_chars_.size())};

public:
  RandomCharGenerator() = default;

  auto getRandomChar() -> char { return s_chars_[char_dist_(r_eng_)]; }
  auto setRandomString(std::string &str) -> void {
    std::generate(std::begin(str), std::end(str),
                  [this] { return this->getRandomChar(); });
  }
};

std::string RandomCharGenerator::s_chars_ = "abcdefghijklmnopqrstuvwxyz";

auto main() -> int {
  RandomCharGenerator rand_char;

  auto input = std::string{};

  cin >> input;
  auto generated = std::string(input.size(), ' ');

  do {
    rand_char.setRandomString(generated);
    cout << generated << endl;
  } while (input != generated);

  cout << "We generated what you input" << endl;
  return 0;
}

对于超过4个字符的输入,生成输入需要很长时间。

Ideone demo

要理解为什么Passwords中只有1个字符:

Passwords = Alpha + I;

AlphacharIint。他们的总和是int。在分配给char Passwords时,会将其转换为string。所以Passwords现在是一个只由一个字符组成的字符串。

目前还不清楚实际的代码行应该做什么,因此无法告诉您修复的内容。也许你打算附加到Passwords。那你应该写Passwords += Alpha + I

答案 1 :(得分:0)

下面的代码是我想要做的一个例子。我没有在下面编写代码,只是给你以及在上面的代码中尝试做的例子

#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;

int main()
{
    string password;
    string Generated;

    cout << "Password to find: ";
    cin >> password;

    char Alpha[]={'a'-1,'a','a','a','a','a','a','a','a','a'};

    while( password.compare(Generated) != 0 )
    {

    Alpha[0]++;
    for(int x=0;x<password.length();x++)
    {
       if (Alpha[x] == 'z'+1)
            {
                Alpha[x] = 'a';
                Alpha[x + 1]++;
            }
    }


    Generated=Alpha[password.length()-1]; 

    for(int i=password.length()-2;  i>=0  ;  i-- )
        Generated+= Alpha[i]; 

        system("clear");

        cout << "Trying: "<< Generated << endl; 
    }

    system("clear");
    sleep(1);
    cout <<"Access Granted: "<< Generated << endl; 
    return 0;
}
相关问题