1

まず、ご協力いただきありがとうございます。この問題は私を悩ませています。

私はc-stringを受け入れ、母音と子音の数を数えることができるプログラムを持っています。これは問題なく機能します。ただし、ユーザーが新しい文字列を作成できるようにする関数も含める必要があります。ただし、問題は、ユーザーがメニューから「新しい文字列」を選択する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;
}
4

3 に答える 3

6

ステートメントcin >> choiceは、入力した文字のみを使用し、後続のキャリッジリターンは使用しません。したがって、後続のgetline()呼び出しは空の行を読み取ります。getline()簡単な解決策の1つは、代わりに呼び出しcin >> choiceて、最初の文字を選択肢として使用することです。

ところで、while (not done)はすぐ後に続く必要がdo { … }あり、return 0は冗長です。また、プログラムの内容を繰り返すのではなく、プログラムの開始時にnewStringを呼び出す必要があります。

于 2012-04-11T00:32:23.877 に答える
3

cin >> choice入力ストリームに改行を残します。これにより、次のgetline() 行がそれを消費して戻ります。多くの方法があります..1つの方法は、cin.ignore()直後に使用することですcin >> choice

于 2012-04-11T00:34:32.443 に答える
0

cin >> choiceストリームから1文字しか消費しません(すでに述べたように)。追加する必要があります

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

cin>>choice選択を読んだ後にストリームに入るすべての文字を無視する直後。

ps#include <limits>

于 2012-04-11T01:03:11.070 に答える