简体   繁体   English

当 char 数组过满时,为什么我的程序 go 进入无限循环?

[英]Why does my program go into endless loop when char array is overfilled?

I'm attempting to write a program in C++ that lets user enter a positive integer and a name into array with fixed size and print them out.我正在尝试在 C++ 中编写一个程序,让用户输入一个正的 integer 和一个名称到具有固定大小的数组中并将它们打印出来。 The program seems to work correctly when I enter correct data from the first time of launching the program.当我从第一次启动程序时输入正确的数据时,该程序似乎可以正常工作。

The problem I'm experiencing is when entering incorrect value for Char array (Exceeding the character count).我遇到的问题是为 Char 数组输入不正确的值(超过字符数)。 If I enter incorrect value for char array, it gives a warning message as expected, but once I enter the correct size word, the program goes into endless loop of asking for already entered values.如果我为 char 数组输入了不正确的值,它会按预期给出警告消息,但是一旦我输入了正确大小的字,程序就会进入无限循环,询问已经输入的值。 My code is as follows:我的代码如下:

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    int count = 1;
    int num = 0; // variable for positive integer
    char name[31]; //char array to store name inside

    while(count != 0){

        cout << "Please enter the number: " << endl;
        cin >> num;
        while(num < 1){
            cout << "Number must be a positive integer!" << endl;  //Loop that checks whether the 
            cin >> num;                                           // entered number is a positive 
        }                                                        //  integer.

        cout << "Please enter the name: " << endl;
        cin >> name;
        while(strlen(name) > 30){                                        //Loop that checks whether the
           cout << "Name exceeds the allowed character count!" << endl; //entered name exceeds the
           cin >> name;                                                // allowed character count.
        }

        --count;
    };

    cout << num << " " << name;

    return 0;
}

I'm trying to understand why does my program go into endless loop if I enter an incorrect value first and the correct value afterwards?我试图理解为什么我的程序 go 如果我先输入错误的值然后输入正确的值会进入无限循环? Is it something to do with exceeding the character count for array?是否与超出数组的字符数有关?

Any help would be appreciated, many thanks!任何帮助将不胜感激,非常感谢!

This loop is problematic:这个循环是有问题的:

 while(strlen(name) > 30) {                                        //Loop that checks whether the
       cout << "Name exceeds the allowed character count!" << endl; //entered name exceeds the
       cin >> name;                                                // allowed character count.
 }

The strlen() will return 31 if there are 31 characters followed by a '\0' .如果有 31 个字符后跟'\0' ,则strlen()将返回 31。 So that's already more than your 31-element long array can hold.所以这已经超过了你的 31 个元素的长数组可以容纳的东西。 But that's not the smallest issue - you are running into an undefined behavior territory as soon as you load more chars from cin than your array's size - as of C++17, cin will happily write past the capacity of a c-style array if you tell it to do so.但这不是最小的问题——一旦你从cin加载的字符多于数组的大小,你就会遇到一个未定义的行为领域——从 C++17 开始,如果你告诉它, cin会很高兴地写入超过 c 样式数组的容量这样做。

I suggest you use std::string s instead of raw C arrays.我建议您使用std::string代替原始 C arrays。 They take care of the null-terminated string technicalities for you.他们为您处理空终止字符串的技术问题。

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

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