为什么我的阅读类型是空白的?

时间:2013-10-09 08:16:24

标签: c

好吧,我把它编成了一些功课,我知道它可以更好地修改,可以使用更多的评论,但这一个错误正在杀了我......想知道它是否只是我或者其他什么,但你能告诉我是否这个错误发生在你身上。

错误:阅读类型没有任何数据。

(你现在可以使用这些输入,因为我没有错误检查程序) 测试变量:

2345
AA
20
30
40
50
60
10
R( supposed to be entered) but program skip 

假设为rea_type获取输入但跳过它并继续到其他行。

以下是代码:

  const float deposit  = 1500.00;
const unsigned multiplier =1;
const float rate_One = 6.350;
const float rate_Two = 14.520;
unsigned bill_Cycle = 0,
         no_Days = 0;
float bi_Exch_Rate = 0,
      ba_Exch_Rate = 0;
float cur_Read=0,
      prev_Read=0,
      cur_Usage=0,
      cur_Peri_Charg=0;
char c_Digit[20],
     premis_numb[20];
char rea_type;
/**Information to be collected from the user**/
      system("cls");
      printf("\nPlease enter the customer digits: ");
      scanf("%s",&c_Digit);
      printf("\nPlease enter Premise Number:");
      scanf("%s",&premis_numb);
      printf("\nPlease enter the Billing Cycle: ");
      scanf("%d",&bill_Cycle);
      printf("\nPlease enter the No. of Days: ");
      scanf("%d",&no_Days);
      printf("\nPlease enter the Billing Exchange Rate: ");
      scanf("%f",&bi_Exch_Rate);
      printf("\nPlease enter the Base Exchange Rate: ");
      scanf("%f",&ba_Exch_Rate);
      printf("\nPlease enter the Current Reading: ");
      scanf("%f",&cur_Read);
      printf("\nPlease enter the Previous Reading: ");
      scanf("%f",&prev_Read);
      printf("What is the Reading Type: ");
      scanf("%c",&rea_type);
       cur_Usage = cur_Read-prev_Read;
         if (cur_Usage<100)
         {
           cur_Peri_Charg = cur_Usage*rate_One;
         }
         else
         {
          cur_Peri_Charg = (((cur_Usage-100) * rate_One)+(cur_Usage*rate_Two));
         }
       strcat(premis_numb,c_Digit);/**Joins the Premis Number and the Customer digits together**/
 /**Information to be displayed showing user all input and calculations.**/
  system("cls");
 printf("\tStored constants for calculation of customer bill\n");
 printf("      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
 printf("       Customer Number:%s",premis_numb);
 printf("          Current Usage:%.2f \n",cur_Usage);
 printf("\n");
 printf("       Billing Cycle:%d\t\t       No of Days:%d\n",bill_Cycle,no_Days);
 printf("\n");
 printf("       Billing Exchange Rate:%.2f     Base Exchange Rate:%.2f\n",bi_Exch_Rate,ba_Exch_Rate);
 printf("\n");
 printf("       Deposit:%.2f\t\t       Multiplier:%d\n",deposit,multiplier);
 printf("\n");
 printf("       Rate 1:%.3f\t\t       Rate 2:%.3f\n",rate_One,rate_Two);
 printf("\n");
 printf("       Current Reading:%.2f\t       Previous Reading:%.2f\n",cur_Read,prev_Read);
 printf("\n");
 printf("       Current Usage Reading:%.2f    Reading Type:%c\n",cur_Peri_Charg,rea_type);
 printf("      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

3 个答案:

答案 0 :(得分:1)

考虑使用rea_type的字符串,因为它对于输入错误比单个字符更强大,即更改:

char rea_type;

为:

char rea_type[20];

  scanf("%c",&rea_type);

为:

  scanf("%s", rea_type);



另请注意,理想情况下应更改两者:

  scanf("%s",&c_Digit);

  scanf("%s",&premis_numb);

为:

  scanf("%s", c_Digit);

  scanf("%s", premis_numb);

您不需要获取字符串的地址,因为它实际上已经是指针。在这种特殊情况下并不重要,但是当字符串确实是指针(而不是数组)时,这是一个很好的习惯。

答案 1 :(得分:0)

您无需传递char [] 的引用。 premin_num&amp; c_Digit是char []的起始地址。这样做

 scanf("%s",premis_numb);
 scanf("%s",c_Digit);

编译器在rea_type scanf中跳过输入的一个可能原因是因为用户输入了换行符so if there's a stray newline in the input stream (from a previous entry, for example) the scanf call will consume it immediately & skip taking input.

<强>解决方案:

%c

之前提供空间
scanf(" %s",&rea_type);

答案 2 :(得分:0)

它不会跳过读取该字符,它会在最后scanf之后读取输入缓冲区中的换行符

通过告诉scanf跳过前导空格来解决这个问题很简单:

scanf(" %c", &rea_type);

注意格式化代码之前的空格。