c++ - C++程序返回 “abort() has been called”,没有其他错误?

标签 c++

我正在尝试制作一个程序,该程序将向用户请求一个字符串并将其转换为字母金字塔(下面的示例)。
例如:如果用户输入字符串“12345”,则程序应显示:

    1                                                                                                                  
   121                                                                                                                 
  12321                                                                                                                
 1234321                                                                                                               
123454321
我的代码如下:(我尚未完成)
#include <iostream>
#include <string>
using namespace std;

int main() 
{
    //gets input
    cout << "Enter the string you would like to be processed" << endl;
    string input;
    cin >> input;
    
    //bottom layer = bottommost layer on the pyramid | element amount = elements in any given layer. | layerNum is to determine which layer computer is on
    int bottomLayer = input.size();
    int elementAmount;
    int layerNum;
    //layerNum keeps track of the pyramid layer the loop is currently on. 1 is the top, bottom = the string's length
    for (layerNum = 1; layerNum <= bottomLayer; layerNum++)
    {
        //signifies the amount of elements in a given layer. eg: for layer 2, the amount of elements would be 2*2-1=3
        elementAmount = layerNum * 2 - 1;
        //this loop prints the numbers up to where the order reverses
        for (int strElement = 0; strElement < layerNum; strElement++)
        {
            cout << input.at(strElement);
        }
        //starts on the first reverse letter to be printed and prints the rest
        for (int strElement = layerNum - 2; strElement < elementAmount; strElement--)
        {
            cout << input.at(strElement);
        }

    }
}

控制台窗口将按预期方式打开,我可以输入我的字符串。但是,这样做之后,它将返回第一个字母,并出现错误弹出窗口:
Debug Error!
abort() has been called
除此之外,没有错误消息甚至警告。对于一个执行,在第28行有一个警告比较无符号整数,这是这样的:
for (layerNum = 1; layerNum <= bottomLayer; layerNum++)
但是在我下次运行程序之后,它停止出现。关于如何使程序运行的任何想法?

最佳答案

第二个循环中存在一些逻辑错误。

  • strElement可能小于0,因为在int strElement = layerNum - 2中,layerNum从1开始。
  • strElement元素在每个循环中减少1,因此在经过几回合后,它必须小于零。
  • 循环条件应为strElement> -1,因此一旦strElement小于0,循环就会中断。

  • 注意:> -1可能不是正确的逻辑。但是strElement < elementAmount一旦通过,循环将使strElement始终小于0并无限循环运行。
        //starts on the first reverse letter to be printed and prints the rest
        for( int strElement = layerNum - 2; ; strElement > -1; strElement--)
        {
            cout << input.at(strElement);
        }
    

    关于c++ - C++程序返回 “abort() has been called”,没有其他错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62505922/

    相关文章:

    c++ - thread::hardware_concurrency() 检查返回零值

    c++ - 继承自 boost::variant 和模板化 AST

    c++ - 最快的语音识别库 C++

    c++ - cos 和 sin 没有提供预期的输出

    c++ - 为什么在将字符串标记与另一个字符串进行比较时会不断出现此错误?

    java - 为什么hypot()函数这么慢?

    c++ - 初学者如何处理用户输入类型不正确引起的异常

    c++ - Linux 内核事件 : timeval or timespec

    c++ - 必须调用对非静态成员函数的引用

    c++ - 为什么 std::make_move_iterator 适用于 vector<string> 但不适用于 vector<int>