Empty Command Line Issue

So I need to add a character to the end and beginning of a string read into the cmd line and compare that to the word file that is a dictionary set of all words possible. I have the logic for that done.

My problems: When nothing is entered to the cmd line than the program simply closes
I tried to fix this when initializing the string "guess" at the beginning with an if-else statement but it didn't help.

I get an out of bounds for my subscript but the program still runs after ignoring it.

Any help would be much appreciated

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
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <locale>
#include <algorithm>
#include <set>

using namespace std;

int main (int argc, char *argv[])
{
	fstream dictFile;
	dictFile.open("words.txt", fstream::in);
	std::set<string> Dictonary;
	string word, guess;

	while (getline(dictFile,word))
	{
    Dictonary.insert(word);
	}

	if(strlen(argv[1]) != 0)
	{
	guess[strlen(argv[1])+2 ];
	guess+= ' ';
	guess+= argv[1];
	guess+= ' ';
	}
	else
	{
		guess[2];
		guess+= ' ';
		guess+= ' ';
	}


	for(int i = 0; i != guess.length()-1; i++)
		{
			guess[i] = tolower(guess[i]);
		}

	for(char letter1='a'; letter1 != '{'; letter1++)
	{
		guess[0]=letter1;

		if(Dictonary.find(guess) != Dictonary.end())
			cout<< guess <<endl;

		for(char letter2='a'; letter2 != '{'; letter2++)
		{
			guess[(guess.length() - 1)]=letter2;
			if(Dictonary.find(guess) != Dictonary.end())
				cout<< guess <<endl;
		}
	}


	system("PAUSE");
When the command line is empty, the value of argc will be 1. In that case, looking at the contents of argv[1] makes no sense, as it doesn't exist.
So first check argc == 2
Topic archived. No new replies allowed.