Help with displaying text on same line.

Hi. I am new here and I just got into C++.
I bought C++ for dummies and am following it well.
I am using Ubuntu Linux.

I am trying to make a little program that finds the midpoint between 2 points on a coordinate plane.

Here is my code so far...

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
// Find the midpoint of 2 points

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace 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.
Last edited on
Your problem is here...

 
if (ME = M)


M isn't defined.
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.
Last edited on
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.





Oh yeah thanks for clearing that up.

But how do I fix the text on wrong line problem?
Last edited on
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:
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
// Find the midpoint of 2 points

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

void get_coord( double &x, double &y )
{
    cout << "x: ";
    cin >> x;
    cout << "y: ";
    cin >> y;
}

int main() {
    char mid_or_end;
    double x1, y1, x2, y2, mx, my;

    cout << "Find the Midpoint or Endpoint of 2 points.\n"
            "Are you trying to find the Midpoint or Endpoint?\n"
            "[M/E] ";

    cin >> mid_or_end;
    if (tolower(mid_or_end) == 'm')
    {
        cout << "Enter the the first coordinate:\n";
        get_coord( x1, y1 );
        cout << "Enter the second coordinate:\n";
        get_coord( x2, y2 );

        cout << "Midpoint is: ";
    }
}


Note that it is customary to use lowercase letters for most variable names.
Also, indentation is important!
Last edited on
Topic archived. No new replies allowed.