简体   繁体   English

我的程序无限循环,我老实说不清楚为什么

[英]My program is looping infinitely, and I'm honestly not sure why

So the problem here was to write a program that uses pointers to point to functions and write it in such a way that it collects 10 doubles, gives feedback to the user of the program, sorts them and print the sorted results as proof. 所以这里的问题是编写一个程序,使用指针指向函数并以这样的方式编写它,它收集10个双打,向程序用户提供反馈,对它们进行排序并打印排序结果作为证据。 The problem is, the program either prints the printf statement in the beginning infinitely, or collects numbers infinitely. 问题是,程序要么无限地打印printf语句,要么无限地收集数字。

Here is some code 这是一些代码

#include <stdio.h>


void func1(double x);
void below_five(void);
void above_five(void);
void other(void);
void sort(double *p[], int n);
void print_doubles(double *p[], int n);

int main(void){

  double *numbers[9];
  int nbr;
  printf("\nEnter 10 doubles that are less than 5 or greater than 5, type 0 to exit");
  for(int i = 0; i < 10 ; i++)
  {

      scanf("%d", &nbr);
      func1(nbr);
      numbers[i] = nbr;

      if(nbr == 0)
        break;


  }
  sort(numbers, 10);
  print_doubles(numbers, 10);
  return 0;
}

void func1(double val)
{
  double (*ptr)(void);

  if(val <= 5.00){
    ptr = below_five;
  }else if((val > 5.00) && (val <= 10.00)){
    ptr = above_five;
  }else
    ptr = other;
}

void below_five(void){
  puts("You entered a number below or equal to five");
}

void above_five(void){
  puts("You entered a number above five");
}

void other(void){
  puts("You entered a number well above five.");
}

void sort(double *p[], int n)
{
double *tmp;
for(int i = 0; i < n; i++)
{
  if(p[i] > p[i+1]){
  tmp = p[i];
  p[i] = p[i+1];
  p[i + 1] = tmp;
}
}
}

void print_doubles(double *p[], int n)
{
  int count;
  for(count = 0; count < n; count++)
    printf("%d\n", p[count]);

}

Like I said, what I expect it to be able to do is collect doubles into the scanf method and then print the numbers after sorting them, but it seems the for loop collects doubles forever without end in this case. 就像我说的那样,我希望它能够做的是将双打收集到scanf方法中,然后在排序后打印数字,但似乎for循环在这种情况下永远收集双打而没有结束。

What have I done wrong, exactly? 我究竟做错了什么?

See my comments in your updated code. 更新的代码中查看我的评论。 There are other modifications required, but the minimum updates needed to work your code is below 还需要进行其他修改,但下面是处理代码所需的最低更新

#include <stdio.h>


void func1(double x);
void below_five(void);
void above_five(void);
void other(void);
    void sort(double p[], int n);  /* Simply use a array notation, 
arrays passed to functions decays to pointer */
    void print_doubles(double p[], int n); /* Simply use a array notation,
 arrays passed to functions decays to pointer */

int main(void){

  double numbers[10];
  double nbr; // Change type to double, as you're reading doubles
  printf("\nEnter 10 doubles that are less than 
           5 or greater than 5, type 0 to exit\n");
  for(int i = 0; i < 10 ; i++)
  {

      scanf("%lf", &nbr); // Use correct format specifier to read doubles
      func1(nbr);
      numbers[i] = nbr;

      if(nbr == 0)
        break;


  }
  sort(numbers, 10);
  print_doubles(numbers, 10);
  return 0;
}

void func1(double val)
{
  double (*ptr)(void);

  if(val <= 5.00){
    ptr = below_five;
  }else if((val > 5.00) && (val <= 10.00)){
    ptr = above_five;
  }else
    ptr = other;
  /* Why you set the pointer to function if you don't call it, 
     so call it here*/    
        (*ptr)();  
    }

void below_five(void){
  puts("You entered a number below or equal to five");
}

void above_five(void){
  puts("You entered a number above five");
}

void other(void){
  puts("You entered a number well above five.");
}

void sort(double p[], int n)  /* Your sorting routine is wrong ,
   see the modified code */
{
double tmp;
    for(int j = 0; j < n-1; j++)
    for(int i = 0; i < n-j-1; i++)
{
  if(p[i] > p[i+1]){
  tmp = p[i];
  p[i] = p[i+1];
  p[i + 1] = tmp;
}
}
}

void print_doubles(double p[], int n)
{
  int count;
  for(count = 0; count < n; count++)
    printf("%lf\n", p[count]); // Use correct format specifier

}

Demo Here 在这里演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的光标无限循环到最后一行 - Why is my cursor infinitely looping on the last row 学校课程测试无法正确覆盖我的代码,我不确定为什么 - School program tests not covering my code correctly and I'm not sure why 我的程序有 AddressSanitizer 错误,但我不知道如何阅读 - My program has AddressSanitizer error but I'm not sure how to read it 我不确定为什么该程序无法编译 - I'm not sure why this program won't compile 我不确定为什么我的方差 function 返回 0.0000 0.0000 - I'm not sure why my variance function returns 0.0000 0.0000 为什么我的链表程序会无限地打印出我输入的那个“第一个数字”的循环? - Why does my linked-list program print out a loop of that "first number" I entered, infinitely? 为什么这个for循环无限循环? (C) - Why is this for loop infinitely looping? (C) 我的程序停止循环,我不知道为什么 - My program stops looping and i cannot figure out why 我不确定为什么我的嵌套 while 循环在第一次迭代后停止 - I'm not sure why my nested while loop is stopping after the first iteration 我不确定为什么我在 C 中的代码会免费给我一个分段错误,有什么想法吗? - I'm not sure why my code in C is giving me a segmentation fault at free, any ideas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM