【问题标题】:How do I add a While loop to this program I would like it to be able to repeat?如何向这个程序添加一个 While 循环,我希望它能够重复?
【发布时间】:2021-04-02 01:40:15
【问题描述】:

我试图让程序能够一遍又一遍地重复我似乎无法让 while 循环工作,我不确定如何使用 while 循环来构造它。该程序计算不同物体之间的热传递。我已经尝试在开头添加一个while循环,但程序仍然没有重复

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


int main()
{
    //Variables
    char userName [50];
    int choice;        /*Material picked by the user*/
    double thickness;   /* "th" Thickness of the material in question*/
    double surfaceArea; /* "a" Surface Area of the wall in ft^2*/
    double temp1;       /* "t1" Temperature of wall 1 in Fahrenheit*/
    double temp2;       /* "t2" Temperature of wall 2 in Fahrenheit*/
    double heatTransfer; /* "h" The heat transfer rate in BTU/hr*/
    /*Variable k, which is a constant, will be defined for each material in the materials own processing*/
    
    //Prompting for Name
    printf("Hello, welcome to the Heat Trasnfer Program! Could you please input your name: "); /*prompting name of user*/
    scanf("%s",  userName);
    printf("\nHello %s, with what can I help you today? The menu is down below:\n\n", userName);/*prompting to get them ready for the menu.*/
    
    //Menu that's displayed on screen
    printf("Heat Transfer Menu:\n");
    printf("\tMaterials:\t\t \n");
    printf("\t1. Hardwood Siding\t\t \n");
    printf("\t2. Concrete Block\t\t\n");
    printf("\t3. Styrofoam\t\t\t \n");
    printf("\t4. Fiberglass\t\t\t \n");
    printf("\t5. Drywall\t\t\t \n");
    printf("\t6. Quit.\n\n");
    
    //Gathering Material that will be worked with
    printf("Please input your material choice number: ");
    scanf("%d", &choice);
    
    //Testing Choice to see if it's a valid input
    if (choice == 1)
    {
        printf("\nYou chose the material Hardwood Siding.\n");/*Verifying that the material is correct*/
        printf("\nWhat is the thickness of the Hardwood Siding in feet?: ");
        scanf("%lf", &thickness);
        printf("What is the surface area of the Hardwood Siding wall in ft?: ");
        scanf("%lf", &surfaceArea);
        printf("What about the temperature of wall 1 in Fahrenheit?: ");
        scanf("%lf", &temp1);
        printf("What about the temperature of wall 2 in Fahrenheit?: ");
        scanf("%lf", &temp2);
        //Processing the Heat Transfer of Hardwood Siding with a k of 0.092
        heatTransfer = surfaceArea*((temp2 - temp1)/(thickness/0.092));
        printf("\nThe heat transfer rate of Hardwood Siding is %lf BTU/hr.", heatTransfer);
    }
    else if (choice == 2)
    {
        printf("\nYou chose the material Concrete Block.\n");
        printf("\nWhat is the thickness of the Concrete Block in feet?: ");
        scanf("%lf", &thickness);
        printf("What is the surface area of the Concrete Block wall in ft?: ");
        scanf("%lf", &surfaceArea);
        printf("What about the temperature of wall 1 in Fahrenheit?: ");
        scanf("%lf", &temp1);
        printf("What about the temperature of wall 2 in Fahrenheit?: ");
        scanf("%lf", &temp2);
        //Processing the Heat Transfer of Concrete Block with k 0.260
        heatTransfer = surfaceArea*((temp2 - temp1)/(thickness/0.260));
        printf("\nThe heat transfer rate of Concrete Block with a thickness of %lf, a surface area of %lf, and with temperatures of %lf and %lf is %lf BTU/hr.", thickness, surfaceArea, temp1, temp2, heatTransfer);
    }
    else if (choice == 3)
    {
        printf("\nYou chose the material Styrofoam.\n");
        printf("\nWhat is the thickness of the Styrofoam in feet?: ");
        scanf("%lf", &thickness);
        printf("What is the surface area of the Styrofoam wall in ft?: ");
        scanf("%lf", &surfaceArea);
        printf("What about the temperature of wall 1 in Fahrenheit?: ");
        scanf("%lf", &temp1);
        printf("What about the temperature of wall 2 in Fahrenheit?: ");
        scanf("%lf", &temp2);
        //Processing the Heat Transfer of Styrofoam with a k of 0.017
        heatTransfer = surfaceArea*((temp2 - temp1)/(thickness/0.017));
        printf("\nThe heat transfer rate of Styrofoam with a thickness of %lf, a surface area of %lf, and with temperatures of %lf and %lf is %lf BTU/hr.", thickness, surfaceArea, temp1, temp2, heatTransfer);
    }
    else if (choice == 4)
    {
        printf("\nYou chose the material Fiberglass.\n");
        printf("\nWhat is the thickness of the Fiberglass in feet?: ");
        scanf("%lf", &thickness);
        printf("What is the surface area of the Fiberglass wall in ft?: ");
        scanf("%lf", &surfaceArea);
        printf("What about the temperature of wall 1 in Fahrenheit?: ");
        scanf("%lf", &temp1);
        printf("What about the temperature of wall 2 in Fahrenheit?: ");
        scanf("%lf", &temp2);
        //Processing the Heat Transfer of Fiberglass with a k of 0.027
        heatTransfer = surfaceArea*((temp2 - temp1)/(thickness/0.027));
        printf("\nThe heat transfer rate of Fiberglass with a thickness of %lf, a surface area of %lf, and with temperatures of %lf and %lf is %lf BTU/hr.", thickness, surfaceArea, temp1, temp2, heatTransfer);
    }
    else if (choice == 5)
    {
        printf("\nYou chose the material Drywall.\n");
        printf("What is the thickness of the Drywall in feet?: ");
        scanf("%lf", &thickness);
        printf("What is the surface area of the Drywall wall in ft?: ");
        scanf("%lf", &surfaceArea);
        printf("What about the temperature of wall 1 in Fahrenheit?: ");
        scanf("%lf", &temp1);
        printf("What about the temperature of wall 2 in Fahrenheit?: ");
        scanf("%lf", &temp2);
        //Processing the Heat Transfer of Drywall with a k of 0.093
        heatTransfer = surfaceArea*((temp2 - temp1)/(thickness/0.093));
        printf("\nThe heat transfer rate of the Drywall with a thickness of %lf, a surface area of %lf, and with temperatures of %lf and %lf is %lf BTU/hr.", thickness, surfaceArea, temp1, temp2, heatTransfer);
    }
    else if (choice == 6)
    {
        printf("\nThank you! Bye bye %s.\n", userName);
    }
    
    //Output
    printf("\n\n\nThank you for using this program %s, bye bye.", userName);
    
    return 0;
}

【问题讨论】:

  • 必须 始终检查scanf返回的值。如果输入流包含“a”,int choice; scanf("%d", &amp;choice); if( choice ==1 ) 是未定义的行为,因为选择未初始化且 scanf 未分配给它。

标签: c loops while-loop


【解决方案1】:

您有两种选择来归档目标。下面的代码使用第一个。

  • while (true)/for(;;)break
  • do{} while();
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

int main() {
  // Variables
  char userName[50];
  int choice;          /*Material picked by the user*/
  double thickness;    /* "th" Thickness of the material in question*/
  double surfaceArea;  /* "a" Surface Area of the wall in ft^2*/
  double temp1;        /* "t1" Temperature of wall 1 in Fahrenheit*/
  double temp2;        /* "t2" Temperature of wall 2 in Fahrenheit*/
  double heatTransfer; /* "h" The heat transfer rate in BTU/hr*/
  /*Variable k, which is a constant, will be defined for each material in the
   * materials own processing*/
  while (true) {
    // Prompting for Name
    printf(
        "Hello, welcome to the Heat Trasnfer Program! Could you please input "
        "your name: "); /*prompting name of user*/
    scanf("%s", userName);
    printf(
        "\nHello %s, with what can I help you today? The menu is down "
        "below:\n\n",
        userName); /*prompting to get them ready for the menu.*/

    // Menu that's displayed on screen
    printf("Heat Transfer Menu:\n");
    printf("\tMaterials:\t\t \n");
    printf("\t1. Hardwood Siding\t\t \n");
    printf("\t2. Concrete Block\t\t\n");
    printf("\t3. Styrofoam\t\t\t \n");
    printf("\t4. Fiberglass\t\t\t \n");
    printf("\t5. Drywall\t\t\t \n");
    printf("\t6. Quit.\n\n");

    // Gathering Material that will be worked with
    printf("Please input your material choice number: ");
    scanf("%d", &choice);

    // Testing Choice to see if it's a valid input
    if (choice == 1) {
      printf("\nYou chose the material Hardwood Siding.\n"); /*Verifying that
                                                                the material is
                                                                correct*/
      printf("\nWhat is the thickness of the Hardwood Siding in feet?: ");
      scanf("%lf", &thickness);
      printf("What is the surface area of the Hardwood Siding wall in ft?: ");
      scanf("%lf", &surfaceArea);
      printf("What about the temperature of wall 1 in Fahrenheit?: ");
      scanf("%lf", &temp1);
      printf("What about the temperature of wall 2 in Fahrenheit?: ");
      scanf("%lf", &temp2);
      // Processing the Heat Transfer of Hardwood Siding with a k of 0.092
      heatTransfer = surfaceArea * ((temp2 - temp1) / (thickness / 0.092));
      printf("\nThe heat transfer rate of Hardwood Siding is %lf BTU/hr.",
             heatTransfer);
    } else if (choice == 2) {
      printf("\nYou chose the material Concrete Block.\n");
      printf("\nWhat is the thickness of the Concrete Block in feet?: ");
      scanf("%lf", &thickness);
      printf("What is the surface area of the Concrete Block wall in ft?: ");
      scanf("%lf", &surfaceArea);
      printf("What about the temperature of wall 1 in Fahrenheit?: ");
      scanf("%lf", &temp1);
      printf("What about the temperature of wall 2 in Fahrenheit?: ");
      scanf("%lf", &temp2);
      // Processing the Heat Transfer of Concrete Block with k 0.260
      heatTransfer = surfaceArea * ((temp2 - temp1) / (thickness / 0.260));
      printf(
          "\nThe heat transfer rate of Concrete Block with a thickness of %lf, "
          "a "
          "surface area of %lf, and with temperatures of %lf and %lf is %lf "
          "BTU/hr.",
          thickness, surfaceArea, temp1, temp2, heatTransfer);
    } else if (choice == 3) {
      printf("\nYou chose the material Styrofoam.\n");
      printf("\nWhat is the thickness of the Styrofoam in feet?: ");
      scanf("%lf", &thickness);
      printf("What is the surface area of the Styrofoam wall in ft?: ");
      scanf("%lf", &surfaceArea);
      printf("What about the temperature of wall 1 in Fahrenheit?: ");
      scanf("%lf", &temp1);
      printf("What about the temperature of wall 2 in Fahrenheit?: ");
      scanf("%lf", &temp2);
      // Processing the Heat Transfer of Styrofoam with a k of 0.017
      heatTransfer = surfaceArea * ((temp2 - temp1) / (thickness / 0.017));
      printf(
          "\nThe heat transfer rate of Styrofoam with a thickness of %lf, a "
          "surface area of %lf, and with temperatures of %lf and %lf is %lf "
          "BTU/hr.",
          thickness, surfaceArea, temp1, temp2, heatTransfer);
    } else if (choice == 4) {
      printf("\nYou chose the material Fiberglass.\n");
      printf("\nWhat is the thickness of the Fiberglass in feet?: ");
      scanf("%lf", &thickness);
      printf("What is the surface area of the Fiberglass wall in ft?: ");
      scanf("%lf", &surfaceArea);
      printf("What about the temperature of wall 1 in Fahrenheit?: ");
      scanf("%lf", &temp1);
      printf("What about the temperature of wall 2 in Fahrenheit?: ");
      scanf("%lf", &temp2);
      // Processing the Heat Transfer of Fiberglass with a k of 0.027
      heatTransfer = surfaceArea * ((temp2 - temp1) / (thickness / 0.027));
      printf(
          "\nThe heat transfer rate of Fiberglass with a thickness of %lf, a "
          "surface area of %lf, and with temperatures of %lf and %lf is %lf "
          "BTU/hr.",
          thickness, surfaceArea, temp1, temp2, heatTransfer);
    } else if (choice == 5) {
      printf("\nYou chose the material Drywall.\n");
      printf("What is the thickness of the Drywall in feet?: ");
      scanf("%lf", &thickness);
      printf("What is the surface area of the Drywall wall in ft?: ");
      scanf("%lf", &surfaceArea);
      printf("What about the temperature of wall 1 in Fahrenheit?: ");
      scanf("%lf", &temp1);
      printf("What about the temperature of wall 2 in Fahrenheit?: ");
      scanf("%lf", &temp2);
      // Processing the Heat Transfer of Drywall with a k of 0.093
      heatTransfer = surfaceArea * ((temp2 - temp1) / (thickness / 0.093));
      printf(
          "\nThe heat transfer rate of the Drywall with a thickness of %lf, a "
          "surface area of %lf, and with temperatures of %lf and %lf is %lf "
          "BTU/hr.",
          thickness, surfaceArea, temp1, temp2, heatTransfer);
    } else if (choice == 6) {
      printf("\nThank you! Bye bye %s.\n", userName);
      break;
    }
  }

  // Output
  printf("\n\n\nThank you for using this program %s, bye bye.\n", userName);
  return 0;
}

【讨论】:

    【解决方案2】:

    例如,您确实需要拆分代码并尝试确定可以组合在一起的内容,并尽量避免重复

    让我们首先像这样定义支持的材料

    // this is a simple map of choice index to name of material
    // 0 being not applicable and switch case can take care of 
    // boundary conditions allowing only 1 - 5 
    const char *materials[] =
    {
        "Not-applicable",
        "Hardwood Siding",
        "Concrete Block",
        "Styrofoam",
        "Fiberglass",
        "Drywall",
    };
    

    开始定义菜单

    // Just a menu .. and there are no read activity here
    // just a write/display
    void show_menu()
    {
        //Menu that's displayed on screen
        printf("Heat Transfer Menu:\n");
        printf("\tMaterials:\t\t \n");
        printf("\t1. Hardwood Siding\t\t \n");
        printf("\t2. Concrete Block\t\t\n");
        printf("\t3. Styrofoam\t\t\t \n");
        printf("\t4. Fiberglass\t\t\t \n");
        printf("\t5. Drywall\t\t\t \n");
        printf("\t6. Quit.\n\n");
    }
    

    生成一些方法,例如获取用户输入 take_choice 和用户名 get_user 来驱动此菜单

    int  take_choice()
    {
        int choice = -1;
        //Gathering Material that will be worked with
        printf("Please input your material choice number: ");
        scanf("%d", &choice);
        return choice;
    }
    
    
    
    void get_user( char *username )
    {
        //Prompting for Name
        printf("Hello, welcome to the Heat Trasnfer Program! Could you please input your name: "); /*prompting name of user*/
        scanf("%s",  username);
        printf("\nHello %s, with what can I help you today? The menu is down below:\n\n", username);/*prompting to get them ready for the menu.*/
    }
    

    我们几乎已经准备好了所有的东西,只是为了帮助获取有关特定产品线的详细信息,例如获得厚度、表面积等

    double get_thickness(int metarial)
    {
        double thickness = -1;
        printf("\nWhat is the thickness of the %s in feet?: ", materials[ metarial ]);
        scanf("%lf", &thickness);
        return thickness;
    }
    
    double get_surfaceArea(int metarial)
    {
        double surfaceArea = -1;
        printf("What is the surface area of the %s wall in ft?: ", materials[ metarial ]);
        scanf("%lf", &surfaceArea);
        return surfaceArea;
    }
    

    在这一点上,我们已经准备好处理最终工作流程的大部分部件,将部件粘在一起

    int main()
    {
        //Variables
        char userName [50];
        get_user(userName);
        
    
        int choice = -1;
        do
        {
            show_menu();
            choice = take_choice();
            switch ( choice )
            {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            {
                double thickness = get_thickness( choice );
                double surfaceArea = get_surfaceArea( choice );
                if( thickness > 0 && surfaceArea > 0 )
                {
                    double temp1;       /* "t1" Temperature of wall 1 in Fahrenheit*/
                    printf("What about the temperature of wall 1 in Fahrenheit?: ");
                    scanf("%lf", &temp1);
    
                    double temp2;       /* "t2" Temperature of wall 2 in Fahrenheit*/
                    printf("What about the temperature of wall 2 in Fahrenheit?: ");
                    scanf("%lf", &temp2);
    
    
                    //Processing the Heat Transfer of Hardwood Siding with a k of 0.092
                    double heatTransfer = surfaceArea * ((temp2 - temp1) / (thickness / 0.092));
                    printf("\nThe heat transfer rate of %s is %lf BTU/hr.\n", materials[choice], heatTransfer);
                }
                else
                {
                    printf("invalid input try again .. ");
                }
    
            }
            break;
            case 6:
                printf("\nThank you! Bye bye %s.\n", userName);
                break;
            default:
                printf("\nWrong choice try again\n");
            }
        }
        while ( choice != 6 );
    
    
        //Output
        printf("\n\n\nThank you for using this program %s, bye bye.", userName);
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 2017-10-16
      • 2020-03-07
      • 2015-03-26
      • 2015-01-04
      • 2014-10-14
      • 2014-03-28
      • 1970-01-01
      相关资源
      最近更新 更多