Program crashes after 11th element is added to array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    int input;
    int counter=0;
    int grades[counter];

    while (input!=-99)
    {
        cout << "Enter your grade, when you are done enter -99: ";
        cin >> input;

        if (input < 0 && input!=-99)
        {
            cout << "You have entered a negative number, please enter a positive number between 0 and 100." << endl;
        }

        else if (input >= 0)
        {
            counter++;
            grades[counter-1]=input;
        }

        else if (input=-99)
        {
            break;
        }

    }



This code works until the 11th element is added to the array, where it says "main.exe has stopped working" and the program crashes.
Last edited on
1
2
int counter=0;
int grades[counter];

You are trying to make an array of size 0. (counter = 0 --> int grades[0];)

It is not allowed in C++ to make an array of size 0, and it is not allowed in C++ to make an array with a non-constant value (or more specifically, you must use a compiler-time constant as an array size).

If you wish to have a dynamically-increasing array, I suggest looking at std::vectors
https://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm
Hello Shezshade,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

In addition to what Ganado has said you have no idea what the size of the grades array is. By your description it sounds like you are trying to store something in the array past the boundary of the array.

Without the rest of the code it is impossible to know what you did or why it crashes. And with out the whole program it is hard for me to test.

In this code:
1
2
3
4
5
6
else if (input >= 0)
{
counter++;
grades[counter];
grades[counter]=input;
}

Line 4 does nothing and I am wondering what you have in mind for putting it there?

Line 5 is a better use of "counter" as a subscript.

Personally I would put the last if/else if as the first if statement. If you have entered -99 there is no point wasting time checking everything else.

Hope that helps,

Andy
Thank you, I've highlighted my code. I was trying to match the array size to the number of grades the user entered. With the code that is posted, the user is able to enter 11 grades. Once the 11th grade is entered, the program crashes.
closed account (E0p9LyTq)
An array has to have its size known at compile time, your code is not correct C++ code.

To contain a variable number of numbers you need to use one of C++'s containers, a vector would work.

I added a bit of code to display the entered numbers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <vector>

int main()
{
   std::vector<int> grades;

   int input = 0;

   while (input > -99)
   {
      std::cout << "Enter your grade, when you are done enter -99: ";
      std::cin >> input;

      if (input < 0 && input != -99)
      {
         std::cout << "You have entered a negative number, please enter a positive number between 0 and 100.\n";
         continue;
      }
      else if (input >= 0)
      {
         grades.push_back(input);
      }
   }

   std::cout << "\nThe grades are:\n";
   for (size_t loop = 0; loop < grades.size(); loop++)
   {
      std::cout << grades[loop] << ' ';
   }
   std::cout << '\n';
}

Enter your grade, when you are done enter -99: 5
Enter your grade, when you are done enter -99: -15
You have entered a negative number, please enter a positive number between 0 and 100.
Enter your grade, when you are done enter -99: 25
Enter your grade, when you are done enter -99: 15
Enter your grade, when you are done enter -99: 8
Enter your grade, when you are done enter -99: -99

The grades are:
5 25 15 8
Last edited on
closed account (E0p9LyTq)
If you absolutely must use an array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

int main()
{
   int input = 0;;
   int counter = 0;
   int grades[1000]; // no one should ever enter that many

   while (input > -99)
   {
      std::cout << "Enter your grade, when you are done enter -99: ";
      std::cin >> input;

      if (input < 0 && input != -99)
      {
         std::cout << "You have entered a negative number, please enter a positive number between 0 and 100.\n";
         continue;
      }
      else if (input >= 0)
      {
         grades[counter] = input;
         counter++;
      }
   }

   std::cout << "\nThe grades are:\n";
   for (int loop = 0; loop < counter; loop++)
   {
      std::cout << grades[loop] << ' ';
   }
   std::cout << '\n';
}

(Output is the same)
Last edited on
Topic archived. No new replies allowed.