Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 538999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:05:26+00:00 2026-05-13T10:05:26+00:00

I have a small, simple program with menu and submenus. User choose from 1-9

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:05:26+00:00Added an answer on May 13, 2026 at 10:05 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small (500kb) swing applet that displays very simple/limited set of small
I have found that my HTML is, to be honest, very clunky. Small, simple
I have small problem with my .net 2.0 winforms application. I want to embed
I have a small utility that I use to download an MP3 file from
I have a small local network. Only one of the machines is available to
I have a program in which I need to apply a 2-dimensional texture (simple
I have small page which has label, DropDownList and a submit button. <div> <asp:label
Where I work, we have small teams of 2 - 5 people. As a
I have a small JS function that does Ajax for me and another like
I have a small VB.NET application that I'm working on using the full version

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.