2

ユーザーに単語または文を入力するように求め、単語/文を調べ、「a」または「A」のすべてのインスタンスを「aoa」または「AoA」に置き換えてから結果を出力する C++ プログラムを作成しています。ただし、長い文を入力しようとすると問題が発生します。たとえば、「なぜプログラムが動作しないのか」と入力すると、プログラムは期待される結果ではなく、奇妙な文字を出力します。これは私のコードです:

#include <iostream>
#include <string>

using namespace std;

int main(int argc, const char * argv[])
{
string mening, temp; //The mening string is the word/sentence the user will input.
int play = 1, add;

while (play == 1) {
cout<<"Type in the sentence: ";

getline(cin, mening); //The input is saved in the string variable mening.

unsigned long y = mening.size(); //Grabs the amount of characters in input; this number is saved in the unsigned long variable y.
add = 0; //Makes sure the int variable add is reset to 0 if the loop restarts.

for (int k = 0, n = 1;n<=y;k++, n++) {
    if (mening[k] == 'a' || mening[k] == 'A') {
        k++;


        for (int i = k, m = 1;m<=y - n;i++, m++) {
            temp[i] = mening[i];
        } //The characters after the one that has been checked are stored in temp array indexes, if the character that has been checked is an a or A.

        for (int i = k, m = 1, j = k + 2;m<=y - n;i++, m++, j++) {
            mening[j] = temp[i];
        } //The characters after the one that has been checked move two steps to the right, to allow the two extra letters.

        mening[k] = 'o';
        mening[k + 1] = mening[k - 1];

        k++;

        add = add + 2; //The int variable add is increased by 2 during each aoa/AoA to avoid strange characters being outputted at the very end.

    }
    else { }

}

for (int k = 0;k<=y + add - 1;k++) {
    cout<<mening[k];
}

cout<<endl<<"Do you want to do it again? (yes/no): ";

getline(cin, mening);

    cin.clear();
    cout << flush;
    cout.flush();
    cout.clear();

if (mening == "Yes" || mening == "yes" || mening == "YES") {

}
else {
    play = 2;
}
}


cout<<endl<<"The program will now close.";

return 0;
}

問題の原因は何ですか?

4

4 に答える 4

0
#include <iostream>
#include <string>


std::string ReplaceA(std::string s) {
    std::string temp = "";
    for (unsigned int k = 0; k < s.size(); k++) {
        if (s[k] == 'a' || s[k] == 'A') {
            temp = temp + s[k];
            temp = temp + "o";
            temp = temp + s[k];
            }
            else { 
                temp = temp + s[k];
            }

        }
    return temp;
}

int main(int argc, const char * argv[])
{
    std::string mening; //The mening string is the word/sentence the user will input.
    int play = 1;

    while (play == 1) {
        std::cout<<"Type in the sentence: ";

        getline(std::cin, mening); //The input is saved in the string variable mening.

        std::cout << ReplaceA(mening) << std::endl;

        std::cout << "Do you want to do it again? (yes/no): ";

        std::cin >> mening;
        if( std::cin.fail() || ( mening != "yes" && mening != "no" ) ) {
            std::cout << "Bad Input\nDo you want to do it again? (yes/no): ";
            std::cin.clear();
            std::cin.ignore('256','\n');
            std::cin >> mening;
        }else{
            if(mening == "no") break;
        }
        std::cin.clear();
        std::cin.ignore('256','\n');

    }


    std::cout << "The program will now close.";

    return 0;
}

文字列を返す変換を処理する関数を使用して、より整理されています。

この機能を試すこともできます。どちらも機能します。一方は少しきれいに見えます。

std::string ReplaceA(std::string s) {
    std::string temp = "";
    for(std::string::iterator k = s.begin(); k != s.end(); k++) {
        switch(*k) {
        case 'a': temp += "aoa"; break;
        case 'A': temp += "AoA"; break;
        default: temp += *k; break;
        }
    }
    return temp;
}
于 2013-09-18T16:53:18.773 に答える
0

文字またはstd::string::insertを挿入するには、標準関数を使用することをお勧めします。これにより、コードの処理とデバッグがはるかに簡単になります。'oA''oa'

または、次のように簡単に実行できます。

#include <iostream>
#include <string>

using namespace std;

int main(int argc, const char * argv[])
{
string mening, temp; //The mening string is the word/sentence the user will input.
int play = 1, add;

while (play == 1) {
cout<<"Type in the sentence: ";

getline(cin, mening); //The input is saved in the string variable mening.

unsigned long y = mening.size(); //Grabs the amount of characters in input; this number is saved in the unsigned long variable y.
add = 0; //Makes sure the int variable add is reset to 0 if the loop restarts.

for (int n = 0; n<y; n++ ) {
    cout << mening[n];
    if (mening[n] == 'a' || mening[n] == 'A') 
        cout << "o" << mening[n];
}

cout<<endl<<"Do you want to do it again? (yes/no): ";

getline(cin, mening);

    cin.clear();
    cout << flush;
    cout.flush();
    cout.clear();

if (mening == "Yes" || mening == "yes" || mening == "YES") {

}
else {
    play = 2;
}
}


cout<<endl<<"The program will now close.";

return 0;
}
于 2013-09-18T16:44:56.520 に答える