简体   繁体   English

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

[英]C Program hangs after trying to call function from main

This is the code:这是代码:

#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]);
  }
}

When I run the program it prints:当我运行程序时,它会打印:

main1
main2

Then the program hangs.然后程序挂起。 I would at least expect it to call ReadCities() and at get as far as printing我至少希望它调用ReadCities()并尽可能打印

ReadCities1
ReadCities2

but no matter what I try it just hangs with a flashing cursor in the terminal after printing main2 .但无论我尝试什么,它都会在打印main2后在终端中挂起一个闪烁的光标。 There might also be other mistakes in the code but I can't even get far enough to test it out!代码中可能还有其他错误,但我什至无法进行足够的测试!

Sorry if it's something obvious, i'm new to programming!对不起,如果这是显而易见的,我是编程新手! Cheers!干杯!

Not really sure what this line:不太确定这一行是什么:

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

Is returning, seems this loop might not be breaking.正在返回,似乎此循环可能不会中断。 In a situation like this I'd printf() the value ofo XCoord(city[i]) within my loop to see what value is actually in there...在这种情况下,我会在循环中 printf() 的值 ofo XCoord(city[i]) 以查看其中实际存在的值...

Using printf()'s can be quite useful for things like this.使用 printf() 对这样的事情非常有用。

Also, as someone said there's a function prototype declaration in main:另外,正如有人所说,main 中有一个函数原型声明:

double TourLength(lineSeg_t cityLines[]);双 TourLength(lineSeg_t cityLines[]);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM