빈 공간이있는 장치 이름을 입력하면 종료됩니다. 나는 다음 루프가 건너 뛸 때 다음 fget로 변경하려고했습니다 "장치 이름을 입력합니다. 나는 또한 시도"%의 [^ \ n을을]의 "그것은 또한 같은 일.c에서 루프 내의 빈 공간이있는 문자열을 스캔하여 완벽한 테이블을 만듭니다.
#include <stdio.h>
#include <stdlib.h>
struct deviceType
{
char cName[20];
float fPrice;
int iQty;
float fTotal;
};
void fnKeyIn(float*,int*);
float fnCalcTotal(const float,int);
void fnGetDiscount (const float,double*);
float fnAfterDiscount(const float,double);
void fnPrint(struct deviceType ,double,const float);
int main()
{
struct deviceType asEntertaiment[50] ;
char ans;
int n,i=0;
double afDiscount[i];
float afAfterDiscount[i];
float full = 0;
do{
printf("\nEnter device name: ");
//gets(asEntertaiment[i].cName);
scanf("%s",asEntertaiment[i].cName);
getchar();
fnKeyIn(&asEntertaiment[i].fPrice,&asEntertaiment[i].iQty);
printf("\nDo you want to add more?\n");scanf("%s",&ans);
i++;n=i;
}while((ans=='Y')||(ans=='y'));
printf("\nDevices Price Quantity Total Discount After Discount\n");
for(i=0;i<n;i++)
{
asEntertaiment[i].fTotal = fnCalcTotal(asEntertaiment[i].fPrice,asEntertaiment[i].iQty);
if(asEntertaiment[i].iQty>=50)
{
fnGetDiscount (asEntertaiment[i].fTotal,&afDiscount[i]);
}
else
{
afDiscount[i]= 0;
}
afAfterDiscount[i] = fnAfterDiscount(asEntertaiment[i].fTotal,afDiscount[i]);
full=full + afAfterDiscount[i];
fnPrint(asEntertaiment[i],afDiscount[i],afAfterDiscount[i]);
}
printf("\nTotal amount payable: %3.2f",full);
return 0;
}
void fnKeyIn(float *price,int *quantity)
{
printf("\nEnter device price: RM ");
scanf("%f",price);
printf("\nEnter quantity ordered: ");
scanf("%d",quantity);
}
float fnCalcTotal(const float Price,int Qty)
{
return(Price*Qty);
}
void fnGetDiscount (const float Total,double *Discount)
{
*Discount=Total*0.1;
}
float fnAfterDiscount(const float Total,double Discount)
{
return(Total-Discount);
}
void fnPrint(struct deviceType E,double Discount,const float After)
{
printf("\n%s %3.2f %d %3.2f %3.2lf %3.2f ",E.cName,E.fPrice,E.iQty,E.fTotal,Discount,After);
}
전체 줄을 읽으려면 ['fgets'] (http://en.cppreference.com/w/c/io/fgets)를 사용하십시오. ['fgets'] (http://en.cppreference.com/w/c/io/fgets)는 끝 줄 바꿈을 버퍼에 씁니다. –
나는 이미 fgets를 시험해 본다. 다음 루프는 입력 장치 이름을 건너 뜁니다. – diniebee
@ JoachimPileborg 내가 fgets를 삽입 할 때 출력 그림 삽입 – diniebee