Time to once again over-help someone with an assignment
That's a bit strange. Not sure if you were trying to do,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); }
but here's what I think you wanted to do:
That should all be koshar, now. I'm not sure about theCode:#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 // thingstring 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); }
type "string," as I stay away from most C++ types
If it doesn't work, I can help you use char[length] and char*
Ah, so that's where you read in normalWords. But a lotCode: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; } } }
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:
Notice how I use two different variable names inside the function to avoid confusion.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; }
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![]()





{
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;
}
}
}
Reply With Quote