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:
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");
}
|