而/ for循环错误

时间:2013-09-10 05:07:19

标签: c++ for-loop while-loop

嗨我早些时候发了帖子,虽然我觉得它很模糊,所以我会再试一次并用不同的方式说出来。

我正在尝试编写一个程序,它接受两个英文数字的输入并将它们转换为整数(使用数组),运行计算然后将它们转换回英文单词。

例如:

用户输入: sixty_six + six 输出: seventy_two

用户输入: 四十四 - 四 输出: 40

但是我一直无法在我的代码中发现问题,花了最后一小时左右试图找到问题,尽管我无法做到。所以希望有人可以帮助我。

我的代码cout在那里如此trank在循环中它出错了,它似乎按预期运行直到第二个cout行。意味着它输出the token is fortytens is euqal to然后它失败并终止:

nt Wordnum::read_number(string n) {
    int result_ten = 0;
    int result_unit = 0;
    int j = 0;

    //  Loops through input and turns it all to lower case.

    while (n[j]) {
        char c;

        c = n[j];
        c = (tolower(c));
        n[j] = c;
        j++;
    }

    string delimiter = "_";
    size_t pos = 0;
    string token;

    // Checks for the _ delimiter and takes the first token then checks it against the array to "convert" from string to int.
    while ((pos = n.find(delimiter)) != string::npos) {
        token = n.substr(0, pos);

        cout << "Token is: '" << token << "'" << std::endl;

        for (int i = 0; i < 20; i++) {
            if (token == tens[i]) {
                result_ten = i * 10;
                n.erase(0, pos + delimiter.length());
                cout << "tens is equals to: " << result_ten << endl;
            } else if (token == units[i]) {
                result_unit = i;
                n.erase(0, pos);
            }
        }

        result_ten = (result_ten + result_unit);

        cout << "ten is equals to: " << result_ten << endl;
    }

    return result_ten;
}

正在使用的数组:

string const Wordnum::units[] = {"zero", "one", "two", "three", "four", "five",
    "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
    "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

string const Wordnum::tens[] = {"zero", " ", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

提前致谢。如果事情不清楚,我会尝试更好地解释它。

3 个答案:

答案 0 :(得分:3)

你尝试索引tens[i],而我从[0,20]开始。但是,在十位数中没有20个指数。

答案 1 :(得分:2)

您的错误出现在 for-loop 中:

for (int i = 0; i < 20; i++) {
    //              ^^
    if (token == tens[i]) {
    //           ^^^^^^^
    // ...
}

i0转到19。而你试图索引tens[i]。但是:

string const Wordnum::tens[] = {"zero", " ", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

此行向我们显示tens只有10个元素。您在这里遇到了着名的out-of-bounds错误。

您可以使用以下方法解决此问题:

for (int i = 0; i < 20; i++) {
    if ( i < 10 && token == tens[i]) {
    //   ^^^^^^^^^^
    // ...
}

我不确定你的算法会给出预期的结果,但至少你的程序不会崩溃。

答案 2 :(得分:1)

int Wordnum :: read_number(string n){

int result_ten = 0;
int result_unit = 0;
int j = 0, f3 =0;
//  Loops through input and turns it all to lower case.
while (n[j]) {
    char c;

    c = n[j];
    c = (tolower(c));
    n[j] = c;
    j++;
}
string delimiter = "_";
size_t pos = 0, pos1 = 0;
string token;
// Checks for the _ delimiter and takes the first token then checks it against the    array to "convert" from string to int.

if((pos = n.find(delimiter)) != string::npos) {
    token = n.substr(0, pos);
}else
{
    token = n.substr(0, n.length());
    f3 = 1;
}

for (int i = 0; i < 20; i++) {
    if (token == tens[i]) {
       result_ten = i * 10;
       i=0;
       if(f3 == 1)
          break;
       token = n.substr(pos+1,(n.length()-pos-1) );
     } else if (token == units[i]) {
            result_unit = i;
            break;
     }
 }## End of loop for converting the string to int ##
 result_ten = (result_ten + result_unit);
 cout << "value is : " << result_ten << endl;
 return result_ten;

}

请检查一下,希望这会对你有所帮助