Answer the question
In order to leave comments, you need to log in
Fix error - C++ program?
There is a program, it has an error, help fix it and understand why it pops up. I found on the Internet that this error occurs when you go beyond the edges of the array - but it seems that I don’t go out. I suspect that I messed up something with references and pointers (I don’t really understand anything about them) - and this is, as it were, a consequence. The program works, gives the result, after the end it already gives this error.
Run-Time Check Failure #2 - Stack around the variable 'match' was corrupted.
Run-Time Check Failure #2 - Stack around the variable 'game' was corrupted. (One of the two pops up in turn)
Program code:
#include "pch.h"
#include <iostream>
using namespace std;
void dynamic_memory_allocation(int m, int* g)
{
g = new int[m];
}
void get_data(int m, int* g)
{
for (int i = 0; i < m; i++)
{
cout << "Введите исход матча №" << (i + 1) << " (кол-во очков: 0, 1, 3)." << endl;
cin >> g[i];
if (g[i] != 0 && g[i] != 1 && g[i] != 3) {
cout << "Введено некоректное значение, повторите ввод." << endl;
i--;
}
}
}
void sum_point(int m, int* g, int& s)
{
for (int i = 0; i < m; i++)
s += g[i];
}
void win_fall(int m, int* g, int& cw, int& cf)
{
for (int i = 0; i < m; i++)
!g[i] ? cf++ : cw++;
}
int main()
{
setlocale(0, "");
int match = 0, game, sum = 0, count_win = 0, count_fall = 0;
cout << "Введите количество матчей" << endl;
cin >> match;
if (match < 0) {
cout << "Некоректные входные данные";
return 0;
}
dynamic_memory_allocation(match, &game);
get_data(match, &game);
sum_point(match, &game, sum);
cout << "Общее количество очков = " << sum << endl;
win_fall(match, &game, count_win, count_fall);
cout << "Команда выиграла или сыграла в ничью " << count_win << " раз(а)" << endl;
cout << "Команда проиграла " << count_fall << " раз(а)" << endl;
return 0;
}
Answer the question
In order to leave comments, you need to log in
I rewrote the function, it works like this. Thanks to those who wrote in the comments, they helped.
#include "pch.h"
#include <iostream>
using namespace std;
int* dynamic_memory_allocation(int m)
{
int *g;
g = new int[m];
return g;
}
void get_data(int m, int* g)
{
for (int i = 0; i < m; i++)
{
cout << "Введите исход матча №" << (i + 1) << " (кол-во очков: 0, 1, 3)." << endl;
cin >> g[i];
if (g[i] != 0 && g[i] != 1 && g[i] != 3) {
cout << "Введено некорректное значение, повторите ввод." << endl;
i--;
}
}
}
void sum_point(int m, int* g, int& s)
{
for (int i = 0; i < m; i++)
s += g[i];
}
void win_fall(int m, int* g, int& cw, int& cf)
{
for (int i = 0; i < m; i++)
!g[i] ? cf++ : cw++;
}
int main()
{
setlocale(0, "");
int match = 0, sum = 0, count_win = 0, count_fall = 0;
cout << "Введите количество матчей" << endl;
cin >> match;
if (match < 0) {
cout << "Некорректные входные данные";
return 0;
}
int *g;
g = dynamic_memory_allocation(match);
get_data(match, g);
sum_point(match, g, sum);
cout << "Общее количество очков = " << sum << endl;
win_fall(match, g, count_win, count_fall);
cout << "Команда выиграла или сыграла в ничью " << count_win << " раз(а)" << endl;
cout << "Команда проиграла " << count_fall << " раз(а)" << endl;
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question