0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "mainl.h"
static struct prod_details pd;
char *getinput(char *inp)
{
printf("Enter the amount of the product %d:\n",pd.no_prod+1);
gets(inp);
return inp;
}
void print()
{
printf("........................\n");
printf("No of Product is: %d\n",pd.no_prod);
printf("Grant Total is : %.2f\n",pd.total);
printf("........................\n");
}
int check(char *str)
{
int i;
if(strlen(str) == 0)
return 2;
for(i=0;i<strlen(str);i++)
{
if(str[i] == '-')
return 3;
if(isalpha(str[i]) != 0)
return 0;
}
return 1;
}
void calc(char *str)
{
pd.array[pd.no_prod]=atof(str);
pd.total=pd.total+pd.array[pd.no_prod];
printf("Total is:%.2f\n",pd.total);
pd.no_prod++;
}
int main()
{
int chkflg,i=0,flag=0,cflag=0;
char ch;
char input[1024];
printf("..................\n");
printf("..CASE RIGISTER..\n");
printf("..................\n");
//strcpy(input,getinput(i+1));
//printf("%s\n",input);
do
{
strcpy(input,getinput(input));
chkflg=check(input);
switch(chkflg)
{
case 0:
printf("Please Enter Correctly...!!!\n");
printf("You Have entered Wrongly.!!!\n");
flag=0;
break;
case 1:
calc(input);
flag=1;
break;
case 2:
printf("You didnt enter anything.!!!\n");
flag=0;
break;
case 3:
printf("Coundnot Subtract the Amount..!!!\n");
flag = 0;
break;
}
if(flag == 0)
{
printf("Do u want to continue(y/n)");
ch=getchar();
if(ch == 'y')
{
flag=1;
//continue;
}
else if(ch == 'n')
{
printf("Thank u..!!!\n");
break;
}
else
{
printf("You didn't Enter Properly...!!!\n");
break;
}
}
}while(flag == 1);
print();
return 0;
}
이 프로그램은 청구서를 계산하는 프로그램입니다.이 프로그램은 올바른 입력 (eg.double)에 적합합니다. 그러나 문제는 문자열로 잘못 입력하면 해당 사례가 표시되고 계속할지 여부를 묻는 것입니다. 우리가 계속하고 싶다면 출력은 다음과 같이 출력됩니다 :사용자로부터 입력 받기가 작동하지 않습니다.
You Have entered Wrongly.!!!
Do u want to continue(y/n)y
Enter the Amount of the product 3:
You didn't enter anything.!!!
Do u want to continue(y/n)
입력을 더 이상 얻지 못합니다. gdb로 작업하고있었습니다. 그러나 나는 왜 그것이 입력을 더 얻을 수 없었는지 이해할 수 없었다. 이 문제를 해결하도록 도와주세요. 사전에 감사드립니다.
감사합니다. 그것은 너무 들어 가지 않습니다 .. 그것은 내 문제를 해결하지 못했습니다. – Dhasneem
@Dhasneem'fgets' (또는 안전하지 않은'gets')도 사용해 보셨습니까? –
아니요 선생님. 나는 그것을 사용하지 않았다 .fgets는 filepointer na 선생님을 필요로합니까? fgets를 사용하는 예를 들어 주시겠습니까? – Dhasneem