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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 356k
  • Answers 356k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well.. as i see it, this is a normal behaviour.… May 14, 2026 at 8:56 am
  • Editorial Team
    Editorial Team added an answer You want to do something like this instead: <Button> <StackPanel>… May 14, 2026 at 8:56 am
  • Editorial Team
    Editorial Team added an answer The opensocial-development-environment is very useful for developing OS applications. You… May 14, 2026 at 8:56 am

Related Questions

I am working on a program which includes a general purpose engine, some program
I have a small server program that accepts connections on a TCP or local
I have a program i frequently use that is made with .NET. This program
I have a small C program to calculate hashes (for hash tables). The code
I have a small task in Flash, that I'm having a little trouble with.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.