Returning to different parts of script.

I hate C++ for Dummies. It doesn't explain anything.

This is my script (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Find the midpoint of 2 points

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cctype>
using namespace std;

int main() 
{
	char ME;
	double X1, Y1, X2, Y2, MX, MY, EX, EY;
	cout << "Find the Midpoint or Endpoint of 2 points.\n\n"
            "Are you trying to find the Midpoint or Endpoint?\n"
            "[M/E] ";
	cin >> ME;
    if (tolower(ME) == 'M')
    {
        cout << "Enter the the first coordinate:\n";
        get_coord( x1, y1 );
        cout << "\nEnter the second coordinate:\n";
        get_coord( x2, y2 );
		MX = (X1 + X2) / 2;
		MY = (Y1 + Y2) / 2; 
		cout << "Midpoint is: " << "(" << MX << "," << MY << ")"; 
		std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
		std::cin.get();
		return 0;
	}
	
	if (tolower(ME) == 'E')
        {
	cout << "Enter the the first coordinate:\n";
        get_coord( X1, Y1 );
        cout << "\nEnter the midpoint:\n";
        get_coord( MX, MY );
		EX = (((-X1 / 2) + MX) * 2);
		EY = (((-Y1 / 2) + MY) * 2);
		cout << "Endpoint is: " << "(" << EX << "," << EY << ")"; 
		std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
		std::cin.get();
		return 0;
	}
	
	else
	{
		cout << "You didn't enter M or E!";
		


After the last line "cout << "You didn't enter M or E!" I need it to clear the screen (note I'm in Linux) and I need it to return to the part that says "cout << "Find the Midpoint or Endpoint of 2 points.\n\n"
"Are you trying to find the Midpoint or Endpoint?\n"
"[M/E] ";"

Like I said I don't mean to be so dumb but my book sucks.

Thanks in advance.
Last edited on
Use a loop. To be simplistic:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  bool done = false;
  char ME;
  do {
    cout << "blah blah blah";
    cin >> ME;
    ME = tolower( ME );

    switch (ME) {
      case 'm': ...; done = true; break;
      case 'e': ...; done = true; break;
      default: ...;
      }

  } while (!done);


Clearing the screen is not so simple as you might think. Here's a little code that will do it for you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// clearscreen.hpp

#ifndef CLEARSCREEN_H
#define CLEARSCREEN_H

namespace console {

  void cursorxy( int col, int line );
  void clear( bool gohome = true );

  }

#endif

// end clearscreen.hpp 

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
// clearscreen.cpp

#include <term.h>
#include <unistd.h>
#include "clearscreen.hpp"

static char *console_clearscreen    = 0;
static char *console_cursorposition = 0;

static bool console_initialize_terminfo()
{
  int result;
  if (cur_term) return true;
  setupterm( NULL, STDOUT_FILENO, &result );
  if (result > 0)
  {
    console_clearscreen    = tigetstr( "clear" );
    console_cursorposition = tigetstr( "cup"   );
  }
  return (result > 0);
}

void console::cursorxy( int col, int line )
{
  if (console_initialize_terminfo())
    putp( tparm( console_cursorposition, line, col, 0, 0, 0, 0, 0, 0, 0 ) );
}

void console::clear( bool gohome )
{
  if (console_initialize_terminfo())
  {
    putp( console_clearscreen );
    if (gohome) cursorxy( 0, 0 );
  }
}

// end clearscreen.cpp 

This requires the terminfo/curses libraries. Link with -lterminfo or -lcurses (whichever works).

For example, using the GCC, the compile command would be something like:

% g++ myprog.cpp clearscreen.cpp -lcurses

Hope this helps.

[EDIT] 2008-02-19: Fixed a typo and an omission in the code. [/EDIT]
[EDIT] 2008-02-20: Fixed the omitted header file to be the correct header file...[/EDIT]
[EDIT] 2008-02-25: Fixed the "gcc" to use "g++" so that it will compile...[/EDIT]
Last edited on
Topic archived. No new replies allowed.