Ошибка из-за case - C (СИ)
Формулировка задачи:
Вот моя программа
При вводе допустим as он выводит case a и все ошибки из него и еще позиция default выводится ,причем не один раз ,а столько сколько букв ввел... Как я понял это из-за цикла while но убрать его я не могу. Как мне сделать чтобы блокировались остальные буквы и просто выдавалась ошибка в случае ввода больше одной буквы??
Листинг программы
- #include <stdio.h>
- #include <conio.h>
- #include <memory.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #define N 40
- //Var 4
- int main()
- {
- int n=0,j=0,i,is_symbol_entered=0,is_number_trizn=0,condition_of_exit=0,Sum=0,Mult=0;
- char s[N]={0};
- double leg1 = 0.0,leg2 = 0.0,hyp=0.0;
- printf("Input 'a' or 'b' for choice variant or 'e' for exit: ");
- while ((j = getchar())!=EOF)
- {
- switch (j)
- {
- case 'a':
- {
- printf("\nInput legs: ");
- printf("leg1= ");
- scanf("%lf",&leg1);
- printf("\nleg2= ");
- scanf("%lf",&leg2);
- if ((leg1>0) && (leg2>0))
- {
- hyp=sqrt((pow(leg1,2)+pow(leg2,2)));
- printf("\nhypotenuse=%.1lf\n",hyp);
- }
- else
- {
- printf("Error! You input negative leg!");
- }
- }
- break;
- case 'b':
- {
- printf("Input number: ");
- for (i=0; i<N-1 && (n != '\n') ; i++)
- {
- n=getchar();
- if (i==0 && (n=='\n' || n==EOF || n=='0'))
- {
- is_symbol_entered=1;
- }
- if (n==EOF || n=='\n')
- {
- break;
- }
- s[i]=n;
- if (i>=3)
- {
- is_number_trizn=1;
- }
- else
- {
- if (n<'0' || n>'9')
- {
- is_symbol_entered=1;
- }
- }
- }
- if (is_symbol_entered!=1 && is_number_trizn!=1 && (i==3))
- {
- atoi(s);
- Sum=s[0]-'0'+s[1]-'0'+s[2]-'0';
- Mult=(s[0]-'0')*(s[1]-'0')*(s[2]-'0');
- printf("Thousand: %c ",s[0]);
- printf("Ten: %c ",s[1]);
- printf("One: %c ",s[2]);
- printf("\nSum: %i",Sum);
- printf("\nmult: %i\n",Mult);
- }
- else
- {
- printf("Error!\n");
- }
- }
- break;
- case 'e':
- exit(1);
- default:
- printf("\nPlease enter 'a' or 'b' or 'e' for exit: ");
- }
- }
- getch();
- return 0;
- }
Решение задачи: «Ошибка из-за case»
textual
Листинг программы
- ...
- char j[2];
- while (1)
- {
- printf("Input 'a' or 'b' for choice variant or 'e' for exit: ");
- scanf("%s",&j);
- if (strlen(j)==1)
- switch (*j)
- {
- case 'a':
- {
- printf("\nInput legs: ");
- printf("leg1= ");
- scanf("%lf",&leg1);
- printf("\nleg2= ");
- scanf("%lf",&leg2);
- if ((leg1>0) && (leg2>0))
- {
- hyp=sqrt((pow(leg1,2)+pow(leg2,2)));
- printf("\nhypotenuse=%.1lf\n",hyp);
- }
- else
- {
- printf("Error! You input negative leg!\n");
- }
- }
- break;
- case 'b':
- {
- printf("Input number: ");
- for (i=0; i<N-1 && (n != '\n') ; i++)
- {
- scanf("%d",&n);
- if (i==0 && (n=='\n' || n==EOF || n=='0'))
- {
- is_symbol_entered=1;
- }
- if (n==EOF || n=='\n')
- {
- break;
- }
- s[i]=n;
- if (i>=3)
- {
- is_number_trizn=1;
- }
- else
- {
- if (n<'0' || n>'9')
- {
- is_symbol_entered=1;
- }
- }
- }
- if (is_symbol_entered!=1 && is_number_trizn!=1 && (i==3))
- {
- atoi(s);
- Sum=s[0]-'0'+s[1]-'0'+s[2]-'0';
- Mult=(s[0]-'0')*(s[1]-'0')*(s[2]-'0');
- printf("Thousand: %c ",s[0]);
- printf("Ten: %c ",s[1]);
- printf("One: %c ",s[2]);
- printf("\nSum: %i",Sum);
- printf("\nmult: %i\n",Mult);
- }
- else
- {
- printf("Error!\n");
- n=0;
- }
- }
- break;
- case 'e':
- exit(1);
- default:
- continue;
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д