Finding the closest city problem. c++

Hello, everybody. I am new to C++ and I'm struggling with a question.
The question is about find the closest city to the given coordinates (x=41.404974; y=19.705841;)
The user will input 3 cities and 3 coordinates, and the output should be only the city which is closer.
this is my code using for loop. and this is the test case it fails:
-------- input.txt ----------
Rome 42.001544 12.420588
Stuttgart 48.804066 9.195834
Istanbul 41.039117 28.848111

---------- output.txt ----------
Istanbul
---------- pattern.txt ----------
Stuttgart
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
  
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double x=41.404974;
    double y=19.705841;
    string city1,city;
    double x1,y1;
    double d1,minn=d1;
    int i,j;
    for (i=0;i<3;i++)
    {
        cin>>city1>>x1>>y1;
       double dx=x1-x;
    double dy=y1-y;
    d1=sqrt(pow(dx,2)+pow(dy,2));
    minn=d1;
    if (d1<=minn){
        minn=d1;
        city=city1;
    }
    }
    cout<<city;
    return 0;
}

Also i tried it in this way:
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
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double x=41.404974;
    double y=19.705841;
    string city1,city,city2,city3;
    double x1,y1,x2,x3,y2,y3,d2,d3;
    double d1,minn;
    int i,j;
    cin>>city1>>x1>>y1;
    cin>>city2>>x2>>y2;
    cin>>city3>>x3>>y3;
    double dx=x1-x;
    double dy=y1-y;
    double dx2=x2-x;
    double dy2=y2-y;
    double dx3=x3-x;
    double dy3=y3-y;
    d1=sqrt(pow(dx,2)+pow(dy,2));
    d2=sqrt(pow(dx2,2)+pow(dy2,2));
    d3=sqrt(pow(dx3,2)+pow(dy3,2));

    if (d1<=d2){
        minn=d1;
        city=city1;
    }
    else if (d2<d1)
    {
        minn=d2;
        city=city2;
    }

    if (d3<=minn)
    {
        minn=d3;
        city=city3;
    }

    cout<<city;
    return 0;
}
Last edited on
The problem with your first program is that you don't initialize minn (the shortest distance so far) to anything meaningful and that you always set it equal to the new distance no matter if it's shorter or not on line 21. After fixing these problems it correctly outputs Rome as the closest city. If Rome is not supposed to be the answer you probably should recheck so that you have copied the coordinates correctly.
I tried several times what you say.

I initialize min at a very big value.
But it always shows the first city.

In the above case, it always shows the last city.

In fact, the correct answer should be Stuttgart.
So, Rome is not correct I think.
No the closest city is Rome. If you don't believe me calculate the three distances by hand and see for yourself.

Are you sure you have not misunderstood the instructions and you are instead supposed to calculate which city is furthest away? In that case Stuttgart would be the correct answer.
@peter87.
You are right. I'm sorry. Thank you.

The question in fact is to find the closest city to the first city entered by the user.

The given x,y that I gave were just an example.

Thank you.
I will correct it now.
Yes, it was acceptted.
Thank you again.
Topic archived. No new replies allowed.