This compiles but does not show me any results? Why

#include <iostream>
#include <cmath>
using namespace std;

void dist(int x1, int x2, int y1, int y2)
{
    double r1 , r2 , d ;
    r1 = sqrt((x1*x1) + (y1*y1));
    r2 = sqrt((x2*x2) + (y2*y2));
    d = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
    cout << " Distance from first point to origin :" << r1 << endl;
    cout << " Distance from second point to origin :" << r2 << endl;
    cout << " Distance between the two points:" << d << endl;
}
int main()
{
int x1, x2, y1, y2;
    cout << " Enter values for x1 x2 y1 y2\n";
    cin >> x1 >> x2 >> y1 >> y2 ;
    dist(x1, x2, y1, y2);


    char aaaa;
    cin >> aaaa;

    return 0;
}

Dani AI

Generated

Common causes for "compiles but shows no results" are: the program is waiting for input you never typed, the console closes immediately (you ran the EXE by double-clicking), or input was entered in the wrong order so later input gets consumed by the "pause" read. 's output shows the program does run; was right that a pause is often needed, but 's original code already used a single-character read as a pause, which can be confusing and is easily consumed if extra numbers are typed.

A safer, clearer approach:

  • use the natural coordinate order x1 y1 x2 y2,
  • validate input,
  • compute distances with std::hypot for accuracy,
  • and use a portable pause (ignore + get) instead of a raw cin >> char.

Example (safe, small changes):

#include <iostream>
#include <cmath>
#include <limits>

void dist(int x1, int y1, int x2, int y2) {
    double r1 = std::hypot(x1, y1);
    double r2 = std::hypot(x2, y2);
    double d  = std::hypot(x2 - x1, y2 - y1);
    std::cout << "Distance from first point to origin: " << r1 << '\n'
              << "Distance from second point to origin: " << r2 << '\n'
              << "Distance between the two points: " << d << '\n';
}

int main() {
    int x1, y1, x2, y2;
    std::cout << "Enter x1 y1 x2 y2:\n";
    if (!(std::cin >> x1 >> y1 >> x2 >> y2)) return 1;
    dist(x1, y1, x2, y2);
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cout << "Press Enter to exit...";
    std::cin.get();
}

Troubleshooting: run the program from a terminal/command prompt so output and prompts remain visible; enter exactly four numbers (spaces or newlines are fine); avoid extra values that will be taken by the pause read.

worked for me using vs2013

 Enter values for x1 x2 y1 y2
1 2 3 4 5
 Distance from first point to origin :3.16228
 Distance from second point to origin :4.47214
 Distance between the two points:1.41421
Press any key to continue . . .

the OP doesnt have any code to pause between the output and the window closing so I would guess that was the issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.