[Wylug-help] C++ code

Philip Wyett philipwyett at dsl.pipex.com
28 Oct 2002 02:23:56 +0000


--
On Mon, 2002-10-28 at 00:10, Roger Greenwood wrote:
> Philip Wyett wrote:
> >
> > Hi,
> >
> > fflush(stdin); may help you once input has been taken and it's contents
> > can be safely dumped. I would suggest you look at the whole code as it
> > is hybrid and never much welcomed by many, especially C++ coders.
> >
> > Regards
> >
> > Philip Wyett
> >
> Good point. By hybrid you mean C/C++? This comes from learning pascal
> first, then C, then some C++ stuff, all within a few months.
>

Yes the mixing of C and C++. Sometimes it cannot be helped, but to
adhere to one or the other is best. The thing which really grabbed my
attention though was:

int valid = FALSE;

better is:

bool is_valid = false;

Proper use of the language and good naming saves alot of problems later.
Especially after more than 12 months when someone asks you to add a
feature too some code.

> fflush didn't work by the way. I will keep looking.
>

I will take a look at the code on the morrow, when I'm not as busy.

> p.s. What is a good way (best way?) to get a single character input
> (e.g. y/n) ?
>

A simple way todo the 'y/n' with a little input fault tolerance is:

#include <iostream>

using namespace std;

int main()
{
  char option;

  do
  {
    cout << "\nAre you briliant? [y/n]: ";
    cin >> option;

    switch(option)
    {
      case 'y':
      case 'Y':
	cout << "\nModest ain't you! :)\n\n";
	break;
      case 'n':
      case 'N':
	cout << "\nToo modest!\n\n";
	break;
      default:
	cout << "\n*** Invalid option. ***";
	cout << "\nPleae enter either 'y' or 'n'\n";
	break;
    }
  }while ((option != 'y') && (option != 'Y')
      && (option != 'n') && (option != 'N'));

  return 0;
}

Ok, time for some sleep.

Regards

Philip Wyett

--

Key pair can be obtained using the link below.

http://www.philipwyett.dsl.pipex.com/PhilipWyett.asc

--
--
Content-Description: This is a digitally signed message part

[ signature.asc of type application/pgp-signature deleted ]
--