im working on an encryption program and i need some help. so far it words fine except when i enter a space. when this happens it seems to end the loop and spit out the first word. any help would be great

#include <iostream>
#include <string>
using namespace std;


string rot13(string input)
{
	int intVal = 0;
	int length = input.size();
	int charPosition = 0;
	string finalText = "";
	string character;
	
	while(charPosition != (length + 1))
	{

		if(input[charPosition] >= 'A' && input[charPosition] <= 'M')
		{
			intVal = input[charPosition] + 13;
			character = char(intVal);
			finalText = finalText + character;
		}
	
		if(input[charPosition] >= 'a' && input[charPosition] <= 'm')
		{
			intVal = input[charPosition] + 13;
			character = char(intVal);
			finalText = finalText + character;
		}
	
		if(input[charPosition] >= 'N' && input[charPosition] <= 'Z')
		{
			intVal = input[charPosition] - 13;
			character = char(intVal);
			finalText = finalText + character;
		}
	
		if(input[charPosition] >= 'n' && input[charPosition] <= 'z')
		{
			intVal = input[charPosition] - 13;
			character = char(intVal);
			finalText = finalText + character;
		}

		if(input[charPosition] >= ' ' && input[charPosition] <= '@')
		{
			intVal = input[charPosition] + 0;
			character = char(intVal);
			finalText = finalText + character;
		}

		charPosition = charPosition + 1;
	
	}

	return finalText;

}

How are you reading in the input string? If you use cin >> str; all you're going to get is one word of input.

Use getline( cin, str ); to read in multiple word strings.

Also, your loop control while(charPosition != (length + 1)) is an OBO situation.

thanks it works alot better now but what do you obo situation and what would be a better way of controlling it?

Length of string is count of the characters in it. For example "string" has length 6. The last character position you can access is at index 5 - counting starts at 0 and goes to length-1. You start correctly, but your code (attempts) to process one character past the end of the actual string content.
If you were using C-style (char array based) strings, that one char too far would be the NULL ( '\0', value 0). In a C++ string, that position is not defined to hold any value. In practice, I find most compilers put a NULL after the last valid character, but you can't count on that.

A more common way to write that loop would be while(charPosition < length ) FYI: OBO means Off By One

//....
if(isspace(input[charPosition]))
//--or input[charPosition] == char(/*decimal number for space*/) 
&& input[charPosition] <= '@')
		{
			intVal = input[charPosition] + 0;
			character = char(intVal);
			finalText = finalText + character;
		}
//....

Use else if instead of several ifs in the loop. Why you check redundant conditions after if (... true ...)?

thanks...

//....
      if(isspace(input[charPosition])
      //--or input[charPosition] == char(/*decimal number for space*/)
      && input[charPosition] <= '@')
      {
      intVal = input[charPosition] + 0;
      character = char(intVal);
      finalText = finalText + character;
      }
      //....
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.