Em gặp lỗi hàm CheckArry khi thực thi đến hàm này đơ vài giây sau đó kết thúc chương trình luôn
#include <stdio.h>
#include <cstdlib>
#include <stdbool.h>
bool CheckArray(int **arr,int index){
fflush(stdin);
char temp;
printf("\ntest0 \n");
if( scanf("%d%c",&(arr)[index], &temp) == 2){
printf("\ntest 1 \n");
if(temp == '\n')
printf("\ntest 2 \n");
return true;
}
return false;
}
void addValue(int **arr , int &size){
fflush(stdin);
size++;
arr = (int**) realloc(arr, (size) * sizeof(int*));
printf("Input number to array:");
while( !CheckArray(arr, size-1 ) ){
printf("Invaild a value: ");
};
//scanf("%d",arr+(size-1));
printf("Number of array: %d",arr[size-1]);
}
bool CheckMenu (int *number , int min , int max ) {
fflush(stdin);
char temp;
printf("Input menu:\n");
if( scanf("%d%c", number, &temp) == 2)
{
if((temp == '\n') && ((*number >= min) && (*number <= max)))
return true;
}
return false;
}
void menu(int **arr, int &size){
int number ;
while(true){
if(number == 6 ) break ;
printf("----Chuong Trinh----\n");
printf("1. Add a value\n");
printf("2. Search a value\n");
printf("3. Print out array \n");
printf("4. Print out values in range inputted min and max values, inclusively\n");
printf("5. Sort the array in ascending order\n");
printf("0. Exit\n\n");
while(!CheckMenu( &number, 1,6 ) ){
printf("loi roi nhap lai di \n");
}
switch (number)
{
case 1: addValue(arr,size) ; break;
}
}
}
int main(){
int size = 1;
int *arr;
arr = (int *)malloc(size* sizeof(int));
menu(&arr,size);
free(arr);
return 0 ;
}