-1

I am trying to get a cin running without interupting the cout (a dialogue for example or instructions).

If you do just cin >> a; while your cout(s) are running through it, it pauses the code to wait for user input, so I was directed to a new thread for it, as I am new and it's my first time using threads finally figuring it out I still have an issue, it waits for the main thread to finish before it completes the if statement (exit) but it will give the cout instantly.

Any help is appreciated.

#include <QCoreApplication>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <cstdlib>
#include <thread>
#include <unistd.h>
#include <cstdlib>
using namespace std;


string typesome;
thread function1;

void function_1()
{
    //cout << "I am Running.. - CIN";
    getline(cin, typesome);
   //std::thread relc(releasecin, getline(cin, typesome));
    if (typesome == "you good cuz")
{
    cout << "You typed " << typesome << " I will stop\n";
    system("exit");
}
    else
{
    cout << "this is my story\n";
}

};

int main(void)
{

    std::thread t1(function_1); // Start the CIN
   // t1.join(); // Waits for the CIN to load
    cout << "Hey type something while i talk\n";
    usleep(10000000);
    cout << "I am a nice person\n";
    cout << "I like pie\n";
    usleep(10000000);
    cout << "At any time if i get out of line just type you good cuz to stop me\n";
    if (typesome == "you good cuz")
    cout << "You typed " << typesome << " I will stop";
    else
    cout << "this is my story\n";
    usleep(10000000);
    cout << "West philly born and raised\n";


    return 0;

}
Pang
  • 9,564
  • 146
  • 81
  • 122
Exsanity
  • 33
  • 4
  • You should consider the thread safety/contract that the cout/cin functions make: http://stackoverflow.com/questions/6374264/is-cout-synchronized-thread-safe . – AhiyaHiya Nov 12 '16 at 02:52

1 Answers1

0

I figured out a way to accomplish this by just putting a while in the thread so when the getline is the correct word it switches to the while loop forgetting the main and i figured out threads and all that!

Exsanity
  • 33
  • 4