Thread: terminating the program after valid input sequence

  1. #1
    Registered User
    Join Date
    Nov 2021
    Posts
    4

    Question terminating the program after valid input sequence

    Hi! I'm making a C++ program which requires the user to input only binary digits (0,1) indefinitely until the user input 1's and 0's in a sequence of
    1
    0
    1
    0
    1
    Problem: Now the problem I'm facing is that previously i have made a program that runs until the user gives a valid input but in this case I don't understand how can I specify this sequence for program to end. I planned of using if(true) and (false) in a while(true) loop. but while I couldn't implement it.

    exact question for the code:
    Write a C++ program that will indefinitely take a binary input of 0 or 1. However, upon receiving a special code pattern of 10101, the program will stop the input, output “Input Terminated”, and then the
    program will terminate.

    sample output:
    terminating the program after valid input sequence-po2-png

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Like this?
    Code:
    int exit_pattern[] = { 1, 0, 1, 0, 1 };
    int exit_i = 0;
    do {
      cin >> v;
      if ( exit_pattern[exit_i] == v ) exit_i++;  // one step closer
      else exit_i = 0;  // back to the beginning
    } while ( exit_i != 5 );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating console input in C and C++
    By etrusks in forum C Programming
    Replies: 7
    Last Post: 01-05-2016, 09:10 AM
  2. terminating the program if s certian input is recieved
    By jekkler in forum C++ Programming
    Replies: 8
    Last Post: 11-05-2010, 11:27 PM
  3. Terminating input on EOF
    By vyn in forum C Programming
    Replies: 7
    Last Post: 03-12-2009, 02:33 AM
  4. terminating input
    By Chaplin27 in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2004, 05:32 PM
  5. terminating input by ctrl+z
    By ammar in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2002, 06:03 AM

Tags for this Thread