• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

C++ How to make a function stop upon user input

April 11, 2020 by

Questions › C++ How to make a function stop upon user input
0
Vote Up
Vote Down
Garmaine asked 3 years ago

I am creating a Turing machine simulator for a class. It takes in a files of transition functions, loads those transition functions into vectors, then takes in a word and checks if that word is accepted or rejected. The program works off a while loop in the operate function that handles the calculation. We are required to implement a button of our choice that will end the loop during calculation. How would I go about this?

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <thread>
using namespace std;

bool able_to_stop = true;

class turing_sim{
     public:
          void fill_vecs(int & num, string & word){
               switch(num){
                    case 0: curr_states.push_back(word);
                            break;
                    case 1: curr_tape_sym.push_back(word);
                            break;
                    case 2: new_state.push_back(word);
                            break;
                    case 3: new_tape_sym.push_back(word);
                            break;
                    case 4: direction.push_back(word);
                            break;
               }
          }

          void fill_tape(string & n){
               vector<string> newtape;
               tape = newtape;
               for(int i = 0; i < n.length(); i++){
                    auto it = tape.begin();
                    tape.insert(it + i, n.substr(i, 1));
               }
               tape_ptr = 0;
               current_state = "0";
          }

          string operate(){
               bool rejected = false;
               while(current_state != "f" && !rejected && !stop_operate){
                    int sub = 0;
                    for(int i = 0; i < curr_states.size(); i++){
                         if(curr_states[i] == current_state){
                              if(curr_tape_sym[i] == tape[tape_ptr]){
                                   cout<<current_state<<" ";
                                   for(int j = tape_ptr; j < tape.size(); j++){
                                        cout<<tape[j]<<" ";
                                   }
                                   cout<<endl;
                                   current_state = new_state[i];
                                   tape[tape_ptr] = new_tape_sym[i];
                                   if(direction[i] == "R"){
                                        tape_ptr++;
                                        if(tape_ptr > (tape.size() - 1)){
                                             tape.push_back("B");
                                        }
                                   }
                                   else{
                                        tape_ptr--;
                                        if(tape_ptr < 0){
                                             auto it = tape.begin();
                                             tape.insert(it, "B");
                                             tape_ptr = 0;
                                        }
                                   }
                                   sub = 2;
                              }
                         }
                    }
                    if(sub == 0){
                         rejected = true;
                         cout<<current_state<<" ";
                         for(int i = tape_ptr; i < tape.size(); i++){
                              cout<<tape[i]<<" ";
                         }
                         cout<<endl;
                    }
               }
               if(current_state == "f") return "Accepted!";
               else return "Rejected...";
          }

     private:
          vector<string> curr_states;
          vector<string> curr_tape_sym;
          vector<string> new_state;
          vector<string> new_tape_sym;
          vector<string> direction;

          string current_state = "0";

          int tape_ptr = 0;

          vector<string> tape;

          bool stop_operate = false;
};

void Decompose_sentence(string& sentence, turing_sim & sim){//separates each line
     string wrd = "";                                       //into individual elements
     int count = 0;                                         //and stores them in vectors
     for(int i = 0; i < sentence.size(); i++){
          if(sentence.substr(i, 1) != " "){
               wrd += sentence.substr(i, 1);
          }
          else if(wrd != ""){
               sim.fill_vecs(count, wrd);
               wrd = "";
               count++;
          }
          if(sentence.substr(i + 1, 1) == "/") i = sentence.size() + 1;
     }
     if(!wrd.empty()){
          sim.fill_vecs(count, wrd);
     }
}

void read_file(string & file, turing_sim & sim){//loads important lines to decompose
     string sentence;
     ifstream infile;
     infile.open(file);
     while (getline(infile, sentence)) {
          while(sentence.back() == '\r' || sentence.back() == '\n'
               || sentence.back() == '.' || sentence.back() == char(34)){
                    sentence.erase(sentence.size() - 1);
          }
          if(sentence[0] != '/' && !sentence.empty()){
               Decompose_sentence(sentence, sim);
          }
     }
     infile.close();
}

int main(){
     turing_sim machine;
     string file, word;
     cout<<"Enter file name: ";
     cin>>file;
     read_file(file, machine);
     bool x = true;
     while(x){
          cout<<"Enter word to test: ";
          cin>>word;
          machine.fill_tape(word);
          cout<<machine.operate()<<endl;
     }
     return 0;
}
Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: c++

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements architecture beautiful life best building calling city commercial convenience employment Finances Cognitive decline Future gadgets Hidden Gems highway Home houses hydration Impact Innovations lamp lighting Mental health military tech Must-See New York City occupation Productivity recreation romance sepia shopping sippy cups smartphones social Technological breakthroughs technology toddlers Treasures turns Uncover Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements architecture beautiful life best building calling city commercial convenience employment Finances Cognitive decline Future gadgets Hidden Gems highway Home houses hydration Impact Innovations lamp lighting Mental health military tech Must-See New York City occupation Productivity recreation romance sepia shopping sippy cups smartphones social Technological breakthroughs technology toddlers Treasures turns Uncover Well-being Wonders Work Young onset dementia

Copyright © 2023