Non threatening compile error?

In the book I purchased for a course I will be taking in January they gave me an excellent snippet that I can use for my typical "new coder wants to make an RPG despite it being a horrible first project for the begining coder".

The snippet essentially takes one's input string and converts it into uppercase letters. Saves the headache of multiple if statements of does input = 'y' or 'Y' or "YES" ect.

Anyhow here is the code:
1
2
3
4
5
6
7
8
9
string uppercase(string str)
{
	string result = str;
	for (int i = 0; i < result.length(); i++)
	{
		result[i] = toupper(result[i]);
	}
	return result;
}


And here is the error:

main.cpp(117): warning C4018: '<' : signed/unsigned mismatch

I prefer to not have compiler errors and warnings, and not sure what exactly happens. The code appears to work fine in my main program.

Should I worry about this particular warning?
Is there a way to get rid of it?

Update(while writing the post) = I added the word unsigned in front of int i =0 ,(the book does not say to do this...so I'm tooting my own horn) and the error went away. It does not appear to have any negative side effects.


The ShallonDark updated code:
1
2
3
4
5
6
7
8
9
string uppercase(string str)
{
	string result = str;
	for (unsigned int i = 0; i < result.length(); i++)
	{
		result[i] = toupper(result[i]);
	}
	return result;
}


Am I safe to proceed as if everything is fine and dandy?
Last edited on
What book ? I'm interested , PM me the title if you want. :)

So it's ok now ? LOL !
Yes, you are (unknowingly) writing better code than your book. The thing is, string::length() returns an unsigned int. Comparing int with unsigned int is not always safe, since the ranges they can store are different.

You should also pass your argument as a const reference, to avoid copying it twice as you're doing. And a STL-friendly way of doing this would be

 
std::transform(str.begin(), str.end(), result.begin(), std::toupper);
Last edited on
Thanks! Glad to know I figured it out on my own.

Heres another snippet for you guys to look at.

This one prints a box around the name of the room in my game. It will eventually be modified to display the room desc, the typical north, south, east, ect, current mobs in the area, ect. It calculates the "middle" of a string and places it in the "middle" of an 80 character box of stars.
I doubt it's the most efficient way of going about it, but I'm just curious if I'm on the right track to solving my various programming problems, or if I'm still including a lot of "Fluff" in my code.


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
[
/**
   Places the room name inside of a bar, and places the string in the middle.
   @param curr_room = The Input of the room_name from an array in the room class.
   @param length_variable = during runtime allows for an extra space to be added to the second
      line of " " characters to ensure the edges line up properly.
*/
void Player_Interface::display_room(string curr_room, int length_variable)
{
int room_length; //length of room name
int half_room_length; //half of room name
const int HALF = 2; // Eliminates magic number of 2
const int IN_OUT_STAR = 2; //Accounts for stars on outer edges of display
const int STAR_LENGTH = (80 - IN_OUT_STAR);// width of display box minus the inner and outer stars
int first_space_print; // how many spaces to print before string
   room_length = curr_room.length(); //calculates length of string
   half_room_length = room_length / HALF; //calculates midpoint of string
   first_space_print = (STAR_LENGTH / HALF) - half_room_length; // calculates 
                                                                //how many spaces to 
                                                                // print before printing string
int remainder = STAR_LENGTH - first_space_print - room_length - length_variable;
          // How many spaces to print after string


cout << "*******************************************************************************" << endl;
cout << "*";
   for (int i = 0; i < first_space_print; i++)
   {
      cout << " ";
   }
cout << curr_room;
   for (int x = 0; x < remainder; x++)
   {
      cout << " ";
   }
cout << "*" << endl;
cout << "*******************************************************************************" << endl;

}


My longterm goal is to be able to program my own mudcode base, but I know I've got a lot to learn until then, but if you know of any good resources/tutorials for the design process of text based RPG's that would be awesome as well. I still have troubles deciding where to include my attack function, do I put it in with my character, the monsters, a seperate combat class, does it even matter, and I would love to find some resources of such to look into.

Thanks much!
Last edited on
Topic archived. No new replies allowed.