如何读取单个字符并在输入两个字符序列时输出? 使用 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
DECD
期望输出:
CS 开门iMacS主要开门
CSS 开门 AB 关门
关门
到目前为止编写的代码:
文件:开门.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
,如果您发现s
之后您找到了c
,那么您就有了答案。
另外,您可能知道,字符串本质上是一个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
相关文章:
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 使用无符号字符数组有效存储内存
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 有没有办法通过使用十进制 ASCII 代码自动类型扣除来获取字符?
- 如何使用C++将字符串中的字符转换为整数变量
- 在C++中使用 UTF-8 字符串和字符
- 使用 char 分隔符解析C++中的字符串,但将可重复的字符保留为每个解析的子字符串 (C++ STL) 中的分隔符
- 使用字符数组作为 Map 中的键
- 使用 fopen 打开 .pak 文件并使该文件应用于 const 无符号字符* (C++)
- 如何使用SQLite将数据库中的值导出为C / C++中的字符串或字符?
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- scanf() 只取数字作为输入,避免使用其他字符
- 使用动态分配将 char* 复制到另一个字符**
- 如何对非 ASCII 字符使用下沉函数
- 字符** 使用 new 运算符分配内存
- 如何一次对 3 个字符使用 switch 语句
- 使用Ampersand字符使用Qt Designer在mac上创建快捷方式
- 字符 [] 和新字符 [] 使用恒定长度时的区别
- 可以告诉非ascii字符使用