Issue with signed/unsigned mismatch error on "<" operator

Ok so i hit a new problem trying to move my code for shifting the letters into a function. It gives me a signed/unsigned mismatch error on the "<" operator inside the charShift function. The code works just fine regularly, but somewhere in the transition to a function, it didn't like it and I made sure the changes were equal in expected results (minus the error i am wondering about).

Here's my code without a function (works perfectly)

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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	//Declaring the variables necessary to open the file
	ifstream inFile;
	string inputFileName;
	string reply;

	int AlphaCount[26] = {0};
	string line;
	char letter;
	int index = 0;

	//Prompt user to enter file
	cout << "Input file name: ";
	getline(cin, inputFileName);

	// Open the input file
	inFile.open(inputFileName.c_str());
    
	// Check the file opened successfully
	if (! inFile.is_open())
	{
		cout << "Unable to open input file." << endl;
		cout << "Press enter to continue...";
		getline(cin, reply);
		exit(1);
	}

	//Echo file onto screen
	while (inFile.peek() != EOF)
	{
		getline(inFile, line);
		cout << line << endl;
	}
	
	//Clear and rewind.
	inFile.clear();
	inFile.seekg(0);

	while (inFile.peek() != EOF)
	{
		//Extracting a line from the text file
		getline(inFile, line);
		//Extracting a character from the line one at a time and analyzing it
		for(int i = 0; i < line.length(); i++)
		{
			letter = line.at(i);
			for(int j = 0; j < 26; j++)
			{
				if(letter == (97 + j) || letter == (65 + j))
				{
					AlphaCount[j]++;
				}
			}
		}
	}
	getchar();

	cout << endl << endl << endl;
	
	//Outputting all the slot values
	for(int x = 0; x < 26; x++)
	{
		cout << AlphaCount[x] << " ";
	}

	cout << endl << endl << endl;

	//Checking to see which slot is the largest
	index = 0;
	for(int x = 1; x < 26; x++)
	{
		if(AlphaCount[x] > AlphaCount[index])
		{
			index = x;
		}
	}
	cout << index;

	//E is the 4th slot (5th character) in the array
	index = index - 4;

	inFile.clear();
	inFile.seekg(0);

	while (inFile.peek() != EOF)
	{
		//Extracting a line from the text file
		getline(inFile, line);
		//Extracting a character from the line one at a time and analyzing it
		for(int i = 0; i < line.length(); i++)
		{
			letter = line.at(i);
			//Ignoring spaces, commas, and periods when converting
			if(letter >= 65 && letter <= 90 || letter >=97 && letter <= 122)
			{
				//Checking for uppercase characters and wrapping as needed
				if((letter - index) >= 65 && letter <= 90)
				{
					letter = letter - index;
				}
				else if((letter - index) < 65 && letter <= 90)
				{
					letter = 90 - (64 - (letter - index));
				}

				//Checking for lowercase characters and wrapping as needed
				if((letter - index) >= 97 && letter <= 122)
				{
					letter = letter - index;
				}
				else if((letter - index) < 97 && letter <= 122 && letter >= 97)
				{
					letter = 122 - (96 - (letter - index));
				}
			}
			//If it is a period, comma, or whitespace
			else
			{
				//Do nothing
			}
			cout << letter;
		}
		cout << endl;
	}

	getchar();
}


And here's my code with the function being used (doesn't work)

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 <iomanip>
#include <string>
#include <fstream>

using namespace std;

int charShift(ifstream inFile, char letter, string line, int index);

int main()
{
	//Declaring the variables necessary to open the file
	ifstream inFile;
	string inputFileName;
	string reply;

	int AlphaCount[26] = {0};
	string line;
	char letter;
	int index = 0;

	//Prompt user to enter file
	cout << "Input file name: ";
	getline(cin, inputFileName);

	// Open the input file
	inFile.open(inputFileName.c_str());
    
	// Check the file opened successfully
	if (! inFile.is_open())
	{
		cout << "Unable to open input file." << endl;
		cout << "Press enter to continue...";
		getline(cin, reply);
		exit(1);
	}

	//Echo file onto screen
	while (inFile.peek() != EOF)
	{
		getline(inFile, line);
		cout << line << endl;
	}
	
	//Clear and rewind.
	inFile.clear();
	inFile.seekg(0);

	while (inFile.peek() != EOF)
	{
		//Extracting a line from the text file
		getline(inFile, line);
		//Extracting a character from the line one at a time and analyzing it
		for(int i = 0; i < line.length(); i++)
		{
			letter = line.at(i);
			for(int j = 0; j < 26; j++)
			{
				if(letter == (97 + j) || letter == (65 + j))
				{
					AlphaCount[j]++;
				}
			}
		}
	}
	getchar();

	cout << endl << endl << endl;
	
	//Outputting all the slot values
	for(int x = 0; x < 26; x++)
	{
		cout << AlphaCount[x] << " ";
	}

	cout << endl << endl << endl;

	//Checking to see which slot is the largest
	index = 0;
	for(int x = 1; x < 26; x++)
	{
		if(AlphaCount[x] > AlphaCount[index])
		{
			index = x;
		}
	}
	cout << index;

	//E is the 4th slot (5th character) in the array
	index = index - 4;

	inFile.clear();
	inFile.seekg(0);

	while (inFile.peek() != EOF)
	{
		charShift(inFile, letter, line, index);
		cout << letter;
	}

	getchar();
}


int charShift(ifstream inFile, char letter, string line, int index)
{
	//Extracting a line from the text file
	getline(inFile, line);
	//Extracting a character from the line one at a time and analyzing it
	for(int i = 0; i < line.length(); i++)
	{
		letter = line.at(i);
		//Ignoring spaces, commas, and periods when converting
		if(letter >= 65 && letter <= 90 || letter >=97 && letter <= 122)
		{
			//Checking for uppercase characters and wrapping as needed
			if((letter - index) >= 65 && letter <= 90)
			{
				letter = letter - index;
			}
			else if((letter - index) < 65 && letter <= 90)
			{
				letter = 90 - (64 - (letter - index));
			}

			//Checking for lowercase characters and wrapping as needed
			if((letter - index) >= 97 && letter <= 122)
			{
				letter = letter - index;
			}
			else if((letter - index) < 97 && letter <= 122 && letter >= 97)
			{
				letter = 122 - (96 - (letter - index));
			}
		}
		//If it is a period, comma, or whitespace
		else
		{
			//Do nothing
		}
		return letter;
	}
	cout << endl;
}
Can you somehow highlight and/or make extremely obvious what parts of the code don't work/are changed? It will make it easier for those of us who like to help so we don't have to search through your entire code to find what is wrong.

Thank you!
I underlined the spot where the issue is in line 6 of the code below. Sorry bout that!

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
int charShift(ifstream inFile, char letter, string line, int index)
{
	//Extracting a line from the text file
	getline(inFile, line);
	//Extracting a character from the line one at a time and analyzing it
        for(int i = 0; i < line.length(); i++)
	{
		letter = line.at(i);
		//Ignoring spaces, commas, and periods when converting
		if(letter >= 65 && letter <= 90 || letter >=97 && letter <= 122)
		{
			//Checking for uppercase characters and wrapping as needed
			if((letter - index) >= 65 && letter <= 90)
			{
				letter = letter - index;
			}
			else if((letter - index) < 65 && letter <= 90)
			{
				letter = 90 - (64 - (letter - index));
			}

			//Checking for lowercase characters and wrapping as needed
			if((letter - index) >= 97 && letter <= 122)
			{
				letter = letter - index;
			}
			else if((letter - index) < 97 && letter <= 122 && letter >= 97)
			{
				letter = 122 - (96 - (letter - index));
			}
		}
		//If it is a period, comma, or whitespace
		else
		{
			//Do nothing
		}
		return letter;
	}
	cout << endl;
}
Last edited on
@BlackDahlia1147

On line 97, you don't do anything with the return value from the charShift function, so the code in the function doesn't achieve anything at all.

One further opinion that I have :

Your code seems very much like C code but with some cout statements. I know that people new to C++ go through this phase (I did, briefly), if you can try to learn "The C++ way" rather than "The C way".

The "C way" seems to involve lots of parsing char by char, or using the rather limited C Functions .

Is this for your assignment, and are you allowed to use the STL algorithms?

It's just that the string class has all kinds of functionality, there are various find algorithms, and functions that make this job much easier.

Hope all goes well.

EDIT: If you learn how to use a debugger, it will be much easier to solve runtime problems. You can step through the code 1 line at a time, keep an eye on the values of the variables, deduce where it all goes wrong.
Last edited on
in regards to the return not doing anything in line 97, it returns the letter and then displays that letter. Should I have the function as a char variable instead of int? And yes, this is for a class and i dont know any methods for coding besides what i have used here. Never heard of the STL algorithms or really any algorithms in coding :/
@BlackDahlia1147

I think you misunderstand what I said.

95
96
97
98
99
	while (inFile.peek() != EOF)
	{
		charShift(inFile, letter, line, index);
		cout << letter;
	}


Line 97 doesn't assign the value returned by the function charShift. The value of the variable letter on line 98 is not changed. You could do one of these instead :

1.
cout << charShift(inFile, letter, line, index);

2. Make charShift a void function, but send it a reference or pointer to the letter variable. That way the value of the letter variable in main() will be changed, and you won't need to change lines 97 &98.

BlackDahlia1147 wrote:
And yes, this is for a class and i dont know any methods for coding besides what i have used here. Never heard of the STL algorithms or really any algorithms in coding :/


That is fine for now, if you are interested there is a wealth of info in the reference section (including examples) & a tutorial in the documentation section.

Scott Meyers (An author of some really good C++ books) describes C++ as a federation of 4 languages:

1. C with it's basic data types int, double, char, pointers etc. and flow control - if-else-if-else, loops, switch etc;
2. C++ with it's classes, Object Oriented - polymorhism, inheritance etc, function & operator overloading plus much more;
3. Template C++ - generic programming, this allows (really basically) one to write a function once that will work with a multitude of types . E.g std::abs works for any type int, doubles, floats. C has a different function for each of these ;
4. STL - Standard Template Library. This has containers like string, vector, list, map etc. They have constructors and member functions, overloaded operators, and one can use algorithms like std::find on them.

I am not quoting him directly here, just the 4 items, I put my own very brief & incomplete descriptions.

Hope this helps a bit.

Edit: I stuffed up item number 3 - that is function overloading. Better off with a link to describe them :

http://www.cplusplus.com/doc/tutorial/templates/


Last edited on
@TheIdeasMan
thanks a lot for your help! That is a good tip for my future assignments with just using cout << charShift(inFile, letter, line, index); Ill be sure to keep it in mind as i move forward!
No worries, I guess the thing here is to understand what happens when you call a function, and how it's return value works. Also how pointers & references can be used instead of a return value.

Good Luck !!
Aw crap - your error mentioned in the title of your topic, is because the size function returns a type of std::size_t which is usually the largest unsigned integer type (unsigned long or unsigned long long ).

Your for loop uses an int, so hence the error. Try this instead :

1
2
3
for(std::size_t i = 0; i < line.length(); i++) { 
    // your code
}


So this is a lesson to always look up the reference for every function, container or algorithm that you use - don't assume :-)

http://www.cplusplus.com/reference/string/string/size/
Topic archived. No new replies allowed.