Jump to content
  • Advertisement
Sign in to follow this  
bronxbomber92

Odd C++ string problems

This topic is 3789 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to do some simple string operations but I'm having all sorts of odd problems trying to set string equal to each other. Here are the errors:
Quote:
/Users/jedd**********/Rot13 Decypher/main.cpp: In function 'std::string Rot13Decypher(std::string)': /Users/jedd**********/Rot13 Decypher/main.cpp:19: error: invalid conversion from 'char' to 'const char*' /Users/jedd**********/Rot13 Decypher/main.cpp:19: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' /Users/jedd**********/Rot13 Decypher/main.cpp:23: error: invalid conversion from 'char' to 'const char*' /Users/jedd**********/Rot13 Decypher/main.cpp:23: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' /Users/jedd**********/Rot13 Decypher/main.cpp:29: error: invalid conversion from 'char' to 'const char*' /Users/jedd**********/Rot13 Decypher/main.cpp:29: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'
and the code..
#include <iostream>
#include <string>

using namespace std;

char abc[26] =
{
	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 
	'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 
	's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 
};

string Rot13Decypher( string cypher )
{	
	string decypher;
	
	for( int j = 0; j < cypher.length(); j++ )
	{
		string character = cypher[j]; // ERROR HERE
		cout << "character: " << character << endl;
		for( int i = 0; i < 25; i++ )
		{
			string letter = abc; // ERROR HERE
			cout << "Letter: " << letter << endl;
			if( character == letter )
			{
				int k = i + 13;
				if( k > 25 ) k -= 25; k--;
				string c = abc[k]; // ERROR HERE
				cout << "c: " << c << endl;
				decypher.append(c);
				break;
			}
			else
			{
				decypher.append(character);
				break;
			}
		}
	}
	
	return decypher;
}

...
What's the problem?

Share this post


Link to post
Share on other sites
Advertisement
You're not assigning strings, just chars. Try

string s;
s += c;

Edit:

On second thought where youre doing this

string character = cypher[j];
string letter = abc;
string c = abc[k];

why not just use the chars abc[k], abc, cypher[j] directly? and skip the three strings for your comparisions...

Share this post


Link to post
Share on other sites
0) For beginner questions, try For Beginners.

1) The elements of a string are characters, hence the compiler complaint.

2) That's way more complicated than it needs to be.


#include <iostream>
#include <string>
#include <algorithm> // std::transform
using namespace std;

char rot13_helper(char c) {
// Let's handle capital letters while we're at it.
if (c >= 'a' && <= 'z') {
return "nopqrstuvwxyzabcdefghijklm"[c - 'a'];
}
if (c >= 'A' && c <= 'z') {
return "NOPQRSTUVWXYZABCDEFGHIJKLM"[c - 'A'];
}
return c; // not a letter, so don't change it
}

string rot13(string s) {
transform(s.begin(), s.end(), s.begin(), rot13_helper);
return s;
}



Welcome to C++ :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
2) That's way more complicated than it needs to be.


char rot13_helper(char c) {
// Let's handle capital letters while we're at it.
if (c >= 'a' && <= 'z') {
return "nopqrstuvwxyzabcdefghijklm"[c - 'a'];
}
if (c >= 'A' && c <= 'z') {
return "NOPQRSTUVWXYZABCDEFGHIJKLM"[c - 'A'];
}
return c; // not a letter, so don't change it
}




<swine>Take care: this isn't entirely portable. It won't work if the character set is EBCDIC, for example.</swine>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!