|
-
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 
------------------
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?
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?
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. 
------------------
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?
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 
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
string normalsWords, rot;
cout << "Input the sentence you want translated:";
// You need to get the stuff
// 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 
If it doesn't work, I can help you use char[length] and char* 
Code:
string rot13 (string normalWords)
{
for (;
{
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
System specs:
| Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
| Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
| Windows 7 Home 64-bit |
-
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);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|