Why does this C++ program print irrelevant characters?
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 c开发者_开发百科reate 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;
}
You are using != in place of = and also there is a missing ;
cipher[i]!='\0'
should be:
cipher[i]='\0';
精彩评论