遍历字符串 c++ 的代码出现字符串下标超出范围错误

发布于 2025-01-11 19:46:31 字数 743 浏览 1 评论 0原文

我正在尝试计算一串空格分隔的整数中正数、负数和零的数量。输入字符串中的整数个数由用户指定。

代码编译得很好,但是每次我尝试运行它时,它都会崩溃并显示错误“调试断言失败。...表达式:字符串下标超出范围”。

#include <iostream>
#include <string>
#include <iomanip>

int main() {
    int i = 0;
    int n, x;
    int positives = 0;
    int negatives = 0;
    int zeros = 0;
    std::string ip;
    char space = ' ';
    std::cin >> n;
    std::cin >> ip;
    while (i < n) {
        if (ip[i] != space) {
            x = (ip[i]);
            if (x > 0) {
                positives++;
            }
            else if (x < 0) {
                negatives++;
            }
            else if (x == 0) {
                zeros++;
            }
        }
        i++;
    }
}

I am trying to count the count the number of positive numbers, negative numbers and zeros in a string of space separated integers. The number of integers in the input string is specified by the user.

The code compiles fine, however every time I try to run it, it crashes with the error "Debug Assertion Failed. ... Expression: String subscript out of range".

#include <iostream>
#include <string>
#include <iomanip>

int main() {
    int i = 0;
    int n, x;
    int positives = 0;
    int negatives = 0;
    int zeros = 0;
    std::string ip;
    char space = ' ';
    std::cin >> n;
    std::cin >> ip;
    while (i < n) {
        if (ip[i] != space) {
            x = (ip[i]);
            if (x > 0) {
                positives++;
            }
            else if (x < 0) {
                negatives++;
            }
            else if (x == 0) {
                zeros++;
            }
        }
        i++;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2025-01-18 19:46:31

首先 std::cin>> some_string_var 将在它找到的第一个空白字符处停止,因此使用它来搜索分隔单词的空格没有什么意义。

您最好只读取整数并将它们直接与零进行比较。以下是您如何在代码中使用 MNC(最少必要的更改):

#include <iostream>

int main() {
    int i = 0;
    int n;
    int positives = 0;
    int negatives = 0;
    int zeros = 0;
    int value;
    std::cin >> n;
    while (i < n) {
        std::cin >> value;

        if (value > 0)
            positives++;
        else if (value < 0)
            negatives++;
        else
            zeros++;

        i++;
    }
    std::cout << "p=" << positives << ", n=" << negatives << ", z=" << zeros << '\n';
}

下面是一个示例运行,请记住初始的 4 是一个计数而不是其中一个值:

pax:~> ./prog
4
0 1 2 -99
p=2, n=1, z=1

如果您正在查找对于稍微健壮的东西,您可以使用这样的东西:

#include <iostream>

int main() {
    int quant, pos = 0, neg = 0, zer = 0, value;
    std::cout << "How many input values? ";
    if (! (std::cin >> quant )) {
        std::cout << "\n*** Invalid input.\n";
        return 1;
    }
    if (quant < 0) {
        std::cout << "\n*** Negative quantity input.\n";
        return 1;
    }

    for (int count = 1; count <= quant; ++count) {
        std::cout << "Enter value #" << count << ": ";
        if (! (std::cin >> value )) {
            std::cout << "\n*** Invalid input.\n";
            return 1;
        }

        if (value > 0.0)
            pos++;
        else if (value < 0.0)
            neg++;
        else
            zer++;
    }

    std::cout << "Positive value count: " << pos << '\n';
    std::cout << "Negative value count: " << neg << '\n';
    std::cout << "Zero value count:     " << zer << '\n';
}

与用户通信时更加用户友好(无论是在请求的内容还是生成的结果方面)。它在检测不良输入方面也更加稳健。

For a start std::cin >> some_string_var is going to stop at the first white space character it finds, so there's little point in using that to search for spaces separating words.

You would be better off just reading in integers and just comparing them directly with zero. Here's how you could do with with the MNC (minimum necessary change) on your code:

#include <iostream>

int main() {
    int i = 0;
    int n;
    int positives = 0;
    int negatives = 0;
    int zeros = 0;
    int value;
    std::cin >> n;
    while (i < n) {
        std::cin >> value;

        if (value > 0)
            positives++;
        else if (value < 0)
            negatives++;
        else
            zeros++;

        i++;
    }
    std::cout << "p=" << positives << ", n=" << negatives << ", z=" << zeros << '\n';
}

A sample run follows, keeping in mind the initial 4 is a count rather than one of the values:

pax:~> ./prog
4
0 1 2 -99
p=2, n=1, z=1

If you were looking for something a little robust, you could use something like this:

#include <iostream>

int main() {
    int quant, pos = 0, neg = 0, zer = 0, value;
    std::cout << "How many input values? ";
    if (! (std::cin >> quant )) {
        std::cout << "\n*** Invalid input.\n";
        return 1;
    }
    if (quant < 0) {
        std::cout << "\n*** Negative quantity input.\n";
        return 1;
    }

    for (int count = 1; count <= quant; ++count) {
        std::cout << "Enter value #" << count << ": ";
        if (! (std::cin >> value )) {
            std::cout << "\n*** Invalid input.\n";
            return 1;
        }

        if (value > 0.0)
            pos++;
        else if (value < 0.0)
            neg++;
        else
            zer++;
    }

    std::cout << "Positive value count: " << pos << '\n';
    std::cout << "Negative value count: " << neg << '\n';
    std::cout << "Zero value count:     " << zer << '\n';
}

It's a little more user friendly on communicating with the user (both in terms of what is requested, and the results generated). It's also a little more robust in detecting bad input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文