views:

126

answers:

3

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;
}
+7  A: 

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

cipher[i]!='\0'

should be:

cipher[i]='\0';
codaddict
thanks i got it to work,
Eric
@Eric: Good to know that. You can mark the answer as accepted.
codaddict
+2  A: 

Not directly related to the question, but you really should consider using std::string instead of char[] in C++.

SurvivalMachine
This would make a nice comment; it does not make a nice answer.
James McNellis
A: 

no this wasn't my main code it was some what of an example i came up with.

Eric