Caeser Code

Pages: 12
Well the function parameters MUST be a string and an int. That's required of me. But let's see if I got this right...

I have a string (caesar) and from this string I can have several chars, right? So how would I connect the chars to a particular string? Like if I had a program with several strings, how would I connect a char to a certain string? Or is that flexible? Would doing something like caesar[0] connect that string to the char?

And as for the ASCII values, those are already assigned with the letters and I don't need to do anything to declare that, right?

Is the * in "*cp" important?
Last edited on
You can always access and change the individual characters in a string.
1
2
std::string str = "Hello";
str[0] = 'J'; //str is now "Jello" 
Last edited on
OK that was a huge eye opener for me.

This is my code so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
bool EncyptString(std::string caesar, int shift)
{
  std::getline(std::cin, caesar);
  if(std::string::npos == caesar.find_first_of(!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")){
  for(std::size_t i = 0; i < caesar.length(); ++i){
    char &c = caesar[i];
  (((c-'a')+3)%26)+'a'}
  return true;
  std::cout << caesar << std::endl;
  }else{
  return false;
  }
}


Is it looking about right? I tried to compile it and it didn't work, but I'm curious if I'm even close to where I should be on this or if I'm way off.

Below is what the compiler gave me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$ make
/usr/bin/g++ -Wall -Wextra -g test.cpp  -o test
test.cpp: In function ‘bool EncyptString(std::string, int)’:
test.cpp:22:103: error: call of overloaded ‘find_first_of(bool)’ is ambiguous
   if(std::string::npos == caesar.find_first_of(!"abcdefghijklmnopqrstuvwxyzABCD           EFGHIJKLMNOPQRSTUVWXYZ")){
                                                                                                                  ^
test.cpp:22:103: note: candidates are:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/string:52:           0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/local           e_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/ios_b           ase.h:41,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ios:42,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ostream:38           ,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/iostream:3           9,
                 from test.cpp:8:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:1971:7: note           : std::basic_string<_CharT, _Traits, _Alloc>::size_type std::basic_string<_CharT           , _Traits, _Alloc>::find_first_of(const std::basic_string<_CharT, _Traits, _Allo           c>&, std::basic_string<_CharT, _Traits, _Alloc>::size_type) const [with _CharT =            char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::bas           ic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
       find_first_of(const basic_string& __str, size_type __pos = 0) const
       ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:2001:7: note           : std::basic_string<_CharT, _Traits, _Alloc>::size_type std::basic_string<_CharT           , _Traits, _Alloc>::find_first_of(const _CharT*, std::basic_string<_CharT, _Trai           ts, _Alloc>::size_type) const [with _CharT = char; _Traits = std::char_traits<ch           ar>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::           size_type = long unsigned int]
       find_first_of(const _CharT* __s, size_type __pos = 0) const
       ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:2020:7: note           : std::basic_string<_CharT, _Traits, _Alloc>::size_type std::basic_string<_CharT           , _Traits, _Alloc>::find_first_of(_CharT, std::basic_string<_CharT, _Traits, _Al           loc>::size_type) const [with _CharT = char; _Traits = std::char_traits<char>; _A           lloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::size_ty           pe = long unsigned int]
       find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
       ^
test.cpp:25:23: error: expected ‘;’ before ‘}’ token
   (((c-'a')+3)%26)+'a'}
                       ^
test.cpp:25:19: warning: statement has no effect [-Wunused-value]
   (((c-'a')+3)%26)+'a'}
                   ^
test.cpp: At global scope:
test.cpp:19:43: warning: unused parameter ‘shift’ [-Wunused-parameter]
 bool EncyptString(std::string caesar, int shift)
                                           ^
test.cpp: In function ‘bool EncyptString(std::string, int)’:
test.cpp:31:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
makefile:10: recipe for target 'test' failed
make: *** [test] Error 1
What is that if-statement on line 4 supposed to be doing?

You always need to loop through the entire string, but you need to check whether it's a lowercase letter or uppercase letter before changing it.
Last edited on
The if statement on line four is to ensure that nothing other than an alphabetical letter is entered. If something outside of the alphabet (1, 2, !, ?, %, etc.) is entered, it returns false. If the string only contains alphabetical letters, it returns true.

As for the loop, do you mean I need to create a different equation if someone enters "CODE" or "Code" instead of "code"?
You are focusing on one letter at a time. Lowercase letters and uppercase letters need slightly different treatment.
Yeah, I know I'm only focusing on one. I mean I need an if statement on if the letter is capital or not, right?
Yes.
1
2
3
4
5
6
7
8
if(c >= 'a' && c <= 'z')
{
    //...
}
else if(c >= 'A' && c <= 'Z')
{
    //...
}
Last edited on
Topic archived. No new replies allowed.
Pages: 12