-1

好吧,我为一些家庭作业编写了这个代码,我知道它可以被更好地修改,并且可以使用更多的评论,但是这个错误正在杀死我......想知道它只是我还是什么,但你能告诉我错误是否发生给你。

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

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

2345
AA
20
30
40
50
60
10
R(应该输入)但程序跳过

假设为 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");
4

3 回答 3

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);

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

于 2013-10-09T08:19:25.697 回答
0

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

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

您的编译器在rea_typescanf 中跳过输入的一个可能原因是用户输入了换行符,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);
于 2013-10-09T08:26:16.883 回答
0

它不会跳过读取字符,它会读取输入缓冲区中最后一个scanf.

scanf通过告诉跳过前导空格,这很容易解决:

scanf(" %c", &rea_type);

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

于 2013-10-09T08:27:54.373 回答