-2

I am in the works of trying to write a program that reads in individual characters and prompts the user when the two-character-sequence 'cs' that the door has opened. I'm stuck on where or how to store the current value and output the response after the string has completely been read.

Data reading in from notepad:

cs
imacsmajor
css
ab
decds

Desired output:

cs open door
imacsmajor open door
css open door
ab closed door
decds closed door

code written so far:

//file: opendoor.cpp

#include <conio.h>

#include <fstream>// requires for external file streams 

#include <iostream>

using namespace std; 
// Associating program identifiers with external file names 
#define in_file  "data.txt"
#define out_file "resline.txt" 

void main()
{
const char nwln = '\n';  // print a new line 

ifstream ins;  // associates ins as an input stream  ofstream outs; 
ofstream outs; // associates outs as an output stream 

int  door = 0;  
char  current, previous = ' ', password;

ins.open(in_file);
outs.open(out_file);   

// Repeat processing until end of file is reached 
ins.get(current);  

while (!ins.eof())  
    {     
        cout << current ;
        // read a character   
        while (current != '\n')  
            {    
                ins.get(current);       
                cout << current ;  
            }
    } 
_getch();  
ins.close();    // closing input file 
outs.close();   // closing output file
}  // end main 
Cœur
  • 32,421
  • 21
  • 173
  • 232
J_man
  • 13
  • 3
  • 1
    set a flag when `c` appears, reset the flag when something else appears. If `s` appears while the flag is set, the door opens. – Blaze Oct 17 '18 at 08:22
  • 1
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Oct 17 '18 at 08:23
  • @J_man https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Biffen Oct 17 '18 at 11:52
  • Welcome to Stack Overflow! Your image of text [isn't very helpful](//meta.unix.stackexchange.com/q/4086). It can't be read aloud or copied into an editor, and it doesn't index very well, meaning that other users with the same problem are less likely to find the answer here. Please [edit] your post to incorporate the relevant text directly (preferably using copy+paste to avoid transcription errors). – Toby Speight Oct 17 '18 at 13:35
  • @J_man did my answer help you? – PradyumanDixit Oct 17 '18 at 16:33
  • Pradyuman Dixit Yes all the input helped. Thanks – J_man Oct 17 '18 at 17:15
  • @J_man great it helped you solve your problem, you can also mark the answer as correct if you got help from the answer, I'd appreciate that. Cheers! – PradyumanDixit Oct 18 '18 at 04:15

2 Answers2

0

All you have to do is check if there's a c in your given string, and if you find s just after that point where you've found a c, then you have your answer.

Also as you may know, string is essentially a char array.

In code this algorithm may look something like this:

std::string s = "abcdcsg";

// string is essentially a char array to begin with

for (int i = 0; i< s.length(); i++)
{
    if (s[i] == 'c')
    {
       i+=1;
       if(s[i]=='s'){
         //found
         break;
       }
    }
}
PradyumanDixit
  • 2,311
  • 2
  • 10
  • 20
0

This is my code. for anyone else struggling.

//file: dooropen.cpp
#include <conio.h>
#include <iostream>
#include <fstream>// requires for external file streams
using namespace std; // Associating program identifiers with external file names
#define in_file  "data.txt" 
#define out_file "resline.txt"
void main()
{
    const char nwln = '\n';  // print a new line
    ifstream ins;  // associates ins as an input stream  
    ofstream outs; // associates outs as an output stream 
    int count = 0;
    int  door = 0;
    int isc = 0;
    char  current, previous = ' ';
    ins.open(in_file);
    outs.open(out_file);    // Repeat processing until end of file is reached 

ins.get(current);

while (!ins.eof())
{
    cout << current;// read a character   
    while (current != '\n')
    {
        if (current == 'c')
        {
            isc = count;
        }
        if (current == 's')
        {
            if (count == isc + 1)
            {
                door = 1;
            }
        }
        ins.get(current);
        cout << current;
        count++;
    }
    if (door == 1)
    {
        cout << ". . . . . . . . . . . . . . . . Door open \n";
    }
    else
    {
        cout << ". . . . . . . . . . . . . . . . Door closed \n";
    }
    door = 0;
    isc = 0;
    ins.get(current);
}
_getch();
ins.close();    // closing input file 
outs.close();   // closing output file
}  // end main
J_man
  • 13
  • 3