i posted my program below. i aim making a decrypting file for rot13. or rotation 13 so im using the memory address to change the capital and small 'A' to 'M' into 'N' to 'Z' and vice versa by using their memory location. but i don't know how to put their memory location in my IF condition. pls help asap.

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

int main ()
{
    string str[100];
    string * strP;
    string * strP2;
    int i, size;
    ifstream myfile;
    myfile.open("PlainText.txt");
    if (myfile.is_open())
    {
        size = myfile.gcount();
        for (i=0;i<=size;i++)
        {
            strP=&str[i];
                    if (((strP>=0x41)&&(strP<=0x4D))||((strP>=0x61)&&(strP<=0x6D)))
            {
                strP2 = strP + 0x0D;
                cout<<strP2;
            }
            else
            {
                if (((strP>=0x4E)&&(strP<=0x5A))||((strP>=0x6E)&&(strP<=0x7A)))
                {
                    strP2 = strP - 0x0D;
                    cout<<strP2;
                }
                else cout<<*strP2;
            }
        }
        myfile.close();
    }
    cout<<"Unable to open file!";
    return 0;
}

your help will be much appreciated. tnx

Edited 5 Years Ago by WolfPack: Added Code Tags

I think you are confused, string str[100] is giving you an array of 100 std::strings. Is that what you want?

Bear in mind that characters are just 1-byte ints, so they can be added to or subtracted from:

char c = 'a';
c++;
std::cout<<c<<std::endl; //'b'
c+=2;
std::cout<<c<<std::endl;//'d'

Bearing also in mind that the elements of the string can be accessed by index:

std::string animal= "cat";
std::cout<<animal[0]<<std::endl; //'c'

(note that this is different from the array of strings that you had before where
str[0] would give you the first string and str[0][0] would give you the first character of the first string).

See if that gets you going in the right direction and post back with any difficulties.

im making str[100] because my input is dependent of what Plaintext.txt contains. anyways, can you please correct my error why strP and 0x41 (what i think memory location format is and 0x41 is for "A") cant be compared wherein strP is a pointer and 0x41 is a memory location. tnx

my input is dependent of what Plaintext.txt

Right, but do you really need 100 strings of indeterminant length to do so? I think you're confusing std::strings and C-strings.

0x41 is a memory location

Maybe what you are doing makes sense to you, but it doesn't make any sense to me. 0x41 is not a memory location, it is the hexadecimal equivalent to the integer which defines the letter 'A' in ASCII.

std::string mystr= "hello";
for (int i = 0;i<mystr.length();i++)
   if(mystr[i] > 0x41 && mystr[i]<0x4d)
        mystr[i] +=0x0D //character values, not the pointer value
   else
        etc.

Edited 5 Years Ago by jonsca: n/a

hahaha.. sorry to over complicate it. this is the idea i got where i think it's the easiest way to create rot13. can you please give me your idea of how to create a rot13 decryption program and getting input from a text file(where the text file contents dont necessarily be alphabetical in order, also, the program is case sensitive). i would appreciate knowing other's idea. thank you.

i think the 0x part dont work so i tried using "A" or "M". im a bit confused if it runs correctly to what im expecting. here it is

myfile.open("PlainText.txt");
        if (myfile.is_open())
           {
               getline(myfile, line);
               str[0]=line;
               for (i=0;i<=myfile.gcount();i++)
                {
                    if((str[i]>="A")||(str[i]<="M"))
                        {
                            str[i]+=13;
                            cout<<str[i];

                        }
                    else cout<<str[i];
                }
           }

So since you are using the string array, line 8 will have to look something like str[0][i] >='A' && str[0][i]<='M' since you want to compare characters. That's the ith character of the first string in your array. You also need && instead, since your values will be over that interval.

tnx alot. now, i got two questions. why do i need to make a two-dimensional array when comparing characters, and how do i iterate downwards? str-=13 makes an error.

this is my program at the moment. i tried eliminating the -13 part so that there will be no error, the inputs A/a - M/m didnt change where in fact, it must have. perhaps the +13 didnt increment to what i expected it would have done.

using namespace std;

int main ()
    {
        string str[30], line;
        int i;
        ifstream myfile;
        myfile.open("PlainText.txt");
        if (myfile.is_open())
           {
               getline(myfile, line);
               str[0]=line;
               for (i=0;i<=myfile.gcount();i++)
                {
                    if(((str[0][i]>='A')&&(str[0][i]<='M'))||((str[0][i]>='a')&&(str[0][i]<='m')))
                        {
                            str[i]+=13;
                            cout<<str[i];
                        }
                    else
                        {
                            if(((str[0][i]>='N')&&(str[0][i]<='Z'))||((str[0][i]>='n')&&(str[0][i]<='z')))
                                {
                                    str[i]=str[i]-13;
                                    cout<<str[i];
                                }
                            else cout<<str[i];
                        }
                        cout<<str[i];
                }
           }
        else cout<<"Unable to open file!";
        myfile.close();
        return 0;
    }

17 and 24, you're not getting the character, you're trying to do that to a whole string. Formulate it like you do in the if statement str[0][i] This is why having an array of strings is a bit confusing, as you're only using the first one anyway...

jons, only the capital A was rotated. the other characters remained the same.

if(((str[0][i]>='A')&&(str[0][i]<='M'))||((str[0][i]>='a')&&(str[0][i]<='m')))
                        {
                            str[0][i]+=13;
                            cout<<str[i];
                        }
                    else
                        {
                            if(((str[0][i]>='N')&&(str[0][i]<='Z'))||((str[0][i]>='n')&&(str[0][i]<='z')))
                                {
                                    str[0][i]-=13;
                                    cout<<str[i];

Don't use gcount in your loop, use str[0].length(). Turn your if/else construct into

if(it is A-M or a-m)
    add
    print
else if(it is N-Z or n-z)
    subtract
    print
else
    Print it out

The way you have it, it's too confusing.

Edited 5 Years Ago by jonsca: n/a

after changing into str[0].length(), i tried to change the COUT into cout<<str[0] instead of cout<<str. it actually worked! yes!!! wooohohoho.. hahaha.. thank you very much jons! thankyou!! thank you for your time. ill be making my other subject's project tomorrow. In our programming subject, i still need to do GPS to KML program, and something about serial port. I'll be posting in daniweb if i got problems then. THANK YOU again jons. I owe you alot. anyway, i got last question for me to fully understand the concept and so that i can answer our teacher's defence appropriately. i don't really understand why the 2dimensional array is to be used instead of using the 1dimensional. im a bit confused in that part.

It's not a two dimensional array.
It's an array of 100 strings from which you are using only the first one:

str[0]

You insisted on using

string str[100];

You could use

string str;

instead.

LOL because you wanted an array of strings.

std::string strarr[2];
strarr[0] = "hello"; //first string of the array
strarr[1] = "world"; //second string of the array
strarr[0][0] is 'h'
strarr[1][0] is 'w'
strarr[0][4] is 'o'

Just like if

std::string str1 = "hello";
str1[0]  is 'h'
str1[4] is 'o'

You have to index into the string you want in the array, and then index into that string to get the character you want.

ahhh okkk. i get it.. tnx alot jons.. till next time.. bye... tnx..!!

This question has already been answered. Start a new discussion instead.