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 开发者_如何学Cwill 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
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
精彩评论