Globalize a function variable

Sorry for bugging the forums so much, last minute coding never works well ;P
I cannot figure out how to make my bWords and cWord variable in my other functions understand that it will be defined with an earlier function.

And after that is cleared, the findeType function is having a problem too. I can sort out everything but an "error." An error would be two things happening to a word. like two letters swapping and one changing. For example: Name is the correct word the the entered word is Sale. Two things are changed but they are not swapped so there for its an Error. So how can i make the code see a swap different from a double letter change like above.

Correct word: NAME

Bad words:
NAMA (substituted)
NAMES (insert)
NAM (Deleted)
NEMA (Transposition)
SALE (Error)

My code is set to reference letters in both words and compare and that is what is messing up and saying its a Transposition over an Error.

Again, All comments are helpful :)




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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

enum eType {Sub, Trans, Del, Insert, Err, Corr};

void get_cWord(ifstream& fin, string cWord)
{
	fin >> cWord;
}

void get_bWords(ifstream& fin, string bWords)
{
	fin >> bWords;
}

void display(eType e)
{
	switch(e)
	{
	case Sub: cout << "User word is " << bWords << endl
				  << "The user word has one character substituted" << endl << endl; break;

	case Trans: cout << "User word is " << bWords << endl
					<< "The user word contains a transpostion" << endl << endl; break;

	case Del: cout << "User word is " << bWords << endl
				  << "The user word has one character delected" << endl <<; break;

	case Insert: cout << "User word is " << bWords << endl
					 << "The user word has one character inserted" << endl << endl; break;
	case Err: cout << "The user word is to bad to be a misspelling" << endl << endl; break;
	case Corr: cout << "The user word is Correct" << endl << endl; break;
	}
}


void findeType(eType e)
{
	int a = 0, x = 0, h = 0;
	int counter1 = 0, counter2 = 0, counter3 = 0;

	if(cWord == bWord)
	{
		a = cWord.size;

		for(int b = 0; b <= a; b++)
		{
			if(cWord.at(b) = bWords.at(b))
			{

			}else if(cWord.at(b) != bWords.at(b))
			{
				counter1++;
			}
		}

		if(counter1 = 0)
		{
			e = Corr;
		}else if(counter1 = 1)
		{
			e = Sub;
		}else if(counter1 = 2)
		{
			e = Trans;
		}

	}else if(cWord == bWord+1)
	{
		x = cWord.size + 1;

		for(int y = 0; y <= x; y++)
		{
			if(cWord.at(y) = bWords.at(y))
			{

			}else if(cWord.at(y) != bWords.at(y))
			{
				counter2++;
			}
		}

		if(counter2 = 1)
		{
			e = Del;
		}else if(counter2 <= 2)
		{
			e = Err;
		}

	}else if(cWord == bWord-1)
	{
		h = cWord.size;

		for(int i = 0; i <= h; i++)
		{
			if(cWord.at(i) = bWords.at(i))
			{

			}else if(cWord.at(i) != bWords.at(i))
			{
				counter3++;
			}
		}

		if(counter3 = 1)
		{
			e = Insert;
		}else if(counter3 <= 2)
		{
			e = Err;
		}
	}
}




void main()
{
	ifstream fin;
	string cWord = "", bWords = "";
	eType e = Corr;


	while(cWord != "/n")
	{
		get_cWord(fin, cWord);

		cout << "The word being checked is " << cWord << "." << endl << endl;

		while(bWords != "/n")
		{
			get_bWords(fin, bWords);
			findeType(e);
			display(e);
		}

	}

	system("pause");
}
I cannot figure out how to make my bWords and cWord variable in my other functions understand that it will be defined with an earlier function.

I suggest you read "passing by value" vs "passing by reference" in this case.

void get_cWord(ifstream& fin, string& cWord)
If you change your function to something like this it will be passed by reference (meaning the memory address is passed, so a copy doesn't need to be made) and it will give the value to the original variable .

All of your if statements should use the comparison operator ==, not the assignment =. See what problems you have after those errors have been fixed.
Maybe try passing the strings by reference?
This,
1
2
3
4
5
6
7
8
9
void get_cWord(ifstream& fin, string& cWord)
{
	fin >> cWord;
}

void get_bWords(ifstream& fin, string& bWords)
{
	fin >> bWords;
}

instead of this (what you had):
1
2
3
4
5
6
7
8
9
void get_cWord(ifstream& fin, string cWord)
{
	fin >> cWord;
}

void get_bWords(ifstream& fin, string bWords)
{
	fin >> bWords;
}

Not really sure of the problem.
Topic archived. No new replies allowed.