Trying to solve an array comparison issue

So.. I have 2 codes, The intent is to descramble 10 words that are entered through cout / cin prompt. by savething them into 2 arrays, one consisting of the actual word, the other the words total ascii value. i am then attempting to compare the words i want to descramble to a "wordlist.txt" that i also have 2 arrays set up for one with the acutal word from the "wordlist.txt" and the other with all of the "wordlist.txt" ascii values. i then am trying to compare the ascii values to see if their equal and if so add them to my finalAnswerArray.

This first program goes as far as setting up all of my arrays succesfully (as far as i know) and prints out the results of the arrays. The issues you will see arises in my second code which is not comparing correctly, and getting stuck somewhere. Any input or help would be appreciated!

First Code- Currently Working
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
int counter = 0;
int number = 0;
string asciiSum;
int sumUpdate = 0;
string getwordsArray[9];

void convertToASCII(string letter)
{
    int sum = 0;
    for (int i = 0; i < letter.length(); i++)
    {
        char x = letter.at(i);
        //cout << int(x) << endl;
        sum += int(x);
    }
    sumUpdate = sum;
}


string temp;
void getwords()
{

    for (int g = 0; g < 10; g++)
    {
        cout << "Please enter you word here " << g << ":\n";
        cin >> temp;
        getwordsArray[g] = temp;
    }
}

int main ()
    {
    getwords();

    int asciiArray[1275];
    string array[1275]; // creates array to hold names
    short loop=0; //short for loop for input
    string line; //this will contain the data read from the file
    ifstream myfile ("C:\\Users\\Zack\\Desktop\\wordlist.txt"); //opening the file.
    if (myfile.is_open()) //if the file is open
    {
    while (! myfile.eof() ) //while the end of file is NOT reached
    {
    getline (myfile,line); //get one line from the file
    array[loop] = line;
    cout << array[loop] << endl; //and output it
    asciiSum = line;
    convertToASCII(asciiSum);
    asciiArray[loop] = sumUpdate;
    cout << asciiArray[loop] << "\n" << "\n";
    loop++;
    }
    //cout << "the loop counted too: " << loop;

    myfile.close(); //closing the file
    string temp1;
    int getwordAsciiArray[9];
    for (int i = 0; i < 10; i++)
    {
        temp1 = getwordsArray[i];
        convertToASCII(temp1);
        getwordAsciiArray[i] = sumUpdate;
    }
    cout << "\nThis is the words array: \n";
    for (int i = 0; i < 10; i++)
    {
        cout << getwordsArray[i] << "\n";
    }
    cout << "\nThis is the value of each word in the array: \n";
    for (int i = 0; i < 10; i++)
    {
        cout << getwordAsciiArray[i] << "\n";
    }
    }
    else cout << "Unable to open file"; //if the file is not open output
    system("PAUSE");

}

Second Code in Progress
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
int counter = 0;
int number = 0;
string asciiSum;
int sumUpdate = 0;
string getwordsArray[9];
string primaryAnswerArray[9];
string primaryAnswer;
// small helper method to display the before and after arrays
void displayArray (string array[], int size)
{
 cout << "{";
 for ( int i = 0; i < size; i++ )
 {
  // you'll see this pattern a lot for nicely formatting
  // lists--check if we're past the first element, and
  // if so, append a comma
  if ( i != 0 )
  {
   cout << ", ";
  }
  cout << array[ i ];
 }
 cout << "}";
}

void convertToASCII(string letter)
{
    int sum = 0;
    for (int i = 0; i < letter.length(); i++)
    {
        char x = letter.at(i);
        //cout << int(x) << endl;
        sum += int(x);
    }
    sumUpdate = sum;
}



void getwords()
{
    string temp;
    for (int g = 0; g < 10; g++)
    {
        cout << "Please enter you word here " << g << ":\n";
        cin >> temp;
        getwordsArray[g] = temp;
    }
}

int main ()
    {
    getwords();
    int asciiArray[1275];
    string wordlistArray[1275]; // creates array to hold names
    short loop=0; //short for loop for input
    string line; //this will contain the data read from the file
    ifstream myfile ("C:\\Users\\Zack\\Desktop\\wordlist.txt"); //opening the file.
    if (myfile.is_open()) //if the file is open
    {
    while (! myfile.eof() ) //while the end of file is NOT reached
    {
    getline (myfile,line); //get one line from the file
    wordlistArray[loop] = line;
    //cout << wordlistArray[loop] << endl; //and output it
    asciiSum = line;
    convertToASCII(asciiSum);
    asciiArray[loop] = sumUpdate;
    //cout << asciiArray[loop] << "\n" << "\n";
    loop++;
    }
    }
    myfile.close(); //closing the file
    //cout << "the loop counted too: " << loop;
    getwords();

    string temp1;
    int getwordAsciiArray[19];
    for (int i = 0; i < 10; i++)
    {
        temp1 = getwordsArray[i];
        convertToASCII(temp1);
        getwordAsciiArray[i] = sumUpdate;
    }
//    cout << "\nThis is the words array: \n";
//    for (int i = 0; i < 10; i++)
//    {
//        cout << getwordsArray[i] << "\n";
//    }
//    cout << "\nThis is the value of each word in the array: \n";
//    for (int i = 0; i < 10; i++)
//    {
//        cout << getwordAsciiArray[i] << "\n";
//    }
    int a = 0;
    int b = 0;
    int d = 0;
    int e = 0;
    string secondaryAnswer;
    string secondaryAnswerArray[30];
    while(b < 1276)
    {
        while (b < 1275) /// While statement that gets all the words that match the entered word  ///
        {
        if (getwordAsciiArray[d] == asciiArray[b])
           {
            secondaryAnswer = wordlistArray[b];
            secondaryAnswerArray[a] = secondaryAnswer;
            a++;
           }
        b++;
        }
        b = 0;
      //  string primaryAnswer;
     //   string primaryAnswerArray[10];
        while (d < 10)  /// While statement that checks to see if the entered word matches wordlist.txt
        {
            for (int i = 0; i < a; i++)
            {
                if (secondaryAnswerArray[i] == getwordsArray[d])
                {
                    primaryAnswer = secondaryAnswerArray[i];
                    primaryAnswerArray[e] = primaryAnswer;
                    e++;
                    return 0;
                }
            }
        }
        d++;
        a = 0;
    }
    cout << "The words you entered are: \n";
    displayArray(getwordsArray, 10);
    cout << "The Final Answers are: \n";
    displayArray(primaryAnswerArray, 9);  /// Final Results
//    cout << "\nThis is the words array: \n";
//    for (int i = 0; i < 10; i++)
//    {
//        cout << getwordsArray[i] << "\n";
//    }
    //cout << "\nThis is the value of each word in the array: \n";
//    int b = 0;
//    int a = 0;
//    string finalAnswer;
//    string finalAnswerArray[9];
//   while (b < 1275)
//    {
//            if (getwordAsciiArray[a] == asciiArray[b])
//            {
//                finalAnswer = array[b];
//                finalAnswerArray[a] = finalAnswer;
//                a++;
//                b = 0;
//            }
//            b++;
//    }  // while statement
//    cout << "The Correct words are: \n";
//    for (int i = 0; i < 10; i++)
//    {
//        cout << finalAnswerArray[i] << "\n";
//    }
  //  }  // for if statement
}
Last edited on
Updated the code to current progress
Could you give an example of the input file "wordlist.txt" contents please.

Could you explain the relationship between the two programs - is the second intended to supersede the first, or are they both supposed to carry out different tasks?
Last edited on
wordlist.txt is a txt doc with one word per line. Bout to update the code to where im at, at the point of needing to sort the diff arrays after ive compared ascii..think i could actually cut out a lot of the second half if i could figure out how to implement a sort function on the arrays. working on that

The goal is to match the unscrambled word from the wordlist.txt with a scrambled word i have typed in.

Hope that clarified a little.

Thanks
OK, thank you. but could you give an example of the actual text in the file, and an example of some of the words which will be typed in. I'm trying to test some of your code, but i need some test data in order to get sensible results.

So far I've started looking at the first program. The design looks a bit haphazard, with both global and local variables used without any particular plan. In particular
at line 10, an array of 9 elements is defined:
 
    string getwordsArray[9];
the valid subscripts for accessing elements will be in the range getwordsArray[0] to getwordsArray[8]. However, in function getwords() g will take values in the range of 0 to 9, thus the last value will be stored in the 10th element. That is one position past the end of the array, and such out of bounds access will cause corruption of some other memory, with unpredictable results.

Generally, a more controlled way of doing things might be like this:
1
2
const int size = 9;
string getwordsArray[size];


1
2
3
4
5
6
7
8
void getwords()
{
    for (int g = 0; g < size; g++)
    {
        cout << "Please enter you word here " << g+1 << ":\n";
        cin >> getwordsArray[g];
    }
}


Above, the use of a named constant allows everything to be kept consistent, and if you wanted to change the array size, only one line needs to change and the rest of the code will remain in sync.

Well, that's as far as I got. There may be other errors as i get further in.

Here's my initial take on program1. I removed most of the global variables, and fixed some of the errors. Also, i removed the eof() loop which is generally not a good way to control reading from a file. It is better to test the status of the file after attempting to read from it. This way, the body of the while loop is entered only when the input was successful.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int size = 9;
string getwordsArray[size];
int getwordAsciiArray[size];

int convertToASCII(string letter)
{
    int sum = 0;
    for (unsigned int i = 0; i < letter.length(); i++)
    {
        sum += int(letter.at(i));
    }
    return sum;
}

void getwords()
{
    for (int g = 0; g < size; g++)
    {
        cout << "Please enter you word here " << g+1 << ":\n";
        cin >> getwordsArray[g];
    }
}

int main ()
{
    getwords();

    int asciiArray[1275]; // array for letter sum
    string array[1275];   // array to hold names
    int count = 0;        // number of words which were read from the file
    string line;          // this will contain the data read from the file
    ifstream myfile ("wordlist.txt");

    if (!myfile.is_open()) // if the file is not open
    {
        cout << "Unable to open file";
        // system("PAUSE");
        return 1;
    }

    while ( getline (myfile,line) )
    {
        array[count] = line;
        asciiArray[count] = convertToASCII(line);
        cout << array[count] << " " << asciiArray[count] << "\n\n";
        count++;
    }

    myfile.close();
    cout << "Number of lines read from file: " << count << endl;


    for (int i = 0; i < size; i++)
    {
        getwordAsciiArray[i] = convertToASCII(getwordsArray[i]);
    }

    cout << "\nThis is the words array: \n";
    for (int i = 0; i < size; i++)
    {
        cout << getwordsArray[i] << "\n";
    }
    cout << "\nThis is the value of each word in the array: \n";
    for (int i = 0; i < size; i++)
    {
        cout << getwordAsciiArray[i] << "\n";
    }

//     system("PAUSE");

}
Last edited on
Thanks for the reply, example input

[code]
1234qwer
1a2b3c
1q2w3e
test123
1p2o3i
puppy123
kitten12
qwerty12
john316
apollo13
ne1469
amanda1
mazda1
angela1
alpha1
sarah1
nirvana1
bubba1
scuba1
rambo1
charlie1
david1
digital1
dragon1
honda1
shadow1
eagle1
freak1
james1
1sanjose
[/close]
your deff right about the originial way i had it looking through the txt document was not very effecient and wouldnt run smoothly.. thanks for the help!!

edit: ive been trying different ways of sorting the string, any suggestions on best method?
Last edited on
ok, so this is where im currently at in comparing, i have gotten it to successfully sort the wordlist.txt array and the words entered, but program is not responding when it gets to my comparing loop i am assuming?

edit: had apifiny, got it working, thanks for the help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

using namespace std;

/// /// /// /// /// ///   Global Variables   /// /// /// /// /// /// /// ///
const int size = 10;
string getwordsArray[size];
int getwordAsciiArray[size];
int asciiWordArray[size];
/// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// ///
 ///         ///           Functions        ///      ///    ///     ///    ///
/// /////////////////////////////////////////////////////////////////////////////////////////////////
// small helper method to display the before and after arrays
void displayArray (string array[], int size)
{
 cout << "{";
 for ( int i = 0; i < size; i++ )
 {
  // you'll see this pattern a lot for nicely formatting
  // lists--check if we're past the first element, and
  // if so, append a comma
  if ( i != 0 )
  {
   cout << ", ";
  }
  cout << array[ i ];
 }
 cout << "}";
}
/// ////////////////////////////////////////////////////////////////////////////////////////////////////
void intdisplayArray (int array[], int size)
{
 cout << "{";
 for ( int i = 0; i < size; i++ )
 {
  // you'll see this pattern a lot for nicely formatting
  // lists--check if we're past the first element, and
  // if so, append a comma
  if ( i != 0 )
  {
   cout << ", ";
  }
  cout << array[ i ];
 }
 cout << "}";
}
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////

int convertToASCII(string letter)
{
    int sum = 0;
    for (unsigned int i = 0; i < letter.length(); i++)
    {
        sum += int(letter.at(i));
    }
    return sum;
}
/// ////////////////////////////////////////////////////////////////////////////////////////////////////
/// ////////////////////////////////////////////////////////////////////////////////////////////////////
int main ()
{
    int asciiArray[1275]; // array for letter sum
    string array[1275];   // array to hold names
    int count = 0;        // number of words which were read from the file
    string line;          // this will contain the data read from the file
    ifstream myfile ("C:\\Users\\Zack\\Desktop\\wordlist.txt");
    fstream File;
    string arraySorted[1275];  // array for the sorted array
    string wordsSorted[size];
    string answerArray[size];
    string text;
    string text1;
    string text2;
    string text3;
    int text4;
    int b = 0;
    int status = 1;
/// //////////////////////////////////////////////////////////////////////////////////////////////////
    for (int g = 0; g < size; g++)
    {
        cout << "Please enter you word here " << g+1 << ":\n";
        cin >> getwordsArray[g];
        text3 = getwordsArray[g];
        text4 = convertToASCII(text3);
        asciiWordArray[g] = text4;
    }

    if (!myfile.is_open()) // if the file is not open
    {
        cout << "Unable to open file";
        // system("PAUSE");
        return 1;
    }

    while ( getline (myfile,line) )
    {
        array[count] = line;
        text = line;
        sort( text.begin(), text.end() );
        arraySorted[count] = text;
        asciiArray[count] = convertToASCII(line);
        //cout << array[count] << " " << asciiArray[count] << " " << arraySorted[count] <<"\n\n";
        count++;
    }

    myfile.close();
//    cout << "Number of lines read from file: " << count << endl;


    for (int i = 0; i < size; i++)
    {
        getwordAsciiArray[i] = convertToASCII(getwordsArray[i]);
    }

    for (int i = 0; i < size; i++)
    {
       text1 = getwordsArray[i];
       sort( text1.begin(), text1.end() );
       wordsSorted[i] = text1;
    }

    for (int i = 0; i < size; i++)
    {
        b = 0;
        status = 1;
        while (status)
        {
            if (wordsSorted[i] == arraySorted[b] && asciiWordArray[i] == asciiArray[b])
            {
                text2 = array[b];
                answerArray[i] = text2;
                status = 0;
            }
            else
            {
                b++;
            }
        }
    }
    File.open("C:\\Users\\Zack\\Desktop\\answers.txt", ios::out);

    if (File.is_open ())
    {
        for (int x = 0; x <= 9; x++)
            {
                File<< answerArray[x] << ", ";
            }
    }
    File.close();
/// /////////////////////////////////////////////////////////////////

cout << "\nThe Answers are: \n";
displayArray(answerArray, size);
cout << "\nThis is the words array: \n";
displayArray(getwordsArray, size);
}
Last edited on
Topic archived. No new replies allowed.