Bonjour,

J'ai à nouveau un problème :

voici mon programme :

test.c

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
 
#include "defines.h"
#include "string.h"
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include "error.h"
 
#if defined(__COMPILE_AVR__)
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
 
//#include "lcd.h"
#include "uart.h"
 
/*
 * Do all the startup-time peripheral initializations.
 */
static void
ioinit(void)
{
  uart_init();
//  lcd_init();
}
 
FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
//FILE lcd_str = FDEV_SETUP_STREAM(lcd_putchar, NULL, _FDEV_SETUP_WRITE);
 
#endif
 
 
//------------------------------------------------------------
  void additionner(char*);
  void soustraire(char*);
 
 
 
  struct cmdDef{
	char* cmdString;
	void (*cmdFunc)(char*);
} cmdTable[] = {
	{"add",additionner},
	{"sub",soustraire},
	{"\0",0}
};
//------------------------------------------------------------
 
int
main(void)
{
#if defined(__COMPILE_AVR__)
  	ioinit();
  	stdout = stdin = &uart_str;
#endif
  //------------------------------------------------------------
	char str[30];
	char cmd[10];
	char arg1[10],arg2[10];
	int strl,i;
	int quit = 0;
	int found;
 
	while (!quit) {
 
		printf("Command:");
 
		str[0]='\0';
		cmd[0]='\0';
		arg1[0]='\0';
        arg2[0]='\0';
 
        i = 0;
		while (1) 
		{
			if (fgets(str, sizeof str - 1, stdin) == NULL)
			{
				i = -1;                     // ERROR fgets
				check_error(i);
				break;
			}
			else
			{
				strl=strlen(str);
				printf("strlen=%d\n",strl);
				if ((strl != 0) && (str[strl-1] == '\n'))
				{
					str[strl-1] = '\0';
					break;
				}
				else
				{
					i = -2;					// ERROR input overflow	
					check_error(i);
				}
			}
		};
 
		sscanf(str, "%s %s %s", cmd, arg1, arg2);
		printf("str=%s, cmd=%s, arg1=%s, arg2=%s\n",str, cmd, arg1, arg2);
 
		if (i == 0)
		{
			if (strcmp(cmd,"quit") == 0)
			{
				quit = 1;
				break;
			}
 
			found = 0;		
			do {
				if (strcmp(cmdTable[i].cmdString,"\0") == 0)
				{
					i = -3;					// ERROR unknown command
					check_error(i);
					break;
				}
				if (strcmp(cmd,cmdTable[i].cmdString) == 0)
				{
					found = 1;
				}
				else
					i++;
			} while (!found);
 
			if (i >= 0)
			{
				(*(cmdTable[i].cmdFunc))(str);
			}
		}
	}
}
 
 
void additionner(char* s)
{
	int  i,num1, num2,K;
 
   printf("addition called with %s\n",s);
 
     if (sscanf(s, "%*s %d %d", &num1, &num2) != 2)
 
        {
                     i=-4;
                     check_error(i);
        }
 
 
     if (num1>999 || num2>999)
 
        {
                     i=-5;
                     check_error(i);
        }
 
     if (num1<-999 || num2<-999)
 
        {
                     i=error_low_range;
                     check_error(i);
        }
 
     else 
        {
                     K=num1+num2;
			         printf("%d+%d=%d\n",num1,num2,K);
 
        }
}
 
 
void soustraire(char* s)
{
	int  i,num1, num2,K;
 
   printf("soustraction called with %s\n",s);
 
     if (sscanf(s, "%*s %d %d", &num1, &num2) != 2)
 
        {
                     i=-4;
                     check_error(i);
        }
 
 
     if (num1>999 || num2>999)
 
        {
                     i=-5;
                     check_error(i); 
        }
 
     if (num1<-999 || num2<-999)
 
        {
                     i=error_low_range;
                     check_error(i);
        }
 
     else 
        {
                     K=num1-num2;
			         printf("%d-%d=%d\n",num1,num2,K);
 
        }
 
 
}
error.h

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
//------------------------------------------------------------------------------
// Gestion des erreurs
//------------------------------------------------------------------------------
 
#define valid_string 0
#define error_fgets -1
#define error_input_overflow -2
#define error_unknown_command -3
#define error_args_not_valid -4
#define error_high_range -5
#define error_low_range -6
 
 
int check_error (int);
error.c

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
#include "error.h"
 
int init_error_value;
 
int check_error(int error)
 
{  
    do 
    {
             if (error!=0 && init_error_value==0)
             {
                 printf("error number =%d\n",error);
                 init_error_value=error;
                 break;
             }
 
             if(init_error_value<0)
             {
                 printf("il y a deja une autre erreur %i\n",init_error_value);
                 break;
             }           
    }while(error==0);
 
 
}

Ce que je voudrais faire, c'est que lorsque qu'une erreur est détectée, on renvoie le code d'erreur correspondant au #define et que si une erreur est détectée, on stop le programme, si il y a plusieurs erreurs en même temps, on doit pouvoir toute les visualiser...

Je ne sais pas si je suis très clair ... Je ne souhaite pas utiliser de biblioteque standard de gestion des erreurs...