C程序在尝试从main调用函数后挂起

时间:2015-11-16 19:51:23

标签: c function call hang

这是代码:

#include <stdlib.h>
#include <stdio.h>
#include "descartes.h"

/*
 *  Eulidean travelling salesman
 */

#define MAXCITIES 100
#define FALSE 0
#define TRUE 1

point_t city [MAXCITIES];
int numCities = 0;

int ReadCities(void);
double TourLength(lineSeg_t cityLines[]);
void DrawTour(void);

int main(void) {
  printf ("main1\n");    
  OpenGraphics();    
  printf ("main2\n");
  ReadCities();    
  DrawTour();    
  double TourLength(lineSeg_t cityLines[]);    
  CloseGraphics();
  return EXIT_SUCCESS;
}


int ReadCities(void) {
  printf ("ReadCities1");
  int i = 1;

  printf ("ReadCities2");

  city[0] = GetPoint();
  while ((XCoord(city[i])) >= 0) {
    city[i] = GetPoint();
    printf ("(%d, %d)", XCoord(city[i]), YCoord(city[i]));
    numCities++;
    i++;
  }

  if (numCities <= MAXCITIES) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}


double TourLength(lineSeg_t cityLines[]) {
  double totLen = 0;
  int i;

  for (i = 0; i < (numCities - 1); i++) {
    totLen += Length(cityLines[i]);
  }

  return totLen;
}


void DrawTour(void) {
  lineSeg_t cityLines[MAXCITIES];
  int i;

  for (i = 0; i < (numCities - 1); i++) {
    cityLines[i] = LineSeg(city[i], city[i + 1]);
    DrawLineSeg(cityLines[i]);
  }
}

当我运行该程序时,它会打印出来:

main1
main2

然后程序挂起。我至少期望它可以打电话给ReadCities()并打印到

ReadCities1
ReadCities2

但无论我尝试什么,只需在打印main2后挂在终端上闪烁的光标。代码中可能还有其他错误,但我甚至无法测试它!

对不起,如果它显而易见,我是编程新手! 干杯!

1 个答案:

答案 0 :(得分:0)

不确定这一行是什么:

while ((XCoord(city[i])) >= 0)

回来了,似乎这个循环可能没有破坏。在这样的情况下,我在我的循环中printf()XCoord(city [i])的值,看看那里有什么值...

使用printf()&#39; s对于这样的事情非常有用。

另外,正如有人说主要的那样是一个函数原型声明:

double TourLength(lineSeg_t cityLines []);