如何输入多个字母并在某个字母(q或q)后停止

How to input many letters and stop after a certain one (q or Q)?

本文关键字:何输入 输入      更新时间:2023-10-16

我被分配了一个家庭作业,输入用户想要的字母数量,并在输入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;
}