我寫了一個程序,每次輸入一個單詞直到輸入一個單獨的'q'。然後程序會報告以元音開頭的單詞的數量,這個單詞以輔音開頭,這兩個類別都不適合的數字。需要幫助調試字符輸入
#include <iostream>
#include <cstdlib>
int main()
{
char ch;
bool cont = true; //for controlling the loop
bool space = false; //see if there is a space in the input
int i = 0; //checking if the input is the first word
int consta, vowel, others;
consta = vowel = others = 0;
std::cout<<"Enter words (q to quit)\n";
while (cont && std::cin>>ch) //continue while cont is true and the input succeded
{
if (i == 0) //check if this is the first word
{
if (isalpha(ch))
if ((ch == 'a' ||ch == 'e' ||ch== 'i' ||ch== 'o' ||ch== 'u') || (ch == 'A' ||ch== 'E' ||ch== 'I' ||ch== 'O' ||ch== 'U'))
++vowel;
else
++consta;
else
++others;
++i; //add 1 to i so this if statement wont run again
}
if (space == true) //check if the last input was a space
{
if (!isspace(ch)) //check if the current input is not a space
{
if (ch != 'q') //and ch is not 'q'
{
if (isalpha(ch))
if ((ch == 'a' ||ch == 'e' ||ch== 'i' ||ch== 'o' ||ch== 'u') || (ch == 'A' ||ch== 'E' ||ch== 'I' ||ch== 'O' ||ch== 'U'))
++vowel;
else
++consta;
else
++others;
space = false;
}
}
else
cont = false;
}
if (isspace(ch)) //check if ch is a space
space = true;
}
std::cout<<"\n"<<consta<<" words beginnig with constants\n";
std::cout<<vowel<<" words beginnig with vowels\n";
std::cout<<others<<" words beginning with others\n";
system("pause");
return 0;
}
但是,它不會終止,當我輸入一個空格和'q'。 但是,如果我把「^ Z」它不終止,但constantans始終是1,其餘的是始終爲0
請查看我的解決方案 – Saksham
我的解決方案有任何問題。至少有人會讚賞甚至不贊成我的答案 – Saksham