Home > Software engineering >  String subscript out of range error for code that traverses string c
String subscript out of range error for code that traverses string c

Time:03-06

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  ;
    }
}

CodePudding user response:

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.

  • Related