Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在这个C代码中在哪里添加循环?_C_While Loop - Fatal编程技术网

在这个C代码中在哪里添加循环?

在这个C代码中在哪里添加循环?,c,while-loop,C,While Loop,我有一个任务是在C语言中创建一个程序,其中用户必须插入3个整数,并根据输入,为用户提供一种三角形(只有在其有效时),它将使用这些值创建 代码本身是有效的,我已经设法插入了一些循环来测试条件,并使其更加健壮 但是,我想为用户提供一个选项,以重试不同的结果或只是关闭程序。我有一种感觉,我必须在循环时添加另一个,但是我不确定在哪里以及如何使程序以这种方式工作 在理想情况下,循环将替换结尾处的输出,或者在用户根据输入获取无效三角形的情况下。当这些情况发生时,我希望它能让用户选择重试或干脆退出程序 请参阅

我有一个任务是在C语言中创建一个程序,其中用户必须插入3个整数,并根据输入,为用户提供一种三角形(只有在其有效时),它将使用这些值创建

代码本身是有效的,我已经设法插入了一些循环来测试条件,并使其更加健壮

但是,我想为用户提供一个选项,以重试不同的结果或只是关闭程序。我有一种感觉,我必须在循环时添加另一个
,但是我不确定在哪里以及如何使程序以这种方式工作

在理想情况下,循环将替换结尾处的输出,或者在用户根据输入获取无效三角形的情况下。当这些情况发生时,我希望它能让用户选择重试或干脆退出程序

请参阅我的代码:

#include <stdio.h>
int main()
{
    /*** Declaring triangle variable sides ****/

  float sideA;
  float sideB;
  float sideC;
  char ch;

  printf("Lets explore triangles! Please insert a value for side 'A' of your triangle:");
  while(scanf("%f", &sideA) != 1)
  {
    printf("You inserted an incorrect value. Please insert a number for side 'A' of your triangle:");
    while ( (ch=getchar()) != '\n' );
  }

  printf(" Now insert a value for side 'B' of your triangle:");
  while(scanf("%f", &sideB) != 1 )
  {
    printf("You inserted an incorrect value. Please insert a number for side 'B' of your triangle:");
    while ( (ch=getchar()) != '\n' );
  }

  printf(" And finally, insert a value for side 'C' of your triangle:");
  while(scanf("%f", &sideC) != 1 )
  {
    printf("You inserted an incorrect value. Please insert a number for side 'C' of your triangle:");
    while ( (ch=getchar()) != '\n' );
  }

  /*** List of conditions based on user input to identify if the triangle is valid and if so, what type of triangle they get***/

  if(sideA <=0 || sideB<=0 || sideC <=0)
  {
      printf("YOUR TRIANGLE IS 'INVALID'.\n\n");
      printf("REASON: You cannot have a triangle with any side having a value of 0.\n");
      printf("Please exit the program and restart it to try again.\n");
  }
  else
      if( (sideA+sideB<sideC) || (sideB+sideC<sideA) || (sideC+sideA<sideB) )
      {
          printf("YOUR TRIANGLE IS 'INVALID'.\n\n");
          printf("REASON: The sum of every pair of sides must be greater than the third side of a triangle.!\n");
          printf("Please exit the program and restart it to try again.\n");
      }
      else
           if( (sideA==sideC && sideB==sideC) || (sideB==sideA && sideC==sideA) || (sideC==sideB && sideA==sideB) ) /*** Code to determine EQUILATERAL TRIANGLE***/
           {
              printf("YOUR TRIANGLE IS 'VALID'.\n");
              printf(" Your input creates a valid EQUILATERAL triangle.\n");
           }
           else 
               if( (sideA == sideB ) || (sideB == sideC ) || (sideC == sideA ) )/*** Code to determine ISOSCELES TRIANGLE***/
               {
                   printf("YOUR TRIANGLE IS 'VALID'.\n");
                   printf("Your input creates a valid ISOSCELES triangle.\n");
               }
               else
                   if( (sideA!= sideB) && (sideB != sideC) )/*** Code to determine SCALENE triangle ***/
                   {
                       printf("YOUR TRIANGLE IS 'VALID'.\n");
                       printf("Your input creates a valid SCALENE triangle.\n");
                   }
                   else
                   {
                       printf("You have inserted invalid range of values, as a result your triangle is invalid.\n");
                       printf("Please exit the program and restart it to try again.\n");
                       printf("Goodbye.\n");
                   }
return(0);
}
#包括
int main()
{
/***声明三角形可变边****/
浮雕;
浮动边b;
浮点数c;
char ch;
printf(“让我们探索三角形!请为三角形的“a”边插入一个值:”;
while(scanf(“%f”,&sideA)!=1)
{
printf(“您插入的值不正确。请为三角形的a边插入一个数字:”;
而((ch=getchar())!='\n');
}
printf(“现在为三角形的“B”边插入一个值:”);
while(scanf(“%f”,&sideB)!=1)
{
printf(“您插入的值不正确。请为三角形的“B”边插入一个数字:”;
而((ch=getchar())!='\n');
}
printf(“最后,为三角形的‘C’边插入一个值:”;
while(scanf(“%f”,&sideC)!=1)
{
printf(“您插入的值不正确。请为三角形的“C”侧插入一个数字:”;
而((ch=getchar())!='\n');
}
/***基于用户输入的条件列表,用于确定三角形是否有效,如果有效,则确定三角形的类型***/

if(sideA将所有代码放在
main()
中的一个单独函数中,如
handleontriangle()

然后在main中放入以下循环(伪代码):


这可以通过do-while循环轻松实现

试试这个算法

char a;
do
  {
    /*  Your code */
    printf(" Do you want to do more ( Y/N ) " );
    scanf( " %c",a );
  } while( a == 'Y' || a == 'y' );
下面是在代码中实现的代码

#include <string.h>
#include <stdio.h>
int main()
{
    /*** Declaring triangle variable sides ****/

  float sideA;
  float sideB;
  float sideC;
  char a;

  do 
   {

      printf("Lets explore triangles! Please insert a value for side 'A' of your triangle:");
      while(scanf("%f", &sideA) != 1)
       {
         printf("You inserted an incorrect value. Please insert a number for side 'A' of your triangle:");
         while ( (getchar()) != '\n' );
       }

      printf(" Now insert a value for side 'B' of your triangle:");
      while(scanf("%f", &sideB) != 1 )
       {
         printf("You inserted an incorrect value. Please insert a number for side 'B' of your triangle:");
         while ( (getchar()) != '\n' );
       }

      printf(" And finally, insert a value for side 'C' of your triangle:");
      while(scanf("%f", &sideC) != 1 )
       {
         printf("You inserted an incorrect value. Please insert a number for side 'C' of your triangle:");
         while ( (getchar()) != '\n' );
       }

      /*** List of conditions based on user input to identify if the triangle is valid and if so, what type of triangle they get***/

      if(sideA <=0 || sideB<=0 || sideC <=0)
       {
           printf("YOUR TRIANGLE IS 'INVALID'.\n\n");
           printf("REASON: You cannot have a triangle with any side having a value of 0.\n");
       }
      else if( (sideA+sideB<sideC) || (sideB+sideC<sideA) || (sideC+sideA<sideB) )
       {
         printf("YOUR TRIANGLE IS 'INVALID'.\n\n");
         printf("REASON: The sum of every pair of sides must be greater than the third side of a triangle.!\n");
       }
      else if( (sideA==sideC && sideB==sideC) || (sideB==sideA && sideC==sideA) || (sideC==sideB && sideA==sideB) ) /*** Code to determine EQUILATERAL TRIANGLE***/
       {
         printf("YOUR TRIANGLE IS 'VALID'.\n");
         printf(" Your input creates a valid EQUILATERAL triangle.\n");
       }
      else if( (sideA == sideB ) || (sideB == sideC ) || (sideC == sideA ) )/*** Code to determine ISOSCELES TRIANGLE***/
       {
         printf("YOUR TRIANGLE IS 'VALID'.\n");
         printf("Your input creates a valid ISOSCELES triangle.\n");
       }
      else if( (sideA!= sideB) && (sideB != sideC) )/*** Code to determine SCALENE triangle ***/
       {
         printf("YOUR TRIANGLE IS 'VALID'.\n");
         printf("Your input creates a valid SCALENE triangle.\n");
       }
      else
       {
         printf("You have inserted invalid range of values, as a result your triangle is invalid.\n");
       }
      printf("Do you want to try again ( Y/N  )");
      scanf(" %c",&a);

   }while( a=='Y' || a=='y'  );

  return(0);
}
#包括
#包括
int main()
{
/***声明三角形可变边****/
浮雕;
浮动边b;
浮点数c;
字符a;
做
{
printf(“让我们探索三角形!请为三角形的“a”边插入一个值:”;
while(scanf(“%f”,&sideA)!=1)
{
printf(“您插入的值不正确。请为三角形的a边插入一个数字:”;
而((getchar())!='\n');
}
printf(“现在为三角形的“B”边插入一个值:”);
while(scanf(“%f”,&sideB)!=1)
{
printf(“您插入的值不正确。请为三角形的“B”边插入一个数字:”;
而((getchar())!='\n');
}
printf(“最后,为三角形的‘C’边插入一个值:”;
while(scanf(“%f”,&sideC)!=1)
{
printf(“您插入的值不正确。请为三角形的“C”侧插入一个数字:”;
而((getchar())!='\n');
}
/***基于用户输入的条件列表,用于确定三角形是否有效,如果有效,则确定三角形的类型***/

如果(sideA)您可以在要重复的部分周围添加一个循环。理想情况下,用户插入一组无效的值,导致程序向用户输出消息。它就在该消息之后。我需要键入什么来执行此操作。它是否与我已经做过的类似?一些一般观察:(1)赋值要求您接受三个整数。为什么您的程序使用
float
值?(2)始终避免代码中的重复。编写一个函数,从用户输入边长并返回该值,然后调用它三次。(例如,
sideA=get_-side('a');sideB=get_-side('B');sideC=get_-side('C');
(3)如果将
else
语句与前面的
if
语句保持一致,则代码的可读性将更高。(4)你用来测试等边三角形的逻辑可以简化很多。我让你自己来解决这个问题。我使用浮点整数,因为对于初学者来说,三角形可以有带小数的长度,这是为了稳健的测试。(2)分配给我这个任务的人建议我这样做。(3)从我所看到的情况来看,if-else是一致的。但是我可能看到了一些错误。(4)谢谢,我确信它可以简化。我正在努力改进它,一旦它变得尽可能强大,即使在它说“你的三角形“无效”的情况下,我也会继续改进它"原因:每对边的总和必须大于三角形的第三条边?就像在它这样说之后,它会要求用户重复这个问题?它仍然适用于这个问题吗?或者我必须在我希望它重复的每个案例上添加一个
,而
循环吗?@Shivarn,是和否,你不必再添加一个循环,只要尝试一下就可以了hanks Arun,它的工作方式完全符合我的要求。然而,当我编译程序时,我收到一条警告:“W8004第79行:'ch'被分配了一个从未在函数main中使用过的值……这是正常的吗?考虑到上次我们讨论while循环时,您使用ch来阻止用户插入除整数以外的任何内容。您可以删除
ch
。此代码不需要它。
ch
仅在您需要检查EOF时才需要,我没有将其添加到此代码中。我将对其进行编辑。这非常有效。@Arun A.S非常感谢您的帮助。我想问您最后一件事。如果您运行此程序,并且当它要求您插入某些内容时,您可以不插入任何内容并按enter键,程序只会转到一个新行。我们将如何处理
#include <string.h>
#include <stdio.h>
int main()
{
    /*** Declaring triangle variable sides ****/

  float sideA;
  float sideB;
  float sideC;
  char a;

  do 
   {

      printf("Lets explore triangles! Please insert a value for side 'A' of your triangle:");
      while(scanf("%f", &sideA) != 1)
       {
         printf("You inserted an incorrect value. Please insert a number for side 'A' of your triangle:");
         while ( (getchar()) != '\n' );
       }

      printf(" Now insert a value for side 'B' of your triangle:");
      while(scanf("%f", &sideB) != 1 )
       {
         printf("You inserted an incorrect value. Please insert a number for side 'B' of your triangle:");
         while ( (getchar()) != '\n' );
       }

      printf(" And finally, insert a value for side 'C' of your triangle:");
      while(scanf("%f", &sideC) != 1 )
       {
         printf("You inserted an incorrect value. Please insert a number for side 'C' of your triangle:");
         while ( (getchar()) != '\n' );
       }

      /*** List of conditions based on user input to identify if the triangle is valid and if so, what type of triangle they get***/

      if(sideA <=0 || sideB<=0 || sideC <=0)
       {
           printf("YOUR TRIANGLE IS 'INVALID'.\n\n");
           printf("REASON: You cannot have a triangle with any side having a value of 0.\n");
       }
      else if( (sideA+sideB<sideC) || (sideB+sideC<sideA) || (sideC+sideA<sideB) )
       {
         printf("YOUR TRIANGLE IS 'INVALID'.\n\n");
         printf("REASON: The sum of every pair of sides must be greater than the third side of a triangle.!\n");
       }
      else if( (sideA==sideC && sideB==sideC) || (sideB==sideA && sideC==sideA) || (sideC==sideB && sideA==sideB) ) /*** Code to determine EQUILATERAL TRIANGLE***/
       {
         printf("YOUR TRIANGLE IS 'VALID'.\n");
         printf(" Your input creates a valid EQUILATERAL triangle.\n");
       }
      else if( (sideA == sideB ) || (sideB == sideC ) || (sideC == sideA ) )/*** Code to determine ISOSCELES TRIANGLE***/
       {
         printf("YOUR TRIANGLE IS 'VALID'.\n");
         printf("Your input creates a valid ISOSCELES triangle.\n");
       }
      else if( (sideA!= sideB) && (sideB != sideC) )/*** Code to determine SCALENE triangle ***/
       {
         printf("YOUR TRIANGLE IS 'VALID'.\n");
         printf("Your input creates a valid SCALENE triangle.\n");
       }
      else
       {
         printf("You have inserted invalid range of values, as a result your triangle is invalid.\n");
       }
      printf("Do you want to try again ( Y/N  )");
      scanf(" %c",&a);

   }while( a=='Y' || a=='y'  );

  return(0);
}