2012-03-15 7 views
0

プログラムはビルドされますが、プロンプトは無限ループに詰まったようにハングアップします。このCプログラムで何が問題になりますか? (scanfを使用して端末からスキャンする)

最初のprintf文は実行されません。

プログラムのアイデアは、MMSI、名前、位置、コース、スピードをとり、ファイルに書き込むための構造体に配置することです。

int main(int argc, char** argv) { 

    ship *current_ship; 

    current_ship = getShipInfo(); 
    //writeShip(current_ship); 

    return (EXIT_SUCCESS); 
} 
ship * getShipInfo() { 
    ship *current_ship; 
    current_ship = malloc(sizeof(ship)); 
    int MMSI, course; 
    char name[51]; 
    float lat, lon, speed; 

    printf("Enter MMSI (9 digits):\n"); 
    scanf(" %9d", &MMSI); 

    printf("Enter ship name (upto 50 characters):\n"); 
    scanf(" %51s", name); 

    printf("Enter ship latitude (real number with upto 3 decimal places):\n"); 
    scanf(" %f", &lat); 

    printf("Enter ship longitude (real number with upto 3 decimal places):\n"); 
    scanf(" %f", &lon); 

    printf("Enter course made good (degrees from true north):\n"); 
    scanf(" %3d", &course); 

    printf("Enter speed over the ground (in knots with exactly one decimal place):\n"); 
    scanf(" %f", &speed); 

    current_ship->MMSI = MMSI; 
    strcpy(current_ship->name, name); 
    current_ship->lat = lat; 
    current_ship->lon = lon; 
    current_ship->course = course; 
    current_ship->speed = speed; 

    return current_ship; 
} 
+1

'stdout'がフラッシュされないようにする特別なモードの端末ですか? 'printf()'の後に 'fflush(stdout)'を挿入してみてください。 –

+0

デバッグの方法は何ですか?たとえば、 'getShipInfo()'の最初のステートメントとして 'NULL NULL; return 'を実行した場合、プログラムはそのコースを実行しますか?そういうもの。 –

+1

船体構造の宣言と関数宣言などで完全なコードを投稿する方が良い – Jay

答えて

0

ship * getShipInfo()はmain()関数の前に宣言されていますか? これは問題となる可能性があります。

+0

ヘッダファイルに宣言されていますが、それは問題ではありません –

+0

Hm、これは完全なプログラムではありませんか? –

1

ship-> nameを割り当てましたか?

current_ship->name = malloc(51);

0

あなたの問題はscanfの書式文字列の先頭のスペースです。より大きな問題はscanfをまったく使用していることです。代わりにfgetsを使用してください。

関連する問題