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