我的代码在while语句之后没有执行printf语句

时间:2015-03-26 01:35:47

标签: c

给出两个整数来表示一系列数字和两个数字的起始值和结束值 表示范围内值的潜在因子的附加整数,通过添加每个来创建总计 m的范围内的整数 正好一个因子的倍数并减去每个整数 范围是两个因素的倍数。

这是我的代码。我正在尝试运行它。它编译并接收所有信息但不打印总数!请帮助!

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

        //Factor Declarations
        int getStart(); //recieves starting range value from user
        int getEnd(int start); //recieves ending range value from user
        int getFirst(); //recieves first factor from user
        int getSecond(int *first); //recieves second factor from user


        int main (void)
        {
          //Local Declarations
          int start; //starting range
          int end; //ending range
          int first; //first factor
          int second; // second factor
          int x;
          int y;
          int total = 0;

          //Executable Statements
          start = getStart();
          end = getEnd(start);
          first = getFirst();
          second = getSecond(&first);
          x = start;
          y = first * second;

          while (x <= end)
          {  if ((x % first == 0 || x % second == 0) && (x % y != 0))
            {
            total += x;}
          else if (x % y == 0)
               total -= x;

     x++;}

        printf("\nTotal: %d", total);
        return 0;
    }


    /******************************************************************************
     *  * Function:    getStart
     *   * Description: Gets user input for starting range, ensures good value
     *    * Parameters:
     *     * Return:      num, ending value
     *      ******************************************************************************/
    int getStart()
    {
      int num;

      do
      {
        printf("\nEnter starting range value: ");
        scanf("%d", &num);

        if(num <= 0)
        {
          printf("Error! Starting value must be greater than zero!\n");
        }
      } while(num <= 0);

      return num;
    }

/******************************************************************************
 *  * Function:    getEnd
 *   * Description: Gets ending range value from user, ensures value is lager than starting value
 *    * Parameters:  start, int, starting value
 *     * Return:      num, ending value
 *      ******************************************************************************/
int getEnd(int start)
{
  int num;

  do
  {
    printf("Enter ending range value: ");
    scanf("%d", &num);

    if(num <= start)
    {
      printf("Error! Ending value must be greater than the starting value!\n");
    }
  } while(num <= start);

  return num;
}

/******************************************************************************
 *  * Function:    <function name>
 *   * Description: <brief description of what the function does>
 *    * Parameters:  <variable1 name, data type, and description>
 *     *              <variable2 name, data type, and description>
 *      * Return:      <data type and description>
 *       ******************************************************************************/
int getFirst()
{
  int num;

  do
  {
    printf("Enter first factor: ");
    scanf("%d", &num);

    if(num <= 0)
    {
      printf("Error! Factors must be positive!\n");
    }
  } while(num <= 0);

  return num;
}
/******************************************************************************
 * * Function:    <function name>
 * * Description: <brief description of what the function does>
 * * Parameters:  <variable1 name, data type, and description>
 * *              <variable2 name, data type, and description>
 * * Return:      <data type and description>
 * ******************************************************************************/
int getSecond(int *first)
{
  int num;

  do
  {
    printf("Enter second factor: ");
    scanf("%d", &num);

    if(num <= 0)
    {
      printf("Error! Factors must be positive!\n");
    }

    if(num == *first)
    {
      printf("Error! The values of the two multiples cannot be the same!\n");
      *first = getFirst();
      num = getSecond(first);
    }
  } while(num <= 0 || num == *first);

  return num;
}

1 个答案:

答案 0 :(得分:4)

当然没有,因为无法在代码中结束while()循环,所以不要在循环内更改xend

也许你的意思是

while (total <= end)