Midpoint Formula

Hey, I'm a beginner and I'm trying to make a C++ program that calculates midpoints. I'm on a mac and I'm using the latest version of Xcode. When I run the program, it builds successfully. However, when I'm running the program it usually stops working once I input the value for a: when I hit return, it just keeps creating new lines instead of continuing. Even when the program does continue past that, it always gives the same incorrect values for the coordinates. Please Help.

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>
using namespace std;

int main ()

{
	float a;
	float b;
	float c;
	float d;
	float acmid;
	float bdmid;
	
    cout << "This program will find the midpoint (x,y) for points (a,b) and (c,d). \n";

	//x coor for point (a,b)
	cout << "Enter the value for a: ";
	cin >> a;
	cout << "\n";
	
	//y coor for point (a,b)
	cout << "Enter the value for b: ";
	cin >> b;
	cout << "\n";
	
	//x coor for point (c,d)
	cout << "Enter the value for c: ";
	cin >> c;
	cout << "\n";
	
	//y coor for point (c,d)
	cout << "Enter the value for d: ";
	cin >> d;
	cout << "\n";
	
	acmid = (a +c)/2;
	bdmid = (b +d)/2;
	
	cout << "The midpoint (x,y) is: \n";
	cout << "x = "; cout << acmid;
	cout << "\n";
	cout << "y = "; cout << bdmid;
	
	return 0;
}
I compiled your code using MSVC2012 and it ran perfectly.

I'm not very familiar with mac compilers but the first thing you should try is cleaning your project. Sometimes the compiler can get confused over things like time stamps and wont compile everything. Cleaning the project will cause the program to recompile completely the next time you build it.
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
#include <iostream>
// using namespace std;

int main ()
{
	float a;
	float b;
	float c;
	float d;
	// float acmid;
	// float bdmid;

        std::cout << "This program will find the midpoint (x,y) for points (a,b) and (c,d). \n";

	//x coor for point (a,b)
	std::cout << "Enter the value for a: ";
	std::cin >> a;
	// cout << "\n";

	//y coor for point (a,b)
	std::cout << "Enter the value for b: ";
	std::cin >> b;
	// cout << "\n";

	//x coor for point (c,d)
	std::cout << "Enter the value for c: ";
	std::cin >> c;
	// cout << "\n";

	//y coor for point (c,d)
	std::cout << "Enter the value for d: ";
	std::cin >> d;
	// cout << "\n";

	const float acmid = (a+c)/2;
	const float bdmid = (b+d)/2;

	std::cout << "The midpoint (x,y) is: \n"
                  << "x = " << acmid << '\n'
	          << "y = " << bdmid << '\n' ;

	//return 0;
}
Last edited on

I compiled your code using MSVC2012 and it ran perfectly.

I'm not very familiar with mac compilers but the first thing you should try is cleaning your project. Sometimes the compiler can get confused over things like time stamps and wont compile everything. Cleaning the project will cause the program to recompile completely the next time you build it.


Thanks for letting me know that it works. I'll try to clean it up, but I don't really know what you mean by "time stamps."
#include <iostream>
// using namespace std;

int main ()
{
float a;
float b;
float c;
float d;
// float acmid;
// float bdmid;

std::cout << "This program will find the midpoint (x,y) for points (a,b) and (c,d). \n";

//x coor for point (a,b)
std::cout << "Enter the value for a: ";
std::cin >> a;
// cout << "\n";

//y coor for point (a,b)
std::cout << "Enter the value for b: ";
std::cin >> b;
// cout << "\n";

//x coor for point (c,d)
std::cout << "Enter the value for c: ";
std::cin >> c;
// cout << "\n";

//y coor for point (c,d)
std::cout << "Enter the value for d: ";
std::cin >> d;
// cout << "\n";

const float acmid = (a+c)/2;
const float bdmid = (b+d)/2;

std::cout << "The midpoint (x,y) is: \n"
<< "x = " << acmid << '\n'
<< "y = " << bdmid << '\n' ;

//return 0;
}


Thanks for the help, but, being a beginner, I am in need of some explanations:

1) Why did you write using namespace std and other commands as comments? I thought compilers just ignored stuff that follows //

2)What is the difference between cout and std::cout, and cin std::cin?

3) What is the difference between float and const float?

Thanks again for the help!
Wait, I just tried running JLBorges's code and it's still doing the thing where it stops working after I enter the value for a. Someone please help me.
What value are you trying to input for a?
What value are you trying to input for a?


I've tried several different values, but when i press return it just keeps making new lines instead of continuing with the program.
> What is the difference between cout and std::cout, and cin std::cin?

Entities provided by the standard C++ library are declared in namespace std.
The cin provided by the library is std::cin and cout is std::cout


> Why did you write using namespace std and other commands as comments?
> I thought compilers just ignored stuff that follows

See: http://www.parashift.com/c++-faq/using-namespace-std.html



> What is the difference between float and const float?

A const float is like a normal float, except that after it is initialized, its value can't be changed.



> I've tried several different values,
> but when i press return it just keeps making new lines instead of continuing with the program.

Create a completely new XCode project (in a different directory), type in the same code afresh into the new file, and then compile and run it.
Topic archived. No new replies allowed.