2017-06-23 13 views
-1

私はあなたが選択した2つの都市(3つの都市があり、2つを選択できます)間の距離を示すプログラムを作成したいと思います。 。私は初心者です。strcmpについて聞いたことがありますが、それを行う別の方法がありますか?文字列の比較が失敗しました

#include <stdio.h> 
#include <stdlib.h> 
#include <ctype.h> 
#include <string.h> 
#include <math.h> 



int main() 
{ 
char city1[] = "Zurich"; 
char city2[] = "Vienna"; 
char city3[] = "Berlin"; 
char location[20]; 
char destination[20]; 

puts("Which city are you in at the moment?"); 
gets(location); 
puts("Which city do you want to go to?"); 
gets(destination); 

if(location == city1) 
{ 
    if(destination == city2) 
    { 
     printf("Distance between %s and %s is 743km and it will take you 7 hours and 40 minutes to get there", city1, city2); 
    } 
    else 
    { 
     printf("Distance between %s and %s is 844km and it will take you 8 hours and 40 minutes to get there", city1, city3); 
    } 

} 
else if(location == city2) 
{ 
    if(destination == city3) 
    { 
     printf("Distance between %s and %s is 640km and it will take you 7 hours and 15 minutes to get there", city2, city3); 
    } 
    else 
    { 
     printf("Distance between %s and %s is 743km and it will take you 7 hours and 40 minutes to get there", city2, city1); 
    } 



} 





return 0; 
} 
+0

'gets()' !!!!あなた自身にある程度の慈悲を与えてください。もし誰かが** Gschlachtenbretzingen **市から来たら? – Gaurav

+0

@GauravPathakよく私の "練習"のポイントは、Cのプログラミングでnoobのようなまだ私の "練習"を使用していたと私はその悪い(しかし、私はなぜ知っていない)を聞いてタフな取得初心者の練習のために私はちょうどバグはありません:D –

+0

ハッハッハ...大丈夫です。私はちょうど冗談だった... – Gaurav

答えて

1

location == city1が等しくならないポインタアドレスを、比較している:ここではコードです。

!strcmp(location, city1) & cを使用する必要があります。代わりに。 strcmpは、文字列の内容がの場合は0を返します。

関連する問題