2013-07-28 161 views
0

我寫了一個程序,每次輸入一個單詞直到輸入一個單獨的'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

+0

請查看我的解決方案 – Saksham

+0

我的解決方案有任何問題。至少有人會讚賞甚至不贊成我的答案 – Saksham

回答

1

的問題是,std::cin默認忽略所有的標籤和空格。因此,在你的代碼

if (isspace(ch)) //check if ch is a space 
    space = true; 

不落spacetrue。避免此問題的一種方法是使用std::cin.get(ch)代替std::cin>>ch

0

希望這有助於你

#include<iostream> 
#include<cstdlib> 
#include<string> 
int main() 
{ 
    char ch[50]; 
    int consta, vowel, others; 
    consta = vowel = others = 0; 
    bool flag = false; 
    std::cout<<"Enter words (q to quit)\n"; 
    while(1) 
    { 
     if(flag == true) 
     { 
      break; 
     } 
     gets(ch); 
     char* pch; 
     pch = strtok(ch," "); 
     if(strcmp("q",pch)==0) 
     { 
      break; 
     } 
     while (pch != NULL) 
     { 
      if(strcmp("q",pch)==0) 
      { 
       flag = true; 
       break; 
      } 
      if(isalpha(pch[0])) 
      { 
       if ((pch[0] == 'a' ||pch[0] == 'e' ||pch[0]== 'i' ||pch[0]== 'o' ||pch[0]== 'u') || (pch[0] == 'A' ||pch[0]== 'E' ||pch[0]== 'I' ||pch[0]== 'O' ||pch[0]== 'U')) 
        ++vowel; 
       else 
        ++consta; 
      } 
      else 
       ++others; 
      pch = strtok (NULL, " "); 
     } 
    } 
    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"; 
    std::cin.ignore(); 
    return 0; 
} 

請參考thisstrtok的。這將統計所有單詞是否用空格或單獨的行分隔。

0

假設你要使用相同的代碼結構,在while循環使用

while (cont && std::cin>> std::noskipws >>ch) 

的std :: noskipws:這將改變CIN流不能忽略空間的行爲。它還在流的末尾添加了空格。小心末端空間。幸運的是,你的代碼處理空間,所以你不會看到它的影響。

正如你在處理代碼中的空格,這將工作。另外,默認情況下,cin的行爲是忽略空格。

不過,我想你可以減少代碼的複雜性,你可能不需要這個說法