В некоторых местах возникают ошибки по месту вызова функции scanf() - C (СИ)
Формулировка задачи:
В некоторых местах возникают ошибки по месту вызова функции scanf(), эти места я указал в коде (пример скрин error1), а ещё на выходе, если scanf() был успешно пройден, видимо, что-то с памятью не так (скрин error2)
#define _CRT_SECURE_NO_WARNINGS #include <conio.h> #include <stdlib.h> #include <stdio.h> #include <time.h> struct settings { int number; char *parity; char *colour; int dozen; int column; int half; }; struct groups { int split[60][2]; int triad[14][3]; int tetrad[22][4]; int sixline[11][6]; }; struct input { unsigned int n; //число ставок int *ss; //сумма ставки int *ks; //критерий ставки int *psc; //параметр ставки число char *pss; //параметр ставки слово int **pscs; //параметр ставки группа чисел }; void initialization(struct settings *a, struct groups *b); void input0(struct input *in); void input1(struct input *in); void input2(struct input *in); void random(int *Num); int main() { int Num; struct settings a[37]; struct groups b; struct input in; initialization(a, &b); input0(&in); input1(&in); input2(&in); random(&Num); _getch(); } void initialization(struct settings *a, struct groups *b) { int i; int c1=0, c2=0, c3=0, c4=0; int d1, d2, d3, d4; //int x = 15; //int num; //char *par; for (i = 0; i < 37; i++) { //число a[i].number = i; //чётность if (i == 0) a[i].parity = "NONE\0"; if ((i != 0) && (i % 2 == 0)) a[i].parity = "Even"; if ((i != 0) && (i % 2 != 0)) a[i].parity = "Odd"; //цвет if (i == 0) a[i].colour = "Green"; if ((i > 0) && (i < 11) || (i>18) && (i<29)) { if (i % 2 == 0) a[i].colour = "Black"; else a[i].colour = "Red"; } if ((i > 10) && (i < 19) || (i>28) && (i<=36)) { if (i % 2 == 0) a[i].colour = "Red"; else a[i].colour = "Black"; } //дюжины if (i == 0) a[i].dozen = 0; if ((i != 0) && (i <= 12)) a[i].dozen = 1; if ((i > 12) && (i < 25)) a[i].dozen = 2; if (i > 24) a[i].dozen = 3; //колонны if (i == 0) a[i].column = 0; if (i % 3 == 1) a[i].column = 1; if (i % 3 == 2) a[i].column = 2; if ((i!=0) && (i % 3 == 0)) a[i].column = 0; //половина if (i == 0) a[i].half = 0; if ((i != 0) && (i < 19)) a[i].half = 1; if (i > 18) a[i].half = 2; //сплиты if ((i % 3 == 0) && (i!=36)) { //1 (*b).split[c1][0] = i;//0 //3 (*b).split[c1][1] = i+3;//3 //6 c1++; //2 (*b).split[c1][0] = i+3;//3 //6 (*b).split[c1][1] = i+2;//2 //5 c1++; //3 (*b).split[c1][0] = i + 2; //2 //5 (*b).split[c1][1] = i - 1; if ((*b).split[c1][1] < 0) (*b).split[c1][1] = 0;//0 //2 c1++; //4 (*b).split[c1][0] = i+2;//2 //5 (*b).split[c1][1] = i+1;//1 //4 c1++; //5 (*b).split[c1][0] = i+1;//1 //4 (*b).split[c1][1] = i-2; if ((*b).split[c1][1] < 0) (*b).split[c1][1] = 0;//0 //1 c1++; } //триады if (i % 3 == 0) { if (i == 0) { (*b).triad[c2][0] = 0; (*b).triad[c2][1] = 1; (*b).triad[c2][2] = 2; c2++; (*b).triad[c2][0] = 0; (*b).triad[c2][1] = 2; (*b).triad[c2][2] = 3; c2++; } else { (*b).triad[c2][0] = i; (*b).triad[c2][1] = i - 1; (*b).triad[c2][2] = i - 2; c2++; } } //тетрады if (i == 0) { (*b).tetrad[c3][0] = 0; (*b).tetrad[c3][1] = 1; (*b).tetrad[c3][2] = 2; (*b).tetrad[c3][3] = 3; c3++; } else if((i%3==1) || (i%3==2)) { (*b).tetrad[c3][0] = i; (*b).tetrad[c3][1] = i+1; (*b).tetrad[c3][2] = i+3; (*b).tetrad[c3][0] = i+4; c3++; } //sixline if (i % 3 == 1) { (*b).sixline[c4][0] = i; (*b).sixline[c4][1] = i+1; (*b).sixline[c4][2] = i+2; (*b).sixline[c4][3] = i+3; (*b).sixline[c4][4] = i+4; (*b).sixline[c4][5] = i+5; c4++; } } //i = x; //par = a[i].parity; //printf("par=%s", par); //num = a[i].number; //printf("num=%d", num); } void input0(struct input *in) { printf("\n Input number rates\n"); printf("n="); scanf("%d", &in->n); if (in->n == 0) exit(1); } void input1(struct input *in) { int i; in->ss = (int*)malloc(in->n * sizeof(int)); in->ks = (int*)malloc(in->n * sizeof(int)); in->psc = (int*)malloc(in->n * sizeof(int)); in->pss = (char*)malloc(in->n * sizeof(char)); in->pscs = (int**)malloc(in->n * sizeof(int*)); for (i = 0; i<in->n; i++) in->pscs[i] = (int*)malloc(6 * sizeof(int)); } void input2(struct input *in) { int i, k; for (i = 0; i < in->n; i++) { printf("\n Input summu rates\n"); printf("ss[%d]=", i); scanf("%d", &in->ss[i]); printf("\n Choose criterion rates from 1 to 10"); printf("\n 1. Number"); printf("\n 2. Parity"); printf("\n 3. Colour"); printf("\n 4. Dozen"); printf("\n 5. Column"); printf("\n 6. Half"); printf("\n 7. Split"); printf("\n 8. Triad"); printf("\n 9. Tetrad"); printf("\n 10. Sixline"); up:printf("\n ks[%d]=", i); scanf("%d", &in->ks[i]); switch (in->ks[i]) { case 1: { printf("\n Input number from 0 to 36"); printf("\n pcs[%d]=", i); scanf("%d", &in->psc[i]); } break; case 2: { printf("\n Input Even or Odd"); printf("\n pss[%d]=", i); scanf("%s", &in->pss); } break; case 3: { printf("\n Input Red or Black"); printf("\n pss[%d]=", i); scanf("%s", &in->pss); } break; case 4: { printf("\n Input number dozen from 1 to 3"); printf("\n pcs[%d]=", i); scanf("%d", &in->psc[i]); } break; case 5: { printf("\n Input number column from 1 to 3"); printf("\n pcs[%d]=", i); scanf("%d", &in->psc[i]); } break; case 6: { printf("\n Input number half from 1 (1-18) to 2 (19-36)"); printf("\n pcs[%d]=", i); scanf("%d", &in->psc[i]); } break; case 7: { printf("\n Input two nearby numbers"); for (k = 0; k < 6; k++) { if (k<2) { printf("\n pscs[%d][%d]=", i, k); scanf("%d", &in->pscs[i][k]);//вот здесь } } } break; case 8: { printf("\n Input three nearby numbers"); for (k = 0; k < 6; k++) { if (k < 3) { printf("\n pscs[%d][%d]=", i, k); scanf("%d", &in->pscs[i][k]); //вот здесь } } } break; case 9: { printf("\n Input four nearby numbers"); for (k = 0; k < 6; k++) { if (k < 4) { printf("\n pscs[%d][%d]=", i, k); scanf("%d", &in->pscs[i][k]); //вот здесь } } } break; case 10: { printf("\n Input six nearby numbers"); for (k = 0; k < 6; k++) { printf("\n pscs[%d][%d]=", i, k); scanf("%d", &in->pscs[i][k]); //вот здесь } } break; default: { printf("\n You input other number"); printf("\n Can you repeat, please"); goto up; } } } } void random(int *Num) { srand(time(NULL)); *Num = rand() % 36; }
Решение задачи: «В некоторых местах возникают ошибки по месту вызова функции scanf()»
textual
Листинг программы
#define _CRT_SECURE_NO_WARNINGS #include <conio.h> #include <stdlib.h> #include <stdio.h> #include <time.h> struct settings { int number; char *parity; char *colour; int dozen; int column; int half; }; struct groups { int split[60][2]; int triad[14][3]; int tetrad[22][4]; int sixline[11][6]; }; struct input { unsigned int n; //число ставок int *ss; //сумма ставки int *ks; //критерий ставки int *psc; //параметр ставки число char *pss; //параметр ставки слово int **pscs2; int **pscs3; int **pscs4; int **pscs6; }; void initialization(struct settings *a, struct groups *b); void input0(struct input *in); void input1(struct input *in); void input2(struct input *in); //void random(int *Num); int main() { int Num; struct settings a[37]; struct groups b; struct input in; initialization(a, &b); input0(&in); input1(&in); input2(&in); //random(&Num); _getch(); } void initialization(struct settings *a, struct groups *b) { int i; int c1=0, c2=0, c3=0, c4=0; int d1, d2, d3, d4; //int x = 15; //int num; //char *par; for (i = 0; i < 37; i++) { //число a[i].number = i; //чётность if (i == 0) a[i].parity = "NONE\0"; if ((i != 0) && (i % 2 == 0)) a[i].parity = "Even"; if ((i != 0) && (i % 2 != 0)) a[i].parity = "Odd"; //цвет if (i == 0) a[i].colour = "Green"; if ((i > 0) && (i < 11) || (i>18) && (i<29)) { if (i % 2 == 0) a[i].colour = "Black"; else a[i].colour = "Red"; } if ((i > 10) && (i < 19) || (i>28) && (i<=36)) { if (i % 2 == 0) a[i].colour = "Red"; else a[i].colour = "Black"; } //дюжины if (i == 0) a[i].dozen = 0; if ((i != 0) && (i <= 12)) a[i].dozen = 1; if ((i > 12) && (i < 25)) a[i].dozen = 2; if (i > 24) a[i].dozen = 3; //колонны if (i == 0) a[i].column = 0; if (i % 3 == 1) a[i].column = 1; if (i % 3 == 2) a[i].column = 2; if ((i!=0) && (i % 3 == 0)) a[i].column = 0; //половина if (i == 0) a[i].half = 0; if ((i != 0) && (i < 19)) a[i].half = 1; if (i > 18) a[i].half = 2; //сплиты if ((i % 3 == 0) && (i!=36)) { //1 (*b).split[c1][0] = i;//0 //3 (*b).split[c1][1] = i+3;//3 //6 c1++; //2 (*b).split[c1][0] = i+3;//3 //6 (*b).split[c1][1] = i+2;//2 //5 c1++; //3 (*b).split[c1][0] = i + 2; //2 //5 (*b).split[c1][1] = i - 1; if ((*b).split[c1][1] < 0) (*b).split[c1][1] = 0;//0 //2 c1++; //4 (*b).split[c1][0] = i+2;//2 //5 (*b).split[c1][1] = i+1;//1 //4 c1++; //5 (*b).split[c1][0] = i+1;//1 //4 (*b).split[c1][1] = i-2; if ((*b).split[c1][1] < 0) (*b).split[c1][1] = 0;//0 //1 c1++; } //триады if (i % 3 == 0) { if (i == 0) { (*b).triad[c2][0] = 0; (*b).triad[c2][1] = 1; (*b).triad[c2][2] = 2; c2++; (*b).triad[c2][0] = 0; (*b).triad[c2][1] = 2; (*b).triad[c2][2] = 3; c2++; } else { (*b).triad[c2][0] = i; (*b).triad[c2][1] = i - 1; (*b).triad[c2][2] = i - 2; c2++; } } //тетрады if (i == 0) { (*b).tetrad[c3][0] = 0; (*b).tetrad[c3][1] = 1; (*b).tetrad[c3][2] = 2; (*b).tetrad[c3][3] = 3; c3++; } else if((i%3==1) || (i%3==2)) { (*b).tetrad[c3][0] = i; (*b).tetrad[c3][1] = i+1; (*b).tetrad[c3][2] = i+3; (*b).tetrad[c3][0] = i+4; c3++; } //sixline if ((i % 3 == 1) && (i!=34)) { (*b).sixline[c4][0] = i; (*b).sixline[c4][1] = i+1; (*b).sixline[c4][2] = i+2; (*b).sixline[c4][3] = i+3; (*b).sixline[c4][4] = i+4; (*b).sixline[c4][5] = i+5; c4++; } } //i = x; //par = a[i].parity; //printf("par=%s", par); //num = a[i].number; //printf("num=%d", num); } void input0(struct input *in) { printf("\n Input number rates\n"); printf("n="); scanf("%u", &in->n); if (in->n == 0) exit(1); } void input1(struct input *in) { int i; in->ss = (int*)malloc(in->n * sizeof(int)); in->ks = (int*)malloc(in->n * sizeof(int)); in->psc = (int*)malloc(in->n * sizeof(int)); in->pss = (char*)malloc(in->n * sizeof(char)); in->pscs2 = (int**)malloc(in->n * sizeof(int*)); for (i = 0; i<in->n; i++) in->pscs2[i] = (int*)malloc(2 * sizeof(int)); in->pscs3 = (int**)malloc(in->n * sizeof(int*)); for (i = 0; i<in->n; i++) in->pscs3[i] = (int*)malloc(3 * sizeof(int)); in->pscs4 = (int**)malloc(in->n * sizeof(int*)); for (i = 0; i<in->n; i++) in->pscs4[i] = (int*)malloc(4 * sizeof(int)); in->pscs6 = (int**)malloc(in->n * sizeof(int*)); for (i = 0; i<in->n; i++) in->pscs6[i] = (int*)malloc(6 * sizeof(int)); } void input2(struct input *in) { int i, k; for (i = 0; i < in->n; i++) { printf("\n Input summu rates\n"); printf("ss[%d]=", i); scanf("%d", &in->ss[i]); printf("\n Choose criterion rates from 1 to 10"); printf("\n 1. Number"); printf("\n 2. Parity"); printf("\n 3. Colour"); printf("\n 4. Dozen"); printf("\n 5. Column"); printf("\n 6. Half"); printf("\n 7. Split"); printf("\n 8. Triad"); printf("\n 9. Tetrad"); printf("\n 10. Sixline"); up:printf("\n ks[%d]=", i); scanf("%d", &in->ks[i]); switch (in->ks[i]) { case 1: { printf("\n Input number from 0 to 36"); printf("\n pcs[%d]=", i); scanf("%d", &in->psc[i]); } break; case 2: { printf("\n Input Even or Odd"); printf("\n pss[%d]=", i); scanf("%s", &in->pss); } break; case 3: { printf("\n Input Red or Black"); printf("\n pss[%d]=", i); scanf("%s", &in->pss); } break; case 4: { printf("\n Input number dozen from 1 to 3"); printf("\n pcs[%d]=", i); scanf("%d", &in->psc[i]); } break; case 5: { printf("\n Input number column from 1 to 3"); printf("\n pcs[%d]=", i); scanf("%d", &in->psc[i]); } break; case 6: { printf("\n Input number half from 1 (1-18) to 2 (19-36)"); printf("\n pcs[%d]=", i); scanf("%d", &in->psc[i]); } break; case 7: { printf("\n Input two nearby numbers"); for (k = 0; k < 2; k++) { printf("\n pscs2[%d][%d]=", i, k); scanf("%d", &in->pscs2[i][k]); //вот здесь } } break; case 8: { printf("\n Input three nearby numbers"); for (k = 0; k < 3; k++) { printf("\n pscs3[%d][%d]=", i, k); scanf("%d", &in->pscs3[i][k]); } } break; case 9: { printf("\n Input four nearby numbers"); for (k = 0; k < 4; k++) { printf("\n pscs4[%d][%d]=", i, k); scanf("%d", &in->pscs4[i][k]); } } break; case 10: { printf("\n Input six nearby numbers"); for (k = 0; k < 6; k++) { printf("\n pscs6[%d][%d]=", i, k); scanf("%d", &in->pscs6[i][k]); } } break; default: { printf("\n You input other number"); printf("\n Can you repeat, please"); goto up; } } } } /*void random(int *Num) { srand(time(NULL)); *Num = rand() % 36; }*/
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д