English 中文(简体)
以下代码中我正在获取一个公共汽车错误
原标题:I am getting a bus error in the following code

我在代码中发现公交车错误。 我试图用这个代码将数字转换成单词, 但我知道逻辑上有缺陷。 但在此之前, 当我在 < a href=> 上使用 g++ 编译和运行这个代码时, https:// en.wikipedia. org/ wiki/ Macintosh' rel = “ nofollow noreferrerr” > Mac < /a>, 我正试图让这个代码运行, 我正遇到一个公交车错误。 任何帮助都会受到欢迎 。

当我运行代码时, 我得到的代码会跟随输出。 我有调试信息可以追踪错误发生地点 。

    Enter a number:1234
    main 1:numbers are:234
    Function1: Number is 234
    two
    two hundred
    34Function2: Number is 34
    Function3: Number is 34
    Bus error: 10

#include <iostream>
#include <string>

using namespace std;
char *convert_number(int);

char *tens[] = {"", "ten", "twenty", "thirty", "forty",
                "fifty", "sixty", "seventy", "eighty", "ninety"};

char *words[] = {"zero", "one", "two", "three", "four",
                 "five", "six", "seven", "eight", "nine",
                 "ten", "eleven", "twelve", "thirteen", "fourteen",
                 "fifteen", "sixteen", "seventeen", "eighteen", "ninteen"};

char *place[] = {"", "hundred", "thouands", "million", "billion", "trillion"};


int main(int argc, char **argv)
{
    int number, conv_num, places;
    places = 1;
    char *string = new char[1000];
    char *temp_string = new char[100];
    cout << "Enter a number:";
    cin >> number;
    string = " ";
    if (number >= 1000)
    {
        while(number >= 1)
        {
            conv_num = number % 1000;
            cout << "main 1:numbers are:" << conv_num << endl;
            temp_string = convert_number(conv_num);
            string =strcat(string, temp_string);
            string =strcat(string, " ");
            number = 0; // (number-conv_num)/1000;
            cout << "main 2:numbers are:" << endl;
            //cout << conv_num << ":" << number << endl;
        }
    }
    else
    {
        string = convert_number(number);
        string = strcat(string, " ");
    }
    cout<<"Main: The word is :"<<string<<endl;
}

char *convert_number(int number)
{
    int divisor;
    char *word;
    word = new char[100];
    divisor = 10;
    cout << "Function1: Number is " << number << endl;

    if (number >= 100)
    {
        word = strcat(word, words[number/100]);
        cout << word << endl;
        word = strcat(word, " hundred ");
        cout << word << endl;
        number = number%100;
        cout << number;
    }
    cout << "Function2: Number is " << number << endl;

    if (number >= 20)
    {
        word = strcat(word, tens[number/10]);
        word = strcat(word, " ");
        if (number%divisor >= 1)
        {
            word=strcat(word, words[number%divisor]);
            word =strcat(word, " ");
        }
    }
    cout << "Function3: Number is " << number << endl;

    if (number < 20)
    {
        word = strcat(word, words[number]);
        word = strcat(word, " ");
    }
    cout << "Returning word:" << word;
    return word;
}
最佳回答

您获取公交车错误的原因是您试图写入不可写区域( 即字符常数, 并且也超过字符的结尾) ; 这是未定义的行为 。

// Good: allocate 100 bytes to string 
char *string = new char[100];

// Bad! Compiler warns you that assigning character const to char* is wrong.
// It does not tell you that you ve just leaked the 100 bytes that you allocated
/// before, but that s also true.
string=" ";

// ... some more code, and then
string = strcat(string,temp_string); // <<== HERE is the problem!

strcat 的呼叫试图写入 string 的终止零, 然后继续写到字符串常数的末尾。 这是未定义的行为, 所以程序会崩溃 。

要解决这个问题, 您需要将 < code> " 复制为 < code> string , 而不是将其指定为 < code> string 指针 。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签