2012-04-11 97 views
1

首先,在此先感谢您的帮助。这个问题让我发疯。C++:不会接受新的C字符串输入

我有一个接受的c字符串,然后可以指望元音和辅音的数目的程序。这工作没有问题。不过,我还需要包含一个允许用户创建新字符串的函数。但问题在于,当用户从菜单中选择“新字符串”时,它只是循环使用newString()方法,而不用等待用户的输入。然后它创建一个新的空白屏幕。

这是整个程序。 newString()方法在最后。

#include <iostream>  
using namespace std; 

// function prototype 
void printmenu(void); 
int vowelCount(char *); 
int consCount(char *); 
int cons_and_vowelCount(char *); 
void newString(char *, const int); 

int main() { 

    const int LENGTH = 101; 
    char input_string[LENGTH];  //user defined string 
    char choice;      //user menu choice 
    bool not_done = true;  //loop control flag 

    // create the input_string object 
    cout << "Enter a string of no more than " << LENGTH-1 << " characters:\n"; 
    cin.getline(input_string, LENGTH); 

    do { 
     printmenu(); 
     cin >> choice; 
     switch(choice) 
     { 
      case 'a': 
      case 'A': 
       vowelCount(input_string); 
       break; 
      case 'b': 
      case 'B': 
       consCount(input_string); 
       break; 
      case 'c': 
      case 'C': 
       cons_and_vowelCount(input_string); 
       break; 
      case 'd': 
      case 'D': 
       newString(input_string, LENGTH); 
       break; 
      case 'e': 
      case 'E': 
       exit(0); 
      default: 
       cout << endl << "Error: '" << choice << "' is an invalid selection" << endl; 
       break; 
     } //close switch 
    } //close do 

while (not_done); 
return 0; 
} // close main 

/* Function printmenu() 
* Input: 
* none 
* Process: 
* Prints the menu of query choices 
* Output: 
* Prints the menu of query choices 
*/ 
void printmenu(void) 
{ 
    cout << endl << endl; 
    cout << "A) Count the number of vowels in the string" << endl; 
    cout << "B) Count the number of consonants in the string" << endl; 
    cout << "C) Count both the vowels and consonants in the string" << endl; 
    cout << "D) Enter another string" << endl; 
    cout << "E) Exit the program" << endl; 
    cout << endl << "Enter your selection: "; 
    return;  
} 

int vowelCount(char *str) { 
    char vowels[11] = "aeiouAEIOU"; 
    int vowel_count = 0; 

    for (int i = 0; i < strlen(str); i++) { 
     for (int j = 0; j < strlen(vowels); j++) { 
      if (str[i] == vowels[j]) { 
       vowel_count++; 
      } 
     } 
    } 
    cout << "String contains " << vowel_count << " vowels" << endl; 
    return vowel_count; 
} // close vowelCount 

int consCount(char *str) { 
    char cons[43] = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"; 
    int cons_count = 0; 

    for (int i = 0; i < strlen(str); i ++) { 
     for (int j = 0; j < strlen(cons); j++) { 
      if (str[i] == cons[j]) { 
       cons_count++; 
      } 
     } 
    } 
    cout << "String contains " << cons_count << " consonants" << endl; 
    return cons_count; 
} // close consCount 

int cons_and_vowelCount(char *str) { 
    int cons = consCount(str); 
    int vowels = vowelCount(str); 
    int total = cons + vowels; 

    cout << "The string contains a total of " << total << " vowels and " 
      "consonants" << endl; 
    return total; 
} 

void newString(char *str, int len) { 
    cout << "Enter a string of no more than " << len-1 << " characters:\n"; 
    cin.getline(str, len); 
    return; 
} 

回答

6

语句cin >> choice只消耗他们键入的字符,而不是后面的回车符。因此,随后的getline()调用读取一个空行。一个简单的解决方案是呼叫getline()而不是cin >> choice,然后使用第一个字符作为选择。

顺便说一下,while (not done)应该紧跟在do { … }之后,而return 0是多余的。另外,您应该在程序开始时调用newString,而不是重复其内容。

+1

另一个解决方案是调用'cin.ignore()''后CIN >> choice',这将从流滴一个字节。 – jli 2012-04-11 00:35:02

+0

@jli:如果用户很傻,并且在字符后键入空格(或者认为他们必须键入'A)'),它就不太健壮。但我正在分裂头发;你的是一个完全有效的选择。 – 2012-04-11 00:38:34

+0

是的,我同意,只是抛出它作为一种可能性。 (它也会在CRLF上窒息,使得它更不健壮) – jli 2012-04-11 00:39:55

3

cin >> choice在输入流中留下换行符,导致下一个getline()消耗它并返回。有很多方法..一种方法是在cin >> choice之后使用cin.ignore()

0

cin >> choice仅消耗来自流的一个字符(如已经提到的)。您应该添加

cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); 

cin>>choice之后忽略阅读的选择后进入流中的所有字符。

p.s. #include <limits>