简体   繁体   English

C-strings,如何确定字符的长度是否在 6 到 10 之间?

[英]C-strings, how to determine if the characters are between 6 and 10 long?

I am beginner programmer.我是初级程序员。 I am writing a program to check if the password, as a c-string, is between 6 and 10 characters long.我正在编写一个程序来检查密码(作为 c 字符串)的长度是否在 6 到 10 个字符之间。 If not, the user has to re-enter the password until it meets the requirement.如果不是,则用户必须重新输入密码,直到满足要求。 For the input validation part of the program, it works when the password is less than 6 characters -- telling the user to re-enter the password.对于程序的输入验证部分,它在密码少于 6 个字符时起作用——告诉用户重新输入密码。 But, when it is more than 10 characters long, it doesn't display an error that it is more than 10 characters long.但是,当长度超过 10 个字符时,它不会显示长度超过 10 个字符的错误。 How would I fix this?我将如何解决这个问题? Thanks for your input.感谢您的输入。

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
const int SIZE = 12; // Maximum size for the c-string
char pass[SIZE];   // to hold password c-string.
int length;


// get line of input
cout << "Enter a password between 6 and " << (SIZE - 2) << "characters long:\n";
cin.getline(pass, SIZE);


length = strlen(pass);

while (length < 6 || length > 10)
{
cout << "Error: password is not between 6 and " << (SIZE - 2) << " characters long.\n"
     << "Enter the password again: ";
cin.getline(pass, SIZE);
    length = strlen(pass);
}

return 0;
}

I'm not sure why you're using cstrings, since normal strings work just as well.我不确定您为什么使用 cstring,因为普通字符串也能正常工作。 Here is how I would rewrite it:这是我将如何重写它:

string pass;
cout << "Enter password: ";
cin  >> pass;
while (pass.length() > 10 || pass.length() < 6)
{
  cout << "Not the right length, try again: ";
  cin  >> pass;
}

If you need to have the password in an array, use this:如果您需要将密码保存在数组中,请使用以下命令:

//To use the c_string version of it, type 
pass.c_str();
return 0;

This is untested however, but it should work.然而,这是未经测试的,但它应该可以工作。

Your not allowing a password over 10 characters.您不允许密码超过 10 个字符。 C strings are null terminated so 10 chars plus null byte is 11. You will only ever get a 10 length. C 字符串是空终止的,所以 10 个字符加上空字节是 11。你只会得到 10 的长度。 Ps I would suggest Ps 我会建议

    char password[SIZE+2]

Makes it clearer that the constant is the maximum length of the password更清楚的是常量是密码的最大长度

EDIT编辑

I agree with the other posts that std::string is a better option but it's also important especially for a biginer to understand problems not just accept solutions because that's what should be done我同意其他帖子,std::string 是一个更好的选择,但它也很重要,尤其是对于一个 biginer 来说,理解问题而不仅仅是接受解决方案,因为这是应该做的

const int SIZE = 11;
char password[SIZE];

There is no way the password can store more than 11 chars (where the last char is NULL terminator '\\0' ). password无法存储超过 11 个字符(其中最后一个字符是 NULL 终止符'\\0' )。

cin.getline(password, SIZE);

There is no way you will get more than you asked, which is SIZE .你不可能得到比你要求的更多的东西,那就是SIZE

A quick fix would be changing it to char password[SIZE + 1];快速解决方法是将其更改为char password[SIZE + 1]; . . Why?为什么? Because it will be capable to store more chars than your wanted, thus allowing you to check if input is too long.因为它能够存储比您想要的更多的字符,从而允许您检查输入是否太长。

Of course, you have to change your cin.getline(... to cin.getline(password, SIZE + 1); as well.当然,您还必须将cin.getline(...更改为cin.getline(password, SIZE + 1);

You forgot to update your length in that if too.你忘了更新您的lengthif太。 I would suggest do something like this instead, making it cleaner:我建议做这样的事情,让它更干净:

const int SIZE = 11;
char password[SIZE + 1];

while (true) // Create a loop
{
    cout << "Enter a password between 6 and "
         << (SIZE - 1) << " characters long:\n";

    // If bad input, discard the remaining input
    if (!cin.getline(password, SIZE + 1))
    {
        cin.clear();
        cin.ignore(INT_MAX, '\n');
    }

    int length = strlen(password);

    // Check input validation if password is bet. 6 and 10 characters
    if (length < 6 || length > 10)
    {
        cout << "Password is not between 6 " << (SIZE - 1)
             << " characters.\n Please enter your password again:\n";
    }
    else { break; } // Break loop if input vaild
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM