如何输入多个字母并在某个字母(q或q)后停止
How to input many letters and stop after a certain one (q or Q)?
我被分配了一个家庭作业,输入用户想要的字母数量,并在输入q或q后停止。然后程序应该输出这样的字符串中有多少元音。我们应该使用_getche((函数,但我有MacBook,所以它不起作用。
我试过做conio.h。我也尝试过使用cin.get((和getchar.((,但都不起作用。
__getche()
不是标准函数,conio.h
也不是标准标头。通常的方法是:
int main() {
int ch;
int vowel_count = 0;
while ((ch = getchar()) != 'q' && ch != 'Q' && ch != EOF) {
vowl_count += is_vowel(ch);
}
}
#include <iostream> // getchar(), std::cout
#include <cctype> // tolower()
int main()
{
int count = 0;
for (;;) // infinite loop
{
char c = getchar(); // read a single char from stdin
if (tolower(c) == 'q') // if input is 'q' or 'Q'
break; // break out of the loop
// check for vowel here
{
count++;
}
}
std::cout << "You typed in " << count << " vowels!n";
return 0;
}
我想你需要它:
#include <conio.h>
using namespace std;
int main()
{
bool h = true;
while(h){
char ch;
ch = getche();
if(ch=='q' || ch == 'Q'){
h = false;
}
}
return 0;
}
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- 多个输入输入 for 循环
- 当我输入一个字母时,cin 输入(输入是一个 int),而不是打印错误一次,它打印正确一次,然后在循环的其余部分打印 i
- 将输入输入到 c++ 数组时出现运行时错误
- 它不会打印出基于用户输入 c++ 输入的任务
- 将用户输入验证为整数:用户必须两次输入输入
- 是否可以将输入输入作为参数
- C 必须将输入输入两次
- 在C++程序运行时输入输入值 (./filename.out)
- 即使我做了>> cin ,也不让我输入输入
- 在没有用户输入输入的情况下中断while循环
- visual将c++ Gui中的输入输入到另一个c++程序中
- 在C中输入输入后,输出窗口关闭
- 将Python的输入输入到c++程序中
- 带空格的输入输入所有循环