如何从输入中获取特定字符
how to get a specific character from input?
我做了一个关于getchar的教程:getChar - C++ 教程
这一切都有效,直到我想进行一些修改。
char c;
puts("enter . to exit");
do{
c=getchar();
} while (c != '.'); {
if(c == 's'){
upgradeOne();
cout << "upgrade1 is done" << endl;
}
if (c == 'a'){
upgradeTwo();
cout << "upgrade2 is done" << endl;
}
}
while (total < 999){
total += i;
cout << total << endl;
}
我想在用户输入使用特定字符时使用特定方法。如果用户键入 a,则必须激活方法升级二()。我应该怎么做?
输入的重复处理发生在do...while
循环中。仅当按下'.'
时,循环才会离开。
如果你想对特定的字符做出反应,你必须将你的if(c==...)
语句移动到这个循环中(即在getchar()
和while
之间)。
如果你想运行循环直到用户输入 period(.),那么你可以使用一些最初将设置为 true 的布尔标志。然后写一段时间,直到标志为假。在 while 中读取字符,如果是句点,则将标志设置为 false。
或者,您可以无限运行 while 循环,如果 char 是一个句点,则中断循环,否则继续。
相关文章:
- constexpr 函数获取常量字符*
- 有没有办法通过使用十进制 ASCII 代码自动类型扣除来获取字符?
- 如何获取C++字符数据类型的地址
- 从格式化输入操作中获取读取字符数
- 如何从 QString 中包含的十六进制值中获取 ASCII 字符?
- 从我的 2DgridArray 中的文本文件中获取每个字符
- 如何将字符传递到find_child_by_attribute中以获取 pugixml
- 根据字符获取数组的索引
- C++ 获取向量中常量字符* [ ] 的长度
- 获取带有字符 X 的输入 X/X/etc 并用斜杠 / 分隔它
- 为什么我无法获取 MSVS2019 / C++ 中字符或uint8_t变量的内存地址?
- 如何从 c++ 中的给定标准输入中获取每个字符的偏移量?
- CPP:如何获取字符的最后一个索引
- 试图在重载函数中获取字符和字符串的输入
- 获取Unicode字符C++的十进制值
- 你能安全地从字符串的 c_str() 常量字符*中获取指向字符串的指针吗?
- 使用位于 C 字符串中的两个字符*获取子字符串
- 如何使用字符获取多个输入,使用 int 获取多个输出?C++
- c++列表字符获取问题,内存分配不好
- 从十六进制字符获取数据