1

Hey i am new to programming in C++, and i get the hang of it but i got stuck on this one simple problem i am suppose to create a shift cipher using the letters A-Z and shifting them 3 places, i get everything but when i do my output i get extra letters that are unneeded like "|[|" i know i have to put a terminator and i did but doesn't seem to work. Heres my rough draft of my program.

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;

int main()
{
//char 
char caesar[]="THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
char cipher[255];
char lookup[26];
int key=3,i,index;

for(i=0;i<26;i++)
{
lookup[i]= static_cast<char>(65+i);
}
for(i=0;i<43;i++)
{
 if (caesar[i]>='A' && caesar[i]<='Z')
 {
  index= static_cast<int>(caesar[i])-65;
  cipher[i]=lookup[(index+key)%26];

 }
 else  

 cipher[i]=caesar[i];
}

//Null Terminator
cipher[i]!='\0'
cout<<cipher<<endl;



return 0;
}
codaddict
  • 445,704
  • 82
  • 492
  • 529
Eric
  • 11
  • 1
  • Is this your actual code? It doesn't compile in its current form (though it's just a missing `;` that prevents compilation). The line missing the `;` looks like it would also be the problem if this is your code, however. – eldarerathis Sep 17 '10 at 03:16
  • you don't need to use `static_cast` to convert from `char` to `int`. – Ben Voigt Sep 17 '10 at 03:19
  • no it was something i just came up with – Eric Sep 21 '10 at 03:03
  • Not directly related to the question, but you really should consider using std::string instead of char[] in C++. – SurvivalMachine Sep 17 '10 at 06:09

1 Answers1

8

You are using != in place of = and also there is a missing ;

cipher[i]!='\0'

should be:

cipher[i]='\0';
codaddict
  • 445,704
  • 82
  • 492
  • 529