2016-10-09 2 views
0

빈 공간이있는 장치 이름을 입력하면 종료됩니다. 나는 다음 루프가 건너 뛸 때 다음 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); 
     } 
+0

전체 줄을 읽으려면 ['fgets'] (http://en.cppreference.com/w/c/io/fgets)를 사용하십시오. ['fgets'] (http://en.cppreference.com/w/c/io/fgets)는 끝 ​​줄 바꿈을 버퍼에 씁니다. –

+0

나는 이미 fgets를 시험해 본다. 다음 루프는 입력 장치 이름을 건너 뜁니다. – diniebee

+0

@ JoachimPileborg 내가 fgets를 삽입 할 때 출력 그림 삽입 – diniebee

답변

0

않습니다 scanf() 함수는 입력 스트림에 원하지 않는 문자를 남겨 둡니다. 사용자가 입력 한 문자가 입력 스트림에 계속 남아있는 경우 사용자가 입력 스트림에 입력 한 문자가 그대로 남아 있습니다.

printf("\nEnter device name: "); 
//gets(asEntertaiment[i].cName); 
scanf("%s",asEntertaiment[i].cName); 
while (getchar() != '\n') 
    continue;    // discard extra characters 

기지를 실제로 다루려면 다음을 수행 할 수 있습니다.

int ch; 
while((ch = getchar()) != '\n' && ch != EOF) 
    continue; 

EOF을 테스트하려는 경우 int 또는 부호있는 정수 유형을 사용해야합니다. char은 서명이 없습니다. 당신은 여전히 ​​입력을 취소 할 필요가

printf("\nEnter device name: "); 
//gets(asEntertaiment[i].cName); 
fgets(asEntertaiment[i].cName, 50, stdin); 
int find = 0; 
while (find < 49 && (asEntertaiment[i].cName)[find] != '\n') 
    ++find; 
if (find == 49) { 
    while (getchar() != '\n') 
     continue;    // discard extra characters 
    } 
(asEntertaiment[i].cName)[find] = '\0'; 

:

먼저 도래하는 첫 번째 줄 바꿈, 또는 문자의 최대 수까지 문자를 읽어 fgets()를 사용해보십시오, 여러 단어의 입력을 얻으려면 사용자가 49 자 (49 자 + '\ 0')보다 긴 장치 이름을 입력 할 경우 추가 문자 스트림. 이러한 여분의 문자는 fgets() 호출 후 여전히 입력 스트림에 있습니다. 이것은 그대로 작동하지만이 입력을 처리하는 함수와 필요할 때마다 호출 할 수있는 정리를 작성하는 것이 좋습니다.

일부 입력 기능은 입력 스트림에서 뒤에 입력 기능을 정리하지 않으면 바로 다음 입력 기능을 기다리고있는 문자를 남겨 두는 것을 기억해야합니다. 또한 입력 내용을 실제로 검증해야합니다. 사용자가 "Enter device price :"프롬프트에서 숫자 대신 문자열을 입력하면 프로그램이 열중하게됩니다. scanf()은 성공적인 할당 수인 int을 반환합니다. 따라서 int을 요청하고 사용자가 문자열을 입력하면 반환 값은 0입니다.이 값을 사용하여 사용자의 입력을 테스트하고 입력이 올바르지 않으면 다시 입력하도록 루프 할 수 있습니다.

+0

@ 데이비드 볼링 감사합니다. 너의 방법을 시도해. 그러나 첫 번째 단어 만 출력합니다. 모든 단어를 출력하는 방법. 죄송합니다. 나는 이것에서 새롭다. – diniebee

+0

나는 오해했다. 나는 첫 단어 만 원한다고 생각했다. 나는 나의 대답을 업데이트 할 것이다. –

+0

고맙습니다. 그걸 가지고 있습니다 :) – diniebee

관련 문제