-
C++ errors
assignment: 4.4. Internet mailers and news-readers often contain a "rot13"
function that can be used to "encrypt" words by
rotating their characters 13 positions
(i.e., A becomes N, B becomes O, C becomes P, ... Z becomes M).
This is convenient, because the same function can be used
to both "encrypt" and "decrypt" a word:
rot13("ROMEO") == "EBZRB"
and
rot13("EBZRB") == "ROMEO"
Write a Rot13() function that, given a string,
returns a string whose characters are those of the first string
rotated 13 positions.
(Hint: You may find the % operator to be useful.)
my program thus far and its error messages can be found at
http://www.spaghettibasgetti.net/rot13error.txt
If anyone can help me, I will be eternally greatful. Thank you https://www.sharkyforums.com/images/.../2005/06/5.gif
------------------
I lost my toe the other day...
then I looked down at my foot and found he'd come back to me.
Guess I'll think twice about putting toes in my mouth,eh?
-
void main()
{
cout << "Input the sentence you want translated:";
string rot13 (string normalWords);
}
Is that how you call a function in C++? I've barely used C++ at all, but if that were C, I'd wonder why you put the "string" in front of rot13.
(btw, why aren't you using the % operator like it said? =)
-
Okay, I'm a dolt. I just figured out my mistake. hehe. thank you to anyone who tried. I realised that i needed to declare my variables inside the main function, rather than out in never never land... well, that took away the errors anyhow... anyone know why is was so wrong?
------------------
I lost my toe the other day...
then I looked down at my foot and found he'd come back to me.
Guess I'll think twice about putting toes in my mouth,eh?
-
Oh, no, the errors came back as soon as i defined rot locally in the second function.
Oh, Strogian, yes, that is how you call the functions in C++... i use string because i want letters rather than numbers, although i'm beginning to think i should just use int... [integer]
and i'm not using the % operator like it said because i can't think of how i could use it effectively. https://www.sharkyforums.com/images/.../2005/08/2.gif
------------------
I lost my toe the other day...
then I looked down at my foot and found he'd come back to me.
Guess I'll think twice about putting toes in my mouth,eh?
-
Time to once again over-help someone with an assignment https://www.sharkyforums.com/images/.../2005/06/5.gif
Code:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
string normalWords, rot;
void main()
{
cout << "Input the sentence you want translated:";
string rot13 (string normalWords);
}
That's a bit strange. Not sure if you were trying to do,
but here's what I think you wanted to do:
Code:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
// This is a prototype. You had yours inside the main program, which I'm not even
// sure is legal.
string rot13(string nomralWords);
void main()
{
// Declare the variable names as local variables.
// They were global before, meaning that they would
// be overwritten in your function. This is a bad
// thing https://www.sharkyforums.com/images/.../2005/06/5.gif
string normalsWords, rot;
cout << "Input the sentence you want translated:";
// You need to get the stuff https://www.sharkyforums.com/images/.../2005/06/5.gif
// I'm guessing normalWords is where you wanted to
// store the user input, and rot is where you
// wanted to store the rotated input
cin >> normalWords;
rot = rot13(normalWords);
}
That should all be koshar, now. I'm not sure about the
type "string," as I stay away from most C++ types https://www.sharkyforums.com/images/.../2005/06/5.gif
If it doesn't work, I can help you use char[length] and char* https://www.sharkyforums.com/images/.../2005/06/5.gif
Code:
string rot13 (string normalWords)
{
for (; https://www.sharkyforums.com/images/.../2005/06/6.gif
{
cin >> normalWords;
int i=0;
int over;
normalWords[i];
if ((normalWords[i] >= 65) && (normalWords[i]<=90))
{
rot = normalWords[i] + 16;
if (rot > 90);
{
over = (rot - 90);
rot = (65-1) + over;
}
return rot;
}
}
}
Ah, so that's where you read in normalWords. But a lot
Of that does not make sense. Why pass it in as a parameter
to the function if you're just going to read it in?
I'm guessing the function assumes that normalWords already has a value.
Also, you read in normalWords EVER iteration of the loop. But wait, the loop
returns after one character. Also, "normalWords[i];" does not make sense at
all. Maybe I'm wrong, but I don't think this will work.
My version:
Code:
string rot13(string someString)
{
// We need a variable to use in this function
string newString;
// Loop through the string.
// Not sure if strlen works on strings, so
// you'll have to fix it if it doesn't
for (int i = 0; i <= strlen(someString); i++)
{
// If the character is a valid letter, rotate it.
// Just copy any other character
if ((toupper(someString[i]) >= 'A') && (toupper(someString[i]) <= 'Z'))
{
// If the letter is between A and M,
// Add 13 to make it a letter between N and Z.
// Otherwise, do the inverse.
if (toupper(someString[i]) <= 13)
{
newString[i] = someString[i] + 13;
}
else
{
newString[i] = someString[i] - 13;
}
}
else
{
newString[i] = someString[i];
}
}
return newString;
}
Notice how I use two different variable names inside the function to avoid confusion.
I haven't tested this code, so I don't know if it will even compile. I'll
leave testing and fixing anything up to you https://www.sharkyforums.com/images/.../2005/06/5.gif
-
Thank you so much for your help. https://www.sharkyforums.com/images/.../2005/06/5.gif Along with your help and others', I finally got it working https://www.sharkyforums.com/images/.../2005/06/5.gif
Code:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
string rot13 (string normalWords);
void main()
{
string normalWords;
cout << "Input the sentence you want translated:";
cin >> normalWords;
cout << "\nThe rot-13 translation is: " << rot13(normalWords) << "\n\n\n";
}
string rot13 (string normalWords)
{
string rot = normalWords;
int i = 0;
for (i=0; i<normalWords.size(); i++)
{
rot[i]=normalWords[i];
normalWords[i];
if ((normalWords[i]>64) && (normalWords[i]< 91))
rot[i] =((normalWords[i]-65+13)%26)+65;
if ((normalWords[i]>96) && (normalWords[i]<123))
rot[i] =((normalWords[i]-97+13)%26)+97;
}
return rot;
}
The only problem is that it doesn't recognise 2 words.... which for the moment i'm going to play naive on, i do believe... hehe. The program was due like... 2 days ago. Or maybe just yesterday. I forget. The point is is that it's late, but I hope it's not too late. hehe.
THANK YOU for everything!!!
I believe that I will probably pop in here again soon. hehe. I always enjoyed the coding forum https://www.sharkyforums.com/images/.../2005/06/5.gif
------------------
I lost my toe the other day...
then I looked down at my foot and found he'd come back to me.
Guess I'll think twice about putting toes in my mouth,eh?
-
Should work nicely (caps only):
Code:
string rot13( string s )
{
static const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for( int i = 0; i < s.length(); ++i ) {
s[i] = alphabet[(s[i]-'A'+13)%alphabet.length()];
}
return(s);
}