// Find the midpoint of 2 points
#include <iostream>
#include <cstdlib>
#include <cstdio>
usingnamespace std;
int main() {
char ME;
double X1;
double Y1;
double X2;
double Y2;
double MX;
double MY;
cout << "Find the Midpoint or Endpoint of 2 points.\n\nAre you trying to find the Midpoint or Endpoint?\n[M/E] ";
cin >> ME;
if (ME = M) {
cout << "\n\nEnter the the first coordinate: ";
cout << "\n(";
cin >> X1;
cout << X1 << ",";
cin >> Y1;
cout << Y1 << ")";
cout << "\n Enter the second coordinate: ";
cout << "\n(";
cin >> X2;
cout << X2 << ",";
cin >> Y2;
cout << Y2 << ")";
cout << "\n\nMidpoint is: ";
}
return 0;
}
Now it will compile with the following error: Midpoint.cpp:18: error: ‘M’ was not declared in this scope
Can anyone help me fix that little error?
Also... If I take out the if statement it outputs like this.
1 2 3 4 5 6 7 8 9 10 11 12 13
Find the Midpoint or Endpoint of 2 points.
Are you trying to find the Midpoint or Endpoint?
[M/E] M
Enter the the first coordinate:
(2
2,3
3)
Enter the second coordinate:
(4
4,5
I am trying to get it to look like this:
1 2 3 4 5 6 7 8 9 10
Find the Midpoint or Endpoint of 2 points.
Are you trying to find the Midpoint or Endpoint?
[M/E] M
Enter the the first coordinate:
(2,3)
Enter the second coordinate:
(4,5)
Any idea how to fix that?
Thanks in advance!
Ps. I probably can't get back on tonight cause I have school so I will be back on tomorrow.
If you are trying to compare ME to the character M use if(ME == 'M'). Make sure you use == instead of = because the compiler will assign 'M' to ME and return true if you don't.
This works. Add #include <string> to the top of your program.
Instead of: char ME;, use: string ME;
Then, replace if (ME=M); with: if (ME.compare("M") || ME.compare("m"))
(Note: Note the '|| ME.compare("m") is optional, but the program will otherwise only run properly if a capital M is entered. This just allows you to enter either a capital or lowercase m.)
The reason this works is because the string class provides a compare method that takes a string as it's argument. It compares the ASCII code values of the characters within the strings and returns zero when the strings are identical. When the strings differ, it returns a -1 when the compared string is of a lower value, or 1 when the second string is of a higher value.
On another note, I have the C++ for Dummies book, too. Despite the 'for Dummies' title, I think that it makes things slightly more complicated than really necessary. I reccomend C++ Programming in Easy Steps by Mike McGrath. It was a lot easier to read. Hope that helps.
You can't display it on the same line since you have to hit enter (which moves to the next line) to get each number. Don't print them out again, since they already are displayed as the user enters them. Try something like this: