tags:

views:

94

answers:

1

Hi there.

I have a small, simple program with menu and submenus. User choose from 1-9 and hits enter. I want the code to read ONLY numbers 1-9 removing "\n" from stdin. I've tried sth like this:

#include <cstdio>
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

class cProgram
{
  private:
    char W;

  public:
    char choice(void);
    void choice(int _W);

    void showSubeMenu1(void);
    void showSubeMenu2(void);
    void showMainMenu(void);
};

char cProgram::choice()
{  return W;  };

void cProgram::choice(int _W)
{  W = _W;  };

void cProgram::showMainMenu(void)
{
  cout << "MAIN MENU:" << endl
       << "[1] option 1" << endl
       << "[2] option 2" << endl
       << "<0> quit" << endl
       << "Your choice: ";
  choice(cin.get());
  getchar();
}

switch (choice())
{
  case '1': choice('n'); showSubeMenu1(); break;    
  case '2': choice('n'); showSubeMenu2(); break;
  case '0': break; // EXITS the program
  default: choice('n'); showMainMenu(); break;
}  

// choice('n'); sets W to neutral char (not 1,2 or 0)

Everything works fine, until the user hits "\n" instead of normal key. By "normal" i mean not "\n". So, when the user hits enter, it is a must to hit enter again (twice in a row). Other way the program behaves weird.

A: 

I am not sure about your problem but I have a few tips for you. When you are using C++ then you should use std::cout and std::cin for input and output. They are stream from library <iostream>. You can also write using namespace std; and then you needn't write std::. Function printf() comes from C and is type unsafe so you shouldn't use it in C++.

Also streams offer many functions for getting information about successful or unsuccessful reading/writing etc. I really recommend it to you.

Method cin.get() reads only 1 character and it also can be a white character ( '\n', '\t', ' ' ). If you would like read number and ignore white spaces ( in default they are used like separators ) then you can use this code:

int x;
cin >> x;
if ( cin.fail() ) cout << "Reading error. It is not a number." << endl;
// cin.eof() means end of file, in this case it is end of input stream

I know when you want read something then user have to write requested data and '\n'. Character '\n' is important. I have never tried it but I think that it can be redefined. I read it somewhere.

I hope that my tips are helpful

Gaim
While your comment on `std::cout` being better C++ style than `printf()` is certainly correct, `printf()` is **not** *deprecated*.Second, `using namespace std;` isn't exactly good advice for a beginner; tell him to use `using std::cout; using std::cin;` and he won't get into the habit of polluting his namespace with everything and the kitchen sink.
DevSolar
Ok, you are right with namespace but it is a little subjective. Function printf is not type safe, it takes defined count of bytes and address and prints it. So it may not be officially deprecated but I think that it is not big evil when you say that it is. But ok I will fix it to being correct.
Gaim
Hey, i know i should use using std::cout; instead of using namespace std; and understand the bad habit of polluting namespaces in headers. I've tried to simplify the code i want to ask for, but now i see i did it the wrong way.Thanks for tips about using istream class.
dygi