为什么当我开始输入更改服务类型或'4'时,循环会停止????以及其他一些问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么当我开始输入更改服务类型或'4'时,循环会停止????以及其他一些问题相关的知识,希望对你有一定的参考价值。

在此代码中,从输入“ 1”开始输入详细信息,然后输入“ 2”以查看详细信息,“ 3”以查看并选择要选择的服务类型会遇到一些问题,但是无论何时我输入一旦到达“ changing service(1-8)type?”,“ 4”循环便会停止。然后也可以通过输入“ 6”和“ 7”来分别搜索客户名称和日期,并显示已存储在文件中的信息,但是只要我输入名称或日期,它都表示细分错误。那么,大家对如何解决它有任何建议吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void entry()

      FILE * fpointer;
      char name[20], gender[20], op, option, date[50];
      int visit_ID, phone_number, service_needed, change_service;
      float service_fee, payment_made, changes, change_service_fee;


      printf("\n\nEnter information:\n");
      printf("\nEnter name: ");
      scanf("%s", name);
      printf("\n\nEnter your ID number(Number only): ");
      scanf("%d", &visit_ID);
      printf("\n\nEnter today date in dd-mm-yy: ");
      scanf("%s" ,date);
      printf("\n\nEnter your gender(Male/Female): ");
      scanf("%s",gender);
      printf("\n\nEnter your phone number: ");
      scanf("%d", &phone_number);
      printf("\n\nDone registered\n\n");

      if((fpointer = fopen("visitdetails.txt", "a")) == NULL)
        
          perror("Write");
              exit(1);
        
      fprintf(fpointer, "Visit ID = %d", visit_ID);
      fprintf(fpointer, "\nCustomer name = %s", name);
      fprintf(fpointer, "\nLast visit date : %s", date);
      fprintf(fpointer,"\nLast Service type repaired: %d", service_needed);
      fscanf(fpointer,"%d",service_needed);
      fprintf(fpointer, "\nLast Service fee: %.2f",service_fee);
      fprintf(fpointer,"\nLast payment: %.2f",payment_made);
      printf("\n\nRecord Written Successfully!!!\n\n");
      fclose(fpointer);

void Repair()

      char name[20], gender[20], op, option, date[50];
      int visit_ID, phone_number,service_needed;
      float service_fee, payment_made, changes, change_service_fee;

      FILE *Welcome;
      Welcome = fopen("visitdetails.txt","r");
          printf("\nWhich service type do you need(1-8)? ");
          scanf("%d", &service_needed);

          switch(service_needed)
          
            case 1:
                printf("\nThe cost of it will be 30RM for Normal and 50RM for Urgent(type the cost): ");
                scanf("%f", &service_fee);
                break;

            case 2:
                printf("\nThe cost of it will be 40RM for Normal and 70RM for Urgent(type the cost): ");
                scanf("%f", &service_fee);
                break;

            case 3:
                printf("\nThe cost of it will be 330RM for Normal and 430RM for Urgent(type the cost): ");
                scanf("%f", &service_fee);
                break;

            case 4:
                printf("\nThe cost of it will be 160RM for Normal and 200RM for Urgent(type the cost): ");
                scanf("%f", &service_fee);
                break;

            case 5:
                printf("\nThe cost of it will be 180RM for Normal and 210RM for Urgent(type the cost): ");
                scanf("%f",&service_fee);
                break;

            case 6:
                printf("\nThe cost of it will be 50RM for Normal and 80RM for Urgent(type the cost): ",service_fee);
                scanf("%f",&service_fee);
            break;

            case 7:
                printf("\nThe cost of it will be 100RM for Normal and 150RM for Urgent(type the cost): ",service_fee);
                scanf("%f",&service_fee);
            break;

            case 8:
                printf("\nThe cost of it will be 50RM for Normal and 75RM for Urgent(type the cost): ",service_fee);
                scanf("%f",&service_fee);
            break;

            default:
            printf("Invalid option");
          

void Viewing_Store()

  FILE *view;
  view = fopen("visitdetails.txt","r");

  printf("No |    Service Type                                    | Needed Days | Service Fee | \n");
  printf("                                                                      |Normal|Urgent|\n");
  printf("1. | Remove virus,Malware, or Spyware                   |      2      | 30RM | 50RM |\n");
  printf("2. | Troubleshot and fix computer running slow          |      2      | 40RM | 70RM |\n");
  printf("3. | Laptop Screen Replacement                          |      3      | 330RM| 430RM|\n");
  printf("4. | Laptop Keyboard Replacement                        |      2      | 160RM| 200RM|\n");
  printf("5. | Laptop Battery Replacement                         |      1      | 180RM| 210RM|\n");
  printf("6. | Operating System Format and Installation           |      2      | 50RM | 80RM |\n");
  printf("7. | Data Backup and Recovery                           |      2      | 100RM| 150RM|\n");
  printf("8. | Internet Connectivity issues                       |      1      | 50RM | 75RM |\n");


  fclose(view);


void payment_store()

  FILE *payment_stores;

  char name[20], gender[20], op, option, date[50],choose;
  int visit_ID, phone_number, service_needed, change_service;
  float service_fee, payment_made, changes, change_service_fee,price;

  payment_stores = fopen("visitdetails.txt", "a");
  printf("\nDo you change your service type(Y/N): \n",choose);
  scanf("%s",&choose);
  if(choose == 'Y')
  
     printf("Here is the price %.2f\n", change_service_fee);
     printf("\n\nPayment: ", payment_made);
     scanf("%f",&payment_made);
     if(payment_made > change_service_fee)
     
        changes = payment_made - change_service_fee;
        printf("\nHere is your changes: %.2f\n", changes);
     
     else if(payment_made == change_service_fee)
     
        changes = payment_made - change_service_fee;
        printf("\nHere is your changes: %f\n", changes);
     
     else
     
        printf("Error");
     
  
  else if(choose == 'N')
  
     printf("\n\nPayment: ", payment_made);
     scanf("%f",&payment_made);
     if(payment_made > service_fee)
     
        changes = payment_made - service_fee;
        printf("\nHere is your changes :%.2f\n", changes);
     
     else if(payment_made == service_fee)
     
        changes = payment_made - service_fee;
        printf("\nHere is your changes %f\n", changes);
     
     else
     
        printf("Error");
     
  
  fprintf(payment_stores,"\nPayment: %.2f",payment_made);
  fclose(payment_stores);

void change_type()

  FILE *change;
  char name[20], gender[20], op, option, date[50];
  int visit_ID, phone_number, service_needed, change_service;
  float service_fee, payment_made, changes, change_service_fee;

      change = fopen("visitdetails.txt","r");
      printf("\nAre you sure you want to change the service type(Y/N) : ", op);
      scanf("%s", &op);
      if(op == 'Y')
      
        printf("\nWhat service type do you want to change into(1-8)? ", change_service);
        scanf("%d", &change_service);
        switch(change_service)
        
          case 1:
          printf("\nThe cost of it will be 30RM for Normal and 50RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          case 2:
          printf("\nThe cost of it will be 40RM for Normal and 70RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          case 3:
          printf("\nThe cost of it will be 330RM for Normal and 430RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          case 4:
          printf("\nThe cost of it will be 160RM for Normal and 200RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          case 5:
          printf("\nThe cost of it will be 180RM for Normal and 210RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          case 6:
          printf("\nThe cost of it will be 50RM for Normal and 80RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          case 7:
          printf("\nThe cost of it will be 100RM for Normal and 150RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          case 8:
          printf("\nThe cost of it will be 50RM for Normal and 75RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          default:
          printf("Invalid option");
        
      
      else if(op == 'N')
      
        printf("\nThe final price will be : %.2f", service_fee);
        printf("\nClick on payment to pay");
      
      else
      
        printf("Error Input");
      

      fprintf(change, "\n\nChanging service type into: %d", change_service);
      fprintf(change, "\nThe price of this service type is: %.2f", change_service_fee);
      fprintf(change, "\nThe price will still be: %.2f", service_fee);
      fclose(change);

void any_visit(char anv[])

  FILE *anyvisit;

  char name[200], gender[20],date[50];
  int visit_ID, phone_number, service_needed, change_service, set;
  float service_fee, payment_made, changes, change_service_fee;

  anyvisit = fopen("visitdetails.txt","r");
  while(fscanf(any_visit, "%s %d %s %s %d %d\n", name, visit_ID, date, gender, phone_number, payment_made) !=EOF)
  
    set = strcmp(date, anv);
    if(set == 0)
    
      printf("Visit_ID is %d", visit_ID);
      printf("\nName : %s", name);
      printf("\nGender is %s", gender);
      printf("\nPhone number is %d", phone_number);
      printf("\nPayment : %.2f", payment_made);
    
  
  fclose(anyvisit);

void search(char cnm[])

  FILE *search;

  char name[200], gender[20],date[50];
  int visit_ID, phone_number, service_needed, change_service,tes;
  float service_fee, payment_made, changes, change_service_fee;

  search = fopen("visitdetails.txt","r");
  while(fscanf(search, "%s %d %s %s %d %d\n", name, visit_ID, date, gender, phone_number, payment_made) !=EOF)
  
    tes = strcmp(name, cnm);
    if (tes == 0)
    
      printf("Visit_ID is %d", visit_ID);
      printf("Date of visit is %s", date);
      printf("\nGender is %s", gender);
      printf("\nPhone number is %d", phone_number);
      printf("\nPayment: %d", payment_made);
    
  
  fclose(search);


void main()

  char name[200], gender[20], op, option, ch, cnm[20], anv[20],date[50];
  int visit_ID, phone_number, service_needed, change_service;
  float service_fee, payment_made, changes, change_service_fee;

  printf("*****Welcome to the Computer center*****\n");
  printf("\nBefore entering the store and have your equipment repair, it is best to view the store\n");
  do
  
  printf("\nPress '1' to enter and have your data store");
  printf("\nPress '2' to View the store");
  printf("\nPress '3' to have your equipment being repaired");
  printf("\nPress '4' if you want to change your repair type");
  printf("\nPress '5' to pay the cost of your repair");
  printf("\nPress '6' if you want to Search");
  printf("\nPress '7' if you want to search payment and unpaid balance by any visit");
  printf("\nPress '0' to exit");
  printf("\nEnter your choice : ");
  scanf("%d",&ch);
    switch(ch)
    
    case 1: entry();
        break;
    case 2: Viewing_Store();
        break;
    case 3: Repair();
        break;
    case 4:change_type();
        break;
    case 5: payment_store();
        break;
    case 6: printf("Enter your name: ");
            scanf("%s", cnm);
            search(cnm);
        break;
    case 7: printf("Enter the date you visit: ");
            scanf("%d", anv);
            any_visit(anv);
        break;
    case 0: exit(0);
    default: printf("\nInvalid Entry!!! ");
    
   while(ch != 0);

答案
I've tried to reduce as many warning as possible however, there are some warning like; data argument not used by format string [-Wformat-extra-args], format specifies type 'int ' but the argument has type 'int' [-Wformat]. I don't understand what should be change from it.



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void entry()

      FILE * fpointer;
      char name[20], gender[20], date[50];
      int visit_ID, phone_number, service_needed;


      printf("\n\nEnter information:\n");
      printf("\nEnter name: ");
      scanf("%s", name);
      printf("\n\nEnter your ID number(Number only): ");
      scanf("%d", &visit_ID);
      printf("\n\nEnter today date in dd-mm-yy: ");
      scanf("%s" ,date);
      printf("\n\nEnter your gender(Male/Female): ");
      scanf("%s",gender);
      printf("\n\nEnter your phone number: ");
      scanf("%d", &phone_number);
      printf("\n\nDone registered\n\n");

      if((fpointer = fopen("visitdetails.txt", "a")) == NULL)
        
          perror("Write");
              exit(1);
        
      fprintf(fpointer, "Visit ID = %d", visit_ID);
      fprintf(fpointer, "\nCustomer name = %s", name);
      fprintf(fpointer, "\nLast visit date : %s", date);
      fprintf(fpointer,"\nLast Service type repaired: %d", service_needed);
      printf("\n\nRecord Written Successfully!!!\n\n");
      fclose(fpointer);

void Repair()

      int service_needed;
      float service_fee;

      FILE *Welcome;
      Welcome = fopen("visitdetails.txt","r");
          printf("\nWhich service type do you need(1-8)? ");
          scanf("%d", &service_needed);

          switch(service_needed)
          
            case 1:
                printf("\nThe cost of it will be 30RM for Normal and 50RM for Urgent(type the cost): ");
                scanf("%f", &service_fee);
                break;

            case 2:
                printf("\nThe cost of it will be 40RM for Normal and 70RM for Urgent(type the cost): ");
                scanf("%f", &service_fee);
                break;

            case 3:
                printf("\nThe cost of it will be 330RM for Normal and 430RM for Urgent(type the cost): ");
                scanf("%f", &service_fee);
                break;

            case 4:
                printf("\nThe cost of it will be 160RM for Normal and 200RM for Urgent(type the cost): ");
                scanf("%f", &service_fee);
                break;

            case 5:
                printf("\nThe cost of it will be 180RM for Normal and 210RM for Urgent(type the cost): ");
                scanf("%f",&service_fee);
                break;

            case 6:
                printf("\nThe cost of it will be 50RM for Normal and 80RM for Urgent(type the cost): ");
                scanf("%f",&service_fee);
            break;

            case 7:
                printf("\nThe cost of it will be 100RM for Normal and 150RM for Urgent(type the cost): ");
                scanf("%f",&service_fee);
            break;

            case 8:
                printf("\nThe cost of it will be 50RM for Normal and 75RM for Urgent(type the cost): ");
                scanf("%f",&service_fee);
            break;

            default:
            printf("Invalid option");
          
      fclose(Welcome);

void Viewing_Store()

  FILE *view;
  view = fopen("visitdetails.txt","r");

  printf("No |    Service Type                                    | Needed Days | Service Fee | \n");
  printf("                                                                      |Normal|Urgent|\n");
  printf("1. | Remove virus,Malware, or Spyware                   |      2      | 30RM | 50RM |\n");
  printf("2. | Troubleshot and fix computer running slow          |      2      | 40RM | 70RM |\n");
  printf("3. | Laptop Screen Replacement                          |      3      | 330RM| 430RM|\n");
  printf("4. | Laptop Keyboard Replacement                        |      2      | 160RM| 200RM|\n");
  printf("5. | Laptop Battery Replacement                         |      1      | 180RM| 210RM|\n");
  printf("6. | Operating System Format and Installation           |      2      | 50RM | 80RM |\n");
  printf("7. | Data Backup and Recovery                           |      2      | 100RM| 150RM|\n");
  printf("8. | Internet Connectivity issues                       |      1      | 50RM | 75RM |\n");


  fclose(view);


void payment_store()

  FILE *payment_stores;

  char  choose;
  float service_fee, payment_made, changes, change_service_fee;

  payment_stores = fopen("visitdetails.txt", "a");
  printf("\nDo you change your service type(Y/N): ",choose);
  scanf("%s",&choose);
  if(choose == 'Y')
  
     printf("\nHere is the price %.2f\n", change_service_fee);
     printf("\nPayment: ", payment_made);
     scanf("%f",&payment_made);
     if(payment_made > change_service_fee)
     
        changes = payment_made - change_service_fee;
        printf("\nHere is your changes: %.2f\n", changes);
     
     else if(payment_made == change_service_fee)
     
        changes = payment_made - change_service_fee;
        printf("\nHere is your changes: %f\n", changes);
     
     else
     
        printf("Error");
     
  
  else if(choose == 'N')
  
     printf("\n\nPayment: ", payment_made);
     scanf("%f",&payment_made);
     if(payment_made > service_fee)
     
        changes = payment_made - service_fee;
        printf("\nHere is your changes :%.2f\n", changes);
     
     else if(payment_made == service_fee)
     
        changes = payment_made - service_fee;
        printf("\nHere is your changes %f\n", changes);
     
     else
     
        printf("Error");
     
  
  fprintf(payment_stores, "\nLast Service fee: %.2f",service_fee);
  fprintf(payment_stores,"\nPayment: %.2f",payment_made);
  fclose(payment_stores);

int change_type()

  FILE *change;
  char op;
  int change_service;
  float service_fee, change_service_fee;

      change = fopen("visitdetails.txt","a");
      printf("\nAre you sure you want to change the service type(Y/N) : ", op);
      scanf("%s", &op);
      if(op == 'Y')
      
        printf("\nWhat service type do you want to change into(1-8)? ", change_service);
        scanf("%d", &change_service);
        switch(change_service)
        
          case 1:
          printf("\nThe cost of it will be 30RM for Normal and 50RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          case 2:
          printf("\nThe cost of it will be 40RM for Normal and 70RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          case 3:
          printf("\nThe cost of it will be 330RM for Normal and 430RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          case 4:
          printf("\nThe cost of it will be 160RM for Normal and 200RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          case 5:
          printf("\nThe cost of it will be 180RM for Normal and 210RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          case 6:
          printf("\nThe cost of it will be 50RM for Normal and 80RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          case 7:
          printf("\nThe cost of it will be 100RM for Normal and 150RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fee);
          break;

          case 8:
          printf("\nThe cost of it will be 50RM for Normal and 75RM for Urgent(type the cost): ",change_service_fee);
          scanf("%f",&change_service_fe

以上是关于为什么当我开始输入更改服务类型或'4'时,循环会停止????以及其他一些问题的主要内容,如果未能解决你的问题,请参考以下文章

怎么修改计算机域的帐号和密码

[输入新编号时输入类型编号的更改事件

为啥我不能更改要提交的输入元素的类型?

为啥我不能更改要提交的输入元素的类型?

Python 3.7.x 介绍 -4 输入和输出

无法定义使用'dynamic'的类或成员,因为编译器需要输入'System.Runtime.CompilerServices.DynamicAttribute'类型(代