Grazie Oregon l'ho riscritto seguendo i tuoi consigli.
Ora sembra funzionare, ma non capisco perchè quando inserisco il valore 343 mi restituisce il valore (Total) -18...... , perchè mi compare il segno "-".
Codice PHP:#include <stdio.h>
#include <stdlib.h>
#include <cctype>
#includ#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1)
#endif
#define BUFSIZE 100
int main()e <errno.h>
{ size_t q; /*quantiy*/
char buff[BUFSIZE];
int n = 0;
char* end_ptr;
bool flag;
for(;;){
size_t p = 10000000 ;/*price*/
size_t t = 0; /*total*/
printf("%s","Suppose the article costs 10.000.000 E \n");
printf("%s","Enter quantity: ");
int char_count = 0;
char ch;
ch = getchar();
if(ch == EOF )
perror("NO INPUT");
char_count = 0;
while((ch != '\n') && (char_count <BUFSIZE)) {
flag = true;
buff[char_count++] = ch;
if(isdigit(ch)==0){
flag = false;
}
ch = getchar();
if(ch == EOF )
perror("NO INPUT");
}
buff[char_count] = 0x00; /* null terminate buffer */
if(flag == true){
errno = 0;
q = (int) strtoul(buff,NULL,0);
if (ERANGE == errno) {
if (puts("number out of range\n") == EOF) {
}
}
else if (end_ptr == buff) {
if (puts("not valid numeric input\n") == EOF) {
}
}
else {
if(q==0){
return 0; /*with 0 we terminate*/
}
if(q>0 && SIZE_MAX/p>=q){
t = q*p;
printf("%s %d","Total:",t);
printf("%s %s","\n","\n");
}
else{
printf("%s", "TOO BIG \n");
}
}
}
}
system("PAUSE");
return 0;
}

Rispondi quotando