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!
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
usingnamespace 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");
}
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
usingnamespace 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
}