为什么这个 C++程序打印不相关的字符?
嘿,我是 C++ 编程新手,我掌握了它的窍门,但我陷入了这个简单的问题,我想使用字母 AZ
创建一个移位密码,并将它们移动 3 个位置,我得到了一切,但是当我输出时,我得到了不需要的额外字母,例如 "|[|"
我知道我必须放置一个终止符,我确实这样做了,但似乎不起作用。这是我的程序的草稿。
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用 != 代替 =,并且还缺少 ;
应该是:
You are using != in place of = and also there is a missing ;
should be: