0

What I want to happen here is whenever result[0](randomly generated letter) is found in array1(list of words), the word should move to array2.

But when I display array2, I get the original words no matter what result[0] is. And when I display found I get a string of numbers repeated (~4942576). What am I doing wrong?

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

char response;
int letter;
int letter2;
int n;
char  result[8];
size_t found= 0;
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()
{
pointer:
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)                     //selects 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<8; 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];
    }
}

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

    if (found!=std::string::npos)
        cout << array1[i] << ' ';

}

cout << endl << endl<<"Your words are: ";
for (int i =0; i <sizeof(array2[i]); i++)
    cout << array2[i] << endl;
return 0;
}
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
user3105207
  • 9
  • 1
  • 5
  • 5
    That's an awful lot of data to store on the stack. You also never store anything in `result[0]`. – chris Dec 15 '13 at 19:44
  • You're `find`ing `result` in each `string` of `array1`, but your question states that you're trying to find `result[0]` in each element of `array1`. Might that be your problem? – Jashaszun Dec 15 '13 at 19:46
  • @chris Unless I'm missing something, those arrays are globals and aren't stored on the stack. –  Dec 15 '13 at 19:48
  • @polkadotcadaver, Oops, that's right. For some reason, I only realized the global part in relation to `result` being initialized at all. In that case, it's a lot to store in the executable. – chris Dec 15 '13 at 19:49
  • @chris it isn't even stored in the executable :) For the ELF solution. see http://stackoverflow.com/questions/610682/bss-section-in-elf-file –  Dec 15 '13 at 19:50
  • No, that was a mistake on my part, my code says result[0]. – user3105207 Dec 15 '13 at 19:50
  • @polkadotcadaver, That's interesting, thanks. – chris Dec 15 '13 at 19:51
  • @user3105207, Ah, missed the `cin >> response;` part. I was referring to `response[n]`, where `n` is always greater than zero at that point. – chris Dec 15 '13 at 19:52
  • An idea for a more scalable and readable implementation would be to initialise a static vector which maps vector index to a character. vector consonants = {'b', 'c'... etc}. Then your huge switch statement becomes result[n] = characters[letter]. Same for vowels. –  Dec 15 '13 at 19:57
  • I know my switch statement is bug and ugly but that part works. – user3105207 Dec 15 '13 at 20:00
  • 2
    @user3105207 : You could replace both switch-case statements with a simple array or pair of arrays... – Joe Z Dec 15 '13 at 20:02

2 Answers2

0

I see the following in your code:

array1[i].find(result)

This tries to find result in each word. Here, result is declared as char[8], that is, a string. Since your code never stores anything into the first element of the result array, it probably represents an empty string, which can be found in any string. This is not what you want! You probably want to find result[0] instead.

You also wrote the following in comments:

No, that was a mistake on my part, my code says result[0]

Your code clearly says result, not result[0]. Try changing result to result[0] (or, because of how your other code works, result[1]); this will fix some of the problems.


In addition, you are trying to fill some arrays here. Arrays don't support this - you should use vectors instead. Then, you can use their size method instead of the incorrect sizeof operator:

std::vector<string> array2;
...
for (int i = 0; i < array2.size(); i++)
    cout << array2[i] << endl;
anatolyg
  • 26,506
  • 9
  • 60
  • 134
0

This isn't related to your problem, but for the love of god, don't do those horrible switch statements, that's a LOT of duplicated and redundant code. Here's the same functionality:

char pickRandomVowel() {
    char vowels[] = "aeiou";
    return vowels[rand() % sizeof(vowels)];
}

char pickRandomConsonant() {
    char consonants[] = "bcdfghjklmnpqrstvw";
    return consonants[rand() % sizeof(consonants)];
}

int main()
{
    srand (time(0));

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

        // keep retrying
        while (response != 'c' && response != 'v')
        {
            cout << "Invalid, please choose 'c' or 'v'" << endl;
            cin >> response;
        }

        char letter;
        if ( 'c' == response ) 
        {
            letter = pickRandomConsonant();
        }    
        else if ('v' == response)
        {
            letter = pickRandomConsonant();
        }

        cout << letter;
        result[n] = letter;
    }

    // etc...
}
Alexander Kondratskiy
  • 4,156
  • 2
  • 30
  • 51