#include <iostream>
#using namespace std;
#include <cstring>
#include <fstream>
int main(int argc, char *argv[] )
{
ifstream file;
// check for valid user input
if (argc != 2)
{
cout << "Please enter the file name." << endl;
return 0;
}
else
{
file.open (argv[1]);
// check if file is valid
if (file == 0)
{
cout << "File is not valid." << endl;
return 0;
}
}
char words[100][16]; // 2d array for unique words
char line[100]; // c-string to hold a line
char *token = NULL;
int h=0; // variable for counting array index
int i, j; // counter variables
while (file.getline (line, 100, '\n'))
{
bool unique = true; // boolian to test for unique words
cout << line << endl; // echo print the line
token = strtok(line, " ."); // 1st token
if (token == NULL) // check if 1st token exists
{
break;
}
// loop to check if token is unique
for (i = 0; i < 100; i++)
{
if (strcmp(token, words[i]) == 0)
{
unique = false;
break;
}
}
if (unique == true)
{
strcpy(words[h], token);
h++;
}
unique = false;
// another loop to continue strtok and check for unique words
while (token != NULL)
{
token = strtok(NULL, " .");
for (i = 0; i < 100; i++)
{
if (strcmp(token, words[i]) == 0)
{
unique = false;
break;
}
}
if (unique == true)
{
strcpy(words[h], token);
h++;
}
}
}
return 0;
}
This is my code so far, all my characters should fit the array and the loops appear to be logically sound. I don't understand why my program compiles but doesn't run correctly. I am guessing that it might have something to do with 2 dimensional arrays and the syntax I chose to use for strcmp and strcpy. But I tried putting words[h][0] instead of words[h] and that doesn't work either. I'm kind of at a complete loss here, please help!