为什么我的程序打印出空终止符?
当我运行它时,它有时会打印出一个空终止字符。大多数时候它会,并且可能有 1/5 的时候它会只打印字符。
void cryptogram::Encrypt(){
cout<<"encrypt"<<endl;
char Alphabet[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
fin.open("original.txt");
cout<<"file opened";
if(!fin)
{
cout<<"No file exists"<<endl;;
}else{
while(fin>>tempS){
len=tempS.length();
int a=0;
for(int j=0;j<len;j++){
for(a;a<=26;a++){
tempS[j]=toupper(tempS[j]);
if(tempS[j]!=Alphabet[a]){//while two characters arent equal
//if the characters arent equal...a is incremented, to see if the character from the word is equal to the next letter in the alphabet
}else{
cout<<Crypto[a];
//crypto is an array of char filled with letters that are used to encrypt the message
if(j!=len){ //if the word still has more characters
j++;
a=0;
}else{ //if the word is done being scanned
cout<<" ";
}
}
}
}
}
}
}
就是这样,这是有时打印的相应预期输出
xvk bkikhxlr wggbtfkj wiylekgbdhx wjjm HKO wigbtubxt xvk iwhj uedjkm glctb gvrmdiwhj iebbdielmeggtbx ctb xvtmk gbtubxvk wjjdxdthgbtubodll khvxvk imkbfdik xt xvk bkudth whj gbtfdjk HKO tgxdthm whj tggtbxehdxdkm ctb mxejkhxmibdzdhtltur whj pemxdik mxejdkm mxdh cok wbk wlmt gbkgctb cteb HKO zdh cgvrmdikjeiwhj qdhkmdtlturzzkjdydtivkzdmxbrw zdh zdjjlkkjeiwhj w jtixtbdh kjeiwjzdhdmxbittgkbodxv mjme whj eimj
这是通常打印的内容
xvkÈ bkikhxlrÈ wggbtfkjÈ wiylekgbdhxÈ wjjmÈ hkoÈ wigbtubxtÈ xvkÈ iwhjÈ uedjkmÈ glctbÈ gvrmdiwhjÈ iebbdielmeggtbxÈ ctbÈ xvtmkÈ gbtubxvkÈ wjjdxdthgbtubodllÈ khvxvkÈ imkbfdikÈ xtÈ xvkÈ bkudthÈ whjÈ gbtfdjkÈ hkoÈ tgxdthmÈ whjÈ tggtbxehdxdkmÈ ctbÈ mxejkhxmibdzdhtlturÈ whjÈ pemxdikÈ mxejdkmÈ mxdhÈ cokÈ wbkÈ wlmtÈ gbkgctbÈ ctebÈ hkoÈ zdhÈ cgvrmdikjeiwhjÈ qdhkmdtlturzzkjdydtivkzdmxbrwÈ zdhÈ zdjjlkkjeiwhjÈ wÈ jtixtbdhÈ kjeiwjzdhdmxbittgkbodxvÈ mjmeÈ whjÈ
或每个单词末尾奇数字符的某种变体
这是密码数组顺便填充的内容
wyijkcuvdpqlzhtgabmxefonrs
这也是加密的原始消息
最近批准的学术蓝图为校园添加了新的学术项目,并指导规划这些项目的物质和课程支持。这些额外的项目将增强该地区的校园服务,并为学生提供新的选择和机会。犯罪学和司法研究于 2003 年秋季开始。我们还准备在 2004 年秋季开设四个新专业——体育教育和运动机能学、大众传媒、生物化学、中等教育硕士和教育管理博士学位(与圣地亚哥州立大学和加州大学圣地亚哥分校)。
PS 抱歉,格式很奇怪,只是从终端复制并粘贴,尝试修复它
When I run this, it will SOMETIMES print out a null termination character. Most of the time it will, and probably 1/5 times it will print just the characters.
void cryptogram::Encrypt(){
cout<<"encrypt"<<endl;
char Alphabet[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
fin.open("original.txt");
cout<<"file opened";
if(!fin)
{
cout<<"No file exists"<<endl;;
}else{
while(fin>>tempS){
len=tempS.length();
int a=0;
for(int j=0;j<len;j++){
for(a;a<=26;a++){
tempS[j]=toupper(tempS[j]);
if(tempS[j]!=Alphabet[a]){//while two characters arent equal
//if the characters arent equal...a is incremented, to see if the character from the word is equal to the next letter in the alphabet
}else{
cout<<Crypto[a];
//crypto is an array of char filled with letters that are used to encrypt the message
if(j!=len){ //if the word still has more characters
j++;
a=0;
}else{ //if the word is done being scanned
cout<<" ";
}
}
}
}
}
}
}
So that's it and this is the corresponding EXPECTED output that is printed SOMETIMES
xvk bkikhxlr wggbtfkj wiylekgbdhx wjjm
hko wigbtubxt xvk iwhj uedjkm glctb
gvrmdiwhj iebbdielmeggtbx ctb xvtmk
gbtubxvk wjjdxdthgbtubodll khvxvk
imkbfdik xt xvk bkudth whj gbtfdjk hko
tgxdthm whj tggtbxehdxdkm ctb
mxejkhxmibdzdhtltur whj pemxdik
mxejdkm mxdh cok wbk wlmt gbkgctb cteb
hko zdh cgvrmdikjeiwhj
qdhkmdtlturzzkjdydtivkzdmxbrw zdh
zdjjlkkjeiwhj w jtixtbdh
kjeiwjzdhdmxbittgkbodxv mjme whj eimj
This is what normally prints though
xvkÈ bkikhxlrÈ wggbtfkjÈ wiylekgbdhxÈ
wjjmÈ hkoÈ wigbtubxtÈ xvkÈ iwhjÈ
uedjkmÈ glctbÈ gvrmdiwhjÈ
iebbdielmeggtbxÈ ctbÈ xvtmkÈ gbtubxvkÈ
wjjdxdthgbtubodllÈ khvxvkÈ imkbfdikÈ
xtÈ xvkÈ bkudthÈ whjÈ gbtfdjkÈ hkoÈ
tgxdthmÈ whjÈ tggtbxehdxdkmÈ ctbÈ
mxejkhxmibdzdhtlturÈ whjÈ pemxdikÈ
mxejdkmÈ mxdhÈ cokÈ wbkÈ wlmtÈ
gbkgctbÈ ctebÈ hkoÈ zdhÈ
cgvrmdikjeiwhjÈ
qdhkmdtlturzzkjdydtivkzdmxbrwÈ zdhÈ
zdjjlkkjeiwhjÈ wÈ jtixtbdhÈ
kjeiwjzdhdmxbittgkbodxvÈ mjmeÈ whjÈ
eimj
or some variation of an odd character at the end of each word
This is what the cryptogram array is filled with by the way
wyijkcuvdpqlzhtgabmxefonrs
Also this is the original message that gets encrypted
The recently approved Academic Blueprint adds new academic programs to the campus and guides planning for physical and curricular support for those programs. The additional programs will enhance the campus service to the region and provide new options and opportunities for students. Criminology and justice studies starts in fall 2003. We are also preparing for four new majors in fall 2004 - physical education and kinesiology, mass media, biochemistry, a master’s in middle-level education and a doctorate in educational administration (in cooperation with SDSU and UCSD).
P.S. Sorry about the weird formatting, just copied and pasted from the terminal, tried to fix it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在从 0 到 26(含)进行迭代,但 Alphabet 仅包含 26 个元素,因此您有时会读取超出数组边界的内容。这可能会导致 NULL 字符或存储在此内存位置的任何其他内容。您应该将 for 循环限制为 <=25 或 <26
You are iterating a from 0 to 26 (inclusive) but Alphabet contains only 26 elements, so you sometimes read beyond the array boundary. This can result in a NULL character or anything else that is stored in this memory location. You should limit your for-loop to <=25 or <26