Help fix my code please

I submit my code through mimir and get full credit for quality but fail when it comes to test input. Below is my prompt for writing a program for calculating distance between 2 points and below that is my code.

Write a C++ console program that declares a function named printDistance. The purpose of the function is to calculate the distance between two points and print the result.

printDistance should define 4 parameter variables. The first 2 parameters declare the x and y coordinates of the first point, and the second 2 parameters declare the x and y coordinates of the second point.

Note that this function should not return a value. We will study value returning functions in Chapter 05.

The printDistance function should not prompt for input. User input should be captured from inside the main function. The user input should then be passed as arguments to printDistance with a function call.

Search the web for information about the distance formula if you have forgotten it. You will need to make use of the pow function inside the math library. There is also a sqrt function in the math library you can use. The same thing can be accomplished by using the pow function with an exponent of 0.5.

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

void printDistance(int x1, int y1, int x2, int y2) {
float distance;
distance = sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2));
cout << distance << endl;
return;
}

int main() {
printDistance(3, 5, 6, 9);
cout << printDistance << endl;
}
So there are a few places you need to remember C++ syntax.

You call printDistance which prints distance to the screen, then what do you do next? Focus on what you want and what your main is doing.

sqrt returns a double while you're putting it in a float, it's not wrong just not necessary to use a float. Your function is void but you have a return statement.

Also for future reference "fix my code" is not a very good way to approach a forum. Do some more research, maybe even start over and try again.
Last edited on
Sorry ToeMater. I missed my second week of CS&131 because my wife was having a baby and I am brand new to the subject. I realize that I'm definitely missing a few key commands. Maybe I need more cout commands? Obviously I've been searching the internet trying to pick up those couple of pieces that I'm missing and I'm really looking for more of a point in the right direction than fixing my code so i guess that headline was a little far out but thank you for your suggestions. I will look up those topics you mentioned.
Think of it this way,
Step 1. call printDistance
Step 2. (you're in printDistance) print distance does some calculations and prints the result to the screen
Step 3. (back in main) cout printDistance.

if you don't know when you do cout << printDistance << endl; you are printing the boolean value of the function. That's probably not what you were expecting.

When I run this on my IDE I get 2 output values, do you want two output values? There should only be one right? So think about where each originates from.

Your professor wants you to take input from a user in main, cout is for output do you know what the standard input stream object is called?
Last edited on
Topic archived. No new replies allowed.