To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here





 Home

News

Forums

Hardware

CPUs

Motherboards

Video

Guides

CPU Prices

RAM Prices

Shop



Sharky Extreme : Forums:


Go Back   Sharky Forums > General Hardware/Software > Programming, Coding, and Scripting

Programming, Coding, and Scripting Need help with questions about programming, coding, or scripting? Here's the place to ask for it.

Reply
 
Thread Tools Search this Thread Display Modes
Old 06-27-2001, 07:39 PM   #1
uNr34l_w0m4N
Goldfish
 
Join Date: May 2001
Location: Lost in my mind... Or is that Lost my mind?
Posts: 69
Post 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?
uNr34l_w0m4N is offline   Reply With Quote
Old 06-27-2001, 07:50 PM   #2
Strogian
Hammerhead Shark
 
Join Date: Feb 2001
Posts: 1,612
Post

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? =)
Strogian is offline   Reply With Quote
Old 06-27-2001, 07:55 PM   #3
uNr34l_w0m4N
Goldfish
 
Join Date: May 2001
Location: Lost in my mind... Or is that Lost my mind?
Posts: 69
Post

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?
uNr34l_w0m4N is offline   Reply With Quote
Old 06-27-2001, 07:58 PM   #4
uNr34l_w0m4N
Goldfish
 
Join Date: May 2001
Location: Lost in my mind... Or is that Lost my mind?
Posts: 69
Post

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?
uNr34l_w0m4N is offline   Reply With Quote
Old 06-27-2001, 10:21 PM   #5
Zoma
Tiger Shark
 
Join Date: Mar 2001
Posts: 545
Post

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:
CPU: Athlon 64 X2 4400+
Mobo: Asus A8N-SLI Premium
RAM: 1024 MB Corsair ValueSelect DDR 3200
Video: BFG 7800 GTX OC
Sound: SBLive! mp3+ (waiting for X-Fi)
OS: Windows XP Home Edition
Zoma is offline   Reply With Quote
Old 06-27-2001, 11:06 PM   #6
uNr34l_w0m4N
Goldfish
 
Join Date: May 2001
Location: Lost in my mind... Or is that Lost my mind?
Posts: 69
Post

Thank you so much for your help. Along with your help and others', I finally got it working

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

------------------
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?
uNr34l_w0m4N is offline   Reply With Quote
Old 06-28-2001, 12:23 AM   #7
Conrad Song
Reef Shark
 
Join Date: Oct 2000
Posts: 397
Post

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);
}
Conrad Song is offline   Reply With Quote
Reply

Bookmarks
Go Back   Sharky Forums > General Hardware/Software > Programming, Coding, and Scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 03:08 PM.




Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.