输入双字符序列时如何读入单个字符并输出?使用 while 循环和 C++

How to read in individual characters and output when two-character-sequence is inputted? using while loop and C++

我正在尝试编写一个程序,该程序读取 个字符,并在出现双字符序列 'cs' 门时提示用户已经打开。我被困在何处或如何存储当前值并在完全读取字符串后输出响应。

从记事本读入的数据:

cs
imacsmajor
css
ab
decds

期望输出:

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

到目前为止编写的代码:

//文件: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,如果您在找到 c 之后找到 s,那么你就有了答案。

您可能也知道,字符串本质上是一个 char 数组。

在代码中,该算法可能如下所示:

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;
       }
    }
}

这是我的代码。对于其他挣扎的人。

//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