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;
}
elseif (input >= 0)
{
counter++;
grades[counter-1]=input;
}
elseif (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.
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).
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.
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.
#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;
}
elseif (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
#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;
}
elseif (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';
}