C++ 取出两个字母

C++ 取出两个字母,c++,C++,我的程序生成用户选择的随机辅音或元音。然后,它在字典中搜索这些单词并显示它们。我想要发生的是改变双字母,使它们不再相同,而不必再次询问用户 #include <iostream> #include <fstream> #include <cstdlib> #include <ctime> #include <algorithm> #include <string> #include <array> usin

我的程序生成用户选择的随机辅音或元音。然后,它在字典中搜索这些单词并显示它们。我想要发生的是改变双字母,使它们不再相同,而不必再次询问用户

#include <iostream> 
#include <fstream> 
#include <cstdlib> 
#include <ctime>
#include <algorithm>
#include <string>
#include <array>
using namespace std;


char response;
int letter;
int letter2;
int n;
char  result[8];
string a = " ";

string array1 [70549];
string array2[70549] ;
string array3[70549] ;
string array4[70549] ;
string array5[70549] ;
string array6[70549] ;
string array7[70549] ;
string array8[70549] ;
string array9[70549] ;
string array10[70549] ;

char test = 'a';
int j = 0;
int k = 0;

  int main()
{
for (int a =0; a<8; a++)
{
cout<< "Consonant (c) or vowel(v)?" << endl;
cin >> response; 


{


if ( 'c' == response ) 
{
    srand (time(0));
    letter =  (rand() %21);
    n ++;


        switch (letter)         //seleccts random const
        {
        case 0: 
            cout << "b" << endl;
            result[n] = 'b';
            break;

        case 1: 
            cout << "c" << endl;
            result[n] = 'c';
            break;

        case 2:  
            cout << "d" << endl;
            result[n] = 'd';
            break;

        case 3: 
            cout << "f" << endl;
            result[n] = 'f';
            break;

        case 4:
            cout << "g" << endl;
            result[n] = 'g';
            break;

        case 5: 
            cout << "h"<< endl;
            result[n] = 'h';
            break;

        case 6: 
            cout << "j" << endl;
            result[n] = 'j';
            break;

        case 7: 
            cout << "k" << endl;
            result[n] = 'k';
            break;

        case 8: 
            cout << "l" << endl;
            result[n] = 'l';
            break;

        case 9: 
            cout << "m" << endl;
            result[n] = 'm';
            break;

        case 10: 
            cout << "n" << endl;
            result[n] = 'n';
            break;

        case 11: 
            cout << "p" << endl;
            result[n] = 'p';
            break;

        case 12: 
            cout << "q" << endl;
            result[n] = 'q';
            break;

        case 13: 
            cout << "r" << endl;
            result[n] = 'r';
            break;

        case 14: 
            cout << "s"<< endl;
            result[n] = 's';
            break;

        case 15: 
            cout << "t" << endl;
            result[n] = 't';
            break;

        case 16: 
            cout << "v"<< endl;
            result[n] = 'v';
            break;

        case 17: 
            cout << "w"<< endl;
            result[n] = 'w';
            break;

        case 18: 
            cout << "x" <<endl;
            result[n] = 'x';
            break;

        case 19: 
            cout << "y"<< endl;
            result[n] = 'y';
            break;

        case 20: 
            cout << "z"<< endl;
            result[n] = 'z';
            break;
        }
}

   else if ('v' == response)
{
    srand (time(0));
    letter2 = ( rand() %4);
    n++;

    switch (letter2)                                      //selects random vowel
        {
        case 0: 
            cout <<  "a"<< endl;
            result[n] = 'a';
            break;

        case 1: 
            cout << "e" <<endl;
            result[n] = 'e';
            break;

        case 2: 
            cout << "i"<< endl;
            result[n] = 'i';
            break;

        case 3: 
            cout << "o"<< endl;
            result[n] = 'o';
            break;

        case 4: 
            cout << "u" << endl;
            result[n] = 'u';

        }   
    }
     else if (  response != 'c' || 'v')
        {
            cout << "Invalid, please choose 'c' or 'v'"<< endl;
            cin >> response;
        }

}


}



cout<<endl<< "Your letters are";




for(int i=0; i<9; i++)
{
    cout << result[i];
    cout << " ";
}



 ifstream file("C:\\Users\\Chris\\Documents\\words.txt");
if(file.is_open())
{

    for(int i = 0; i < 70549; ++i)
    {
        file >> array1[i];
    }
}

    int l = 0;




    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array1[i].find(result[1]);

            if (found != std::string::npos)
                array2[i] = array1[i];              
        }   


    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array2[i].find(result[2]);

            if (found!=std::string::npos)

                array3[i] = array2[i];              
        }

    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array3[i].find(result[3]);

            if (found!=std::string::npos)

                array4[i] = array3[i];
        }

    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array4[i].find(result[4]);

            if (found!=std::string::npos)

                array5[i] = array4[i];              
        }

    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array5[i].find(result[5]);

            if (found!=std::string::npos)

                array6[i] = array5[i];              
        }

    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array6[i].find(result[6]);

            if (found!=std::string::npos)

                array7[i] = array6[i];              
        }

    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array7[i].find(result[7]);

            if (found!=std::string::npos)

                array8[i] = array7[i];              
        }

    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array8[i].find(result[8]);

            if (found!=std::string::npos)

                array9[i] = array8[i];              
        }


cout << "Your words are: ";
    for ( int i=0; i< (sizeof(array2)/sizeof(*array2));i ++)
        if (!array9[i].empty())
        {
        cout << array9[i] << ", ";
        }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
字符响应;
整数字母;
int字母2;
int n;
字符结果[8];
字符串a=“”;
字符串数组1[70549];
字符串数组2[70549];
字符串数组3[70549];
字符串数组4[70549];
字符串数组5[70549];
字符串数组6[70549];
字符串数组7[70549];
字符串数组8[70549];
字符串数组9[70549];
字符串数组10[70549];
字符测试='a';
int j=0;
int k=0;
int main()
{

对于(int a=0;a一些一般建议:

  • 使用二维字符串数组,如
    string sa[10][70549];
  • 不要重复调用srand()。只需播种一次。(来自上面的Lie Ryan)
  • 使用字符数组而不是那些讨厌的开关块
  • 您有一个元音选择错误(称为%4,而不是%5,因此您永远不会看到“u”)
  • 同时使用
    a
    n
    作为位置跟踪器是不好的,看起来像上面的bug
  • else if(response!=“c”|“v”)
    也是一个bug;你的意思是说
    if(response!=“c”&&response!=“v”)
    ,但考虑到上面的测试,这实际上是多余的。只需使用
    else{…}
  • 为了防止出现两个相同的连续字母(惰性方式),在每个字母生成块中,通过a/n==0,循环随机生成器,直到它得到与前一个字符不同的结果

这个程序过于臃肿,以至于人们不愿意帮助你,因为你只需浏览代码。这看起来像是一个编程入门课的家庭作业。你对事情的运作方式有足够的误解,你可能需要花一些时间阅读一些初级书籍并从助教那里获得一对一的帮助。

我不确定这是否与您的问题有关,但这段代码可能不正确:

 else if (  response != 'c' || 'v')
    {
        cout << "Invalid, please choose 'c' or 'v'"<< endl;
        cin >> response;
    }
<>在C++中,一个非零的变量总是被认为是真的,而0被认为是假的。在这种情况下,'v '有一个非零字符值(记住字符基本上只是格式化(通常)8位数字)。
else if (response != 'c' && response != 'v')


或者,您可以将整个if('c')/else if('v')/else if(!('c'| |'v'))我不太确定你想做什么,但是我想到了
std::unique
。我不想删除它们,我还需要8个字母。我只是不想重复你忘了问一个问题。你给我们看了你的程序,告诉了你你想做什么,然后就没问Don’不要问我们一个问题。你有什么问题?你有没有制定出这样做的算法?如果没有,为什么不请我们帮助你这么做?如果有,你在实现它时有什么问题?不要调用srand()在您的程序中不止一次。这样做实际上会产生非常可预测的随机性。在这样的小游戏中,这可能不是什么问题,但在更重要的程序中,这可能会导致安全漏洞。此外,您的大开关表可以通过索引到char:
char[]consonates=“bcdfghjklmnpqrstvwxyz”;结果[n]=辅音[rand()%21]
。元音也是如此。
else if (response != 'c' && response != 'v')
else if (!(response == 'c' || response == 'v'))