Why is my C ++ program crashing?

Hi. First of all, I apologize in advance for any mistakes in explaining. I use the Google translator...

I must make a program that complies with the following:

"Make a program in c ++ that calculates the average of n notes, shows the notes greater than 15 pts and that the program is iterative (ask if you want to run the program again) place the pseudocode in c ++ either in visual c ++ or in dev c ++"

From there I have created a code that, despite the fact that the Dev-C ++ program does not throw errors when executed, when the first qualifications are entered, it crashes. What I can do?

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
	
using namespace std;

int main() {
	
	float Notas[10];
	float promedio;
	float calificaciones;
	bool reinicio = 1;
	float recopilacion = 0;

	
	while(reinicio == 1){
			printf("\n");
			printf("Ingrese numero de notas totales: ");
				scanf("%f", &Notas[0]);
				printf("\n");
				printf("El numero total de notas es: %.2f\n\n", Notas[0]);
				
		float notas = Notas[0];
	
		for(Notas[0] > 0; Notas[0]--;) {
				printf("Introducir calificacion: ");
				scanf("%f", &calificaciones);
				promedio += calificaciones;
					for(int i = 0; i >= 0; i++) {
						Notas[i] += calificaciones;
					}
			}
		printf("\n");
		printf("El promedio de las calificaciones del alumno es: ");
		cout << promedio/notas;
		printf("\n");
		printf("\n");
		printf("Las notas superiores a 15 son: \n");
			
		for(int i = 0; i < notas; i++){
			if(Notas[i]>15){
				printf("Notas[i] \n");
			}
			}
			
			printf("\n");
			printf("\n");
		printf("¿Desea inicia nuevamente el programa? 1 para si, 0 para no\n");
			cin >> reinicio;
	}
	system ("PAUSE");
	return 0;
}
Why are you using C functions (scanf, printf) in C++ code? Use std::cin and std::cout instead. You already included the C++ stream I/O header.

If you want/need to use the C functions you should include <cstdio>/<stdio.h> also.

Recommended: don't mix C and C++ code. Either write and compile code as C or C++. Not both.

You have a 10 element array, and you use the first element for the number of entries the user wants to make. That leaves you at most 8 grades that can be entered. Any more and you cause the array to go out of bounds.

Your promedio variable is not initialized when you declare it, it contains a garbage value. Adding other values to garbage is still a garbage value.

Your biggest problem is the for loop beginning at line 27. You are starting at zero and count up with each iteration. So the loop will continue until the incremented value wraps around. Your loop ensures you go out of bounds with your array.

Very far out of bounds. You try to access elements from 0 to 214748364. CRASH!

Another problem is your for loop starting at line 23. The start statement is a comparison, the comparison statement is a decrement, and you have no increment statement. A malformed loop.
Consider using for (; Notas[0] > 0; Notas[0]--)

> You have a 10 element array, and you use the first element for the number of
> entries the user wants to make. That leaves you at most 8 grades that can be
> entered.
you have ten apples, you eat one, now you have nine apples
so you may enter at most 9 grades

> program does not throw errors when executed, when the first qualifications
> are entered, it crashes
then it does throw an error
a crash is an error

> place the pseudocode
¿and the pseudocode?
the code that you wrote is awful, but perhaps you just have issues translating it to c++


also, ¿is there also another person writing your code?
1
2
3
for(int i = 0; i >= 0; i++) {
	Notas[i] += calificaciones;
}

Infinite loop. 'i' will always be greater or equal to zero (until integer overflow engenders undefined behavior).
Last edited on
M'ok, so I mistyped 8 when I meant 9. *shrug*

The OP's code will still force the array access to go out of bounds and into undefined behavior when the for loop keeps incrementing past 9 and eventually overflows the integer.
First of all, thank you for your prompt responses.

I have to say that I'm still getting used to this with C ++, so I assume that I made several mistakes now that they are mentioned to me ...

I have fixed the loops as I thought they were appropriate, and I modified the declaration of the variables to design one that took all the values ​​that were placed (that is, no procedure gets to modify it for something else, so we will not have to subtract apples from the thing). With this also the user should be able to decide the number of notes, and therefore the number of elements in the variable.

Still, the program continues to crash, specifically when placing a second rating. What's more: if the number of Notes is 1, the program enters the loop and does not stop asking for grades ...

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
	
using namespace std;

int main() {
	
	int Notas = 0;
	int promedio = 0;
	int calificaciones = 0;
	bool reinicio = 1;
	
	while(reinicio == 1){
			cout<<"\n";
			cout<<"Ingrese numero de notas totales: ";
				cin>> Notas;
				int recopilacion[Notas];
				cout<<"\n";
				cout<<"El numero total de notas es: %.2f\n\n", Notas;
				
		for(int i = Notas; i > 0; i--) {
				cout<<"Introducir calificacion: ";
				cin>>calificaciones;
				promedio += calificaciones;
				recopilacion[i] += calificaciones;
				}
					
		cout<<"\n";
		cout<<"El promedio de las calificaciones del alumno es: ";
		cout<< promedio/Notas;
		cout<<"\n";
		cout<<"\n";
		cout<<"Las notas superiores a 15 son: \n";
			
		for(int i = 0; i < Notas; i++){
			if(recopilacion[i]>15){
				cout<<"recopilacion[i] \n";
			}
			}
			
			cout<<"\n";
			cout<<"\n";;
		cout<<"¿Desea inicia nuevamente el programa? 1 para si, 0 para no\n";
			cin >> reinicio;
	}
	system ("PAUSE");
	return 0;
}


Edit: Trying to make the code from scratch I have come up with this one again which seems to be going pretty well. It performs the average, gives freedom of the number of notes, and shows the values ​​greater than 15, but the problem is precisely in this last step, where for some reason some values ​​are shown with numbers like 26688 or similar.
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
33
34
35
36
37
38
39
40
41
#include <iostream>
	
using namespace std;

int main() {
	
	int Notas = 0;
	int calificaciones = 0;
	int total = 0;
	bool reinicio = 1;

while(reinicio==1){
	cout<<"Introducir numero de Notas\n";
	cin>>Notas;
	int promedio[Notas];
	
		for(int i = Notas; i > 0; i--) {
				cout<<"Introducir calificacion: ";
				cin>>calificaciones;
				total += calificaciones;
				promedio[i] = calificaciones;
				}
				
		cout<<"\n";
		cout<<"El promedio de las calificaciones del alumno es: \n";
		cout<< total/Notas<<"\n";
		cout<<"Las notas superiores a 15 son: \n";
			
		for(int i = 0; i < Notas; i++){
			if(promedio[i]>15){
				cout<<promedio[i]<<"\n";
					}
				}
		cout<<"\n";;
		cout<<"¿Desea inicia nuevamente el programa? 1 para si, 0 para no\n";
		cin >> reinicio;
			}
	system ("PAUSE");
	return 0;
	
}
Last edited on
Line 13: std::cout << '\n';

Line 16: variable length arrays set at run time are not allowed in C++, you either use a std::vector or the array must have a constant value at compile time. You had that in your original code. Make the array larger than you will use.

Line 18: you are trying to use a C++ stream as if it is printf(). That won't work.

Arrays are zero-based, your loop starting at line 20 causes the array to go out of bounds.

Correcting those mistakes, and making a couple of other minor modifications such as moving around when variables are created and using uniform initialization.

Not understanding the Spanish(?) prompts, the program now can run without crashing. But I don't know if it executes correctly. You need to try it for correct operation.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

int main()
{
   // used to create a larger than needed array
   constexpr int MAX_NOTES { 1000 };

   // create the array and initialize all the elements to zero
   int recopilacion[MAX_NOTES] { { } };

   bool reinicio { true }; // C++ boolean values are true and false

   while (reinicio)
   {
      std::cout << "\nIngrese numero de notas totales: ";
      int Notas { };
      std::cin >> Notas;
      std::cout << '\n';

      std::cout << "El numero total de notas es: " << Notas << "\n\n";

      int promedio { };

      for (int i { Notas - 1 }; i >= 0; i--)
      {
         std::cout << "Introducir calificacion: ";
         int calificaciones { };
         std::cin >> calificaciones;

         promedio += calificaciones;
         recopilacion[i] += calificaciones;
      }

      std::cout << "\nEl promedio de las calificaciones del alumno es: ";
      std::cout << promedio / Notas;

      std::cout << "\n\nLas notas superiores a 15 son: \n";

      for (int i { }; i < Notas; i++)
      {
         if (recopilacion[i] > 15)
         {
            std::cout << "recopilacion[" << i << "]\n";
         }
      }

      std::cout << "\n\n¿Desea inicia nuevamente el programa? 1 para si, 0 para no\n";
      std::cin >> reinicio;
   }
}


Ingrese numero de notas totales: 5

El numero total de notas es: 5

Introducir calificacion: 5
Introducir calificacion: 10
Introducir calificacion: 15
Introducir calificacion: 20
Introducir calificacion: 25

El promedio de las calificaciones del alumno es: 15

Las notas superiores a 15 son:
recopilacion[0]
recopilacion[1]


┐Desea inicia nuevamente el programa? 1 para si, 0 para no
0


A quick lesson on std::vector:
https://www.learncpp.com/cpp-tutorial/6-16-an-introduction-to-stdvector/
I have copied and pasted the code you have given me and ... Dev-C ++ throws me several errors when trying to execute ...

I have translated (with google translator, cough, cough) the texts and variables of the program so that it is easier for them to understand it, although for some reason Dev-C ++ continues to reproduce everything in SPANISH: I start to believe that my program is badly installed or something ...

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
33
34
35
36
37
38
39
40
41
#include <iostream>
	
using namespace std;

int main() {
	
	int Notes = 0;
	int ratings = 0;
	int total = 0;
	bool reboot = 1;

while(reboot==1){
	cout<<"Enter number of Notes\n";
	cin>>Notes;
	int average[Notes];
	
		for(int i = Notes; i > 0; i--) {
				std::cout<<"Enter grade: ";
				std::cin>>ratings;
				total += ratings;
				average[i] = ratings;
				}
				
		std::cout<<"\n";
		std::cout<<"The average of the student's grades is: \n";
		std::cout<< total/Notes<<"\n";
		std::cout<<"Notes above 15 are: \n";
			
		for(int i = 0; i < Notes; i++){
			if(average[i]>15){
				std::cout<<average[i]<<"\n";
					}
				}
		std::cout<<"\n";;
		std::cout<<"Do you want to start the program again? 1 for yes, 0 for no\n";
		std::cin >> reboot;
			}
	system ("PAUSE");
	return 0;
	
}
> Dev-C ++
Which version?
The 'Bloodshed' version is massively out of date (by about 15 to 20 years).
The 'Orwell' version is better (only 5 years).
One major problem you'll face is not being able to compile code using modern C++11/14/17/20 features.


> int average[Notes];
This is illegal in C++. Some compilers support it, but it's not a good idea.
Ideally, use something like vector<int> averages(Notes);

> for(int i = Notes; i > 0; i--)
You need to be careful here, your first array access will be off the end of the array when you effectively do average[Notes] = ratings;

Do you have a good reason for iterating backwards?
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
33
34
35
36
#include <iostream>

using namespace std;

int main()
{
	const size_t maxNotes {20};

	int notes[maxNotes] {};
	size_t noNotes {};
	int total {};
	bool reboot {true};

	while (reboot == true) {
		cout << "Enter number of Grades (max " << maxNotes << "): ";
		cin >> noNotes;

		if (noNotes > maxNotes)
			noNotes = maxNotes;

		for (size_t i = 0; i < noNotes; total += notes[i++]) {
			std::cout << "Enter grade: ";
			std::cin >> notes[i];
		}

		std::cout << "\nThe average of the student's grades is: " << (total + 0.0) / noNotes;
		std::cout << "\nGrades above 15 are:\n";

		for (size_t i = 0; i < noNotes; ++i)
			if (notes[i] > 15)
				std::cout << notes[i] << "\n";

		std::cout << "\nDo you want to start the program again? 1 for yes, 0 for no: ";
		std::cin >> reboot;
	}
}



Enter number of Grades (max 20): 5
Enter grade: 23
Enter grade: 10
Enter grade: 56
Enter grade: 12
Enter grade: 15

The average of the student's grades is: 23.2
Grades above 15 are:
23
56

Do you want to start the program again? 1 for yes, 0 for no: 1
Enter number of Grades (max 20): 2
Enter grade: 10
Enter grade: 20

The average of the student's grades is: 73
Grades above 15 are:
20

Do you want to start the program again? 1 for yes, 0 for no: 0

Use the Orwell version of Dev-C ++

Seeplus, I have copied your code into Dev-C ++ and have been able to execute it (even though it has thrown several errors). At the moment of showing the notes higher than 15 it continues throwing strange values.

Honestly, I started to iterate backwards to try options, but I just realized that you can iterate forward, and precisely after doing that the program has not given me any kind of problems, so I suppose that with that everything would be done. Still I leave the code in case you want to check it and give some other advice. In any case, thank you very much for your help.

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
33
34
35
36
37
38
39
40
41
#include <iostream>
	
using namespace std;

int main() {
	
	int Notes = 0;
	int ratings = 0;
	int total = 0;
	bool reboot = 1;

while(reboot==1){
	cout<<"Enter number of Notes\n";
	cin>>Notes;
	int average[Notes];
	
		for(int i = 0; i < Notes; i++) {
				std::cout<<"Enter grade: ";
				std::cin>>ratings;
				total += ratings;
				average[i] = ratings;
				}
				
		std::cout<<"\n";
		std::cout<<"The average of the student's grades is: \n";
		std::cout<< total/Notes<<"\n";
		std::cout<<"Notes above 15 are: \n";
			
		for(int i = 0; i < Notes; i++){
			if(average[i]>15){
				std::cout<<average[i]<<"\n";
					}
				}
		std::cout<<"\n";;
		std::cout<<"Do you want to start the program again? 1 for yes, 0 for no\n";
		std::cin >> reboot;
			}
	system ("PAUSE");
	return 0;
	
}
The sole issue Dev C++ had with my code earlier is how the array was declared. Change that and the code compiles with Dev C++:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

int main()
{
   // used to create a larger than needed array
   constexpr int MAX_NOTES { 1000 };

   // create the array and initialize all the elements to zero
   int recopilacion[MAX_NOTES] { };

   bool reinicio { true }; // C++ boolean values are true and false

   while (reinicio)
   {
      std::cout << "\nIngrese numero de notas totales: ";
      int Notas { };
      std::cin >> Notas;
      std::cout << '\n';

      std::cout << "El numero total de notas es: " << Notas << "\n\n";

      int promedio { };

      for (int i { Notas - 1 }; i >= 0; i--)
      {
         std::cout << "Introducir calificacion: ";
         int calificaciones { };
         std::cin >> calificaciones;

         promedio += calificaciones;
         recopilacion[i] += calificaciones;
      }

      std::cout << "\nEl promedio de las calificaciones del alumno es: ";
      std::cout << promedio / Notas;

      std::cout << "\n\nLas notas superiores a 15 son: \n";

      for (int i { }; i < Notas; i++)
      {
         if (recopilacion[i] > 15)
         {
            std::cout << "recopilacion[" << i << "]\n";
         }
      }

      std::cout << "\n\n¿Desea inicia nuevamente el programa? 1 para si, 0 para no\n";
      std::cin >> reinicio;
   }
}

Dev C++ is old and outdated. It won't work with C++14 or later code. I seriously suggest you get a newer compiler/IDE. Code::Blocks is one possibility.
http://www.codeblocks.org/

Another if you are on Windows is Visual Studio, the Community edition.
https://visualstudio.microsoft.com/downloads/
Topic archived. No new replies allowed.