+#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "io.h"
#include "array.h"
-#include "utils.h"
int prompt_value(const char* msg, int* result) {
puts(msg);
int errno = prompt_value("Taille du tableau?", (int*)size);
array = create_tab(array, *size);
for (unsigned i = 0; i < *size; i++) {
- errno += prompt_value("Valeur?", &array[i]);
+ errno += prompt_value("Valeur?", &array[i]);
+ /* error might have occured */
+ handle_prompt_error(errno, array);
}
- /* error might have occured */
- handle_prompt_error(errno);
return array;
}
printf("4) Croissant impairs en premier.\n");
do {
errno = prompt_value("Choix?", &choice);
- handle_prompt_error(errno);
+ handle_prompt_error(errno, array);
done = true;
if (1 > choice || 4 < choice) {
printf("\nFaire un choix compris entre 1 et 4\n");
int search_value = 0;
errno = prompt_value("\nValeur a chercher?", &search_value);
- handle_prompt_error(errno);
+ handle_prompt_error(errno, array);
printf("La valeur %d est presente %d fois dans le tableau\n", search_value, count_tab_element(array, size, search_value));
}
unsigned new_size = 0;
errno = prompt_value("\nNouvelle taille?", (int*)&new_size);
- handle_prompt_error(errno);
- /* FIXME: one should able the set the array new content if new_size > *old_size
- * for now, new values are zeroed */
+ handle_prompt_error(errno, array);
array = resize_tab(array, *old_size, new_size);
+ if (new_size > *old_size) {
+ printf("Saisie des valeurs supplementaires du tableau\n");
+ for (unsigned i = *old_size; i < new_size; i++) {
+ errno += prompt_value("Valeur?", &array[i]);
+ handle_prompt_error(errno, array);
+ }
+ }
*old_size = new_size;
}
+void handle_prompt_error(int errno, int* tab) {
+ if (errno != 0) {
+ printf("\nMerci de saisir un nombre entier, exiting\n");
+ /* it's somewhat violent but better than looping forever */
+ free_tab(tab);
+ exit(EXIT_FAILURE);
+ }
+}
+
void display_array(int array[], unsigned size) {
if (array != NULL) {
printf("\n--array begin--\n");