distance calculating program

i have to make a program which calculate disstance b/w 2 cities i try my best here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string.h>
using namespace std;
string response;
int main ()
{
    int array[4][4] = {
    {   0,  100, 180, 109 },
    {  100,   0, 101, 192 },
    {  173, 101, 0, 255  },
    {  103, 191, 255, 0  },
    };


char *cities[]={"city1",
                  "city2",
                  "city3",
                  "city4"
             }; 
             
  
                   
cout <<"\t city1 and city2" <<endl;
cout <<"\t city2 and city3"<<endl;
cout <<"\t city3 and city4" <<endl;
cout <<"\t city4 and city1" <<endl;

int i;
string response;
cout <<"\t Select 2 cities:" <<endl;
getline (cin,response);



if (response  == "city1 and city2") {
    cout << " Total Distance = " ;
    cout<<array[0][1]<<endl;
}
else if (response == "city2 and city3")
{
    cout << " Total Distance = " ;
    cout<<array[0][2]<<endl;
}
else if (response == "city3 and city4"){
    cout << " Total Distance = " ;
    cout<<array[0][3]<<endl;
}
else if (response == "city4 and city1"){
    cout << " Total Distance = " ;
    cout<<array[0][4]<<endl;
}
else
{
 cout <<"Incorrect!";

}
system ("pause");
}


but this one is not working fine i mean if user only enter city1 it shows output but as long as if user enter a space like city1 and city2 it shows incorrect.

i ask to teacher he was saying to use stricmp like
char *cities={"city1" , 0
"city2", 1
...
... }

then
distance stricmp=cities[i]

but i m not able to use please help.
What if the user wants to know the distance between city2 and city4?
Maybe you should not use string to get the answer?
You can use int, for example:
1
2
3
4
5
6
7
8
9
int answer;
cout<<"What apple you want, green(1) or red(2) ?"<<endl;
cin>>answer;
if(answer == 1)
   cout<<"Your choice is green apple"<<endl;
else if(answer == 2)
   cout<<"Your choice is red apple"<<endl;
else
   cout<<"Incorrect!";

Last edited on
@ zipuch i have to use stricmp to compare two strings

@ helios i don't have any idea :(
Topic archived. No new replies allowed.