Take the 2-minute tour ×

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<<"file opened";


      cout<<"No file exists"<<endl;;


        int a=0;
          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

//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
           }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


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

share|improve this question
How can you read your own code? The a variable is off by one, Alphabet[26] is not a valid index. –  Hans Passant Feb 22 '11 at 23:28
like I said, the formatting is wonky because I simply copy/pasted from emacs –  Tyler Pfaff Feb 23 '11 at 7:59

1 Answer 1

up vote 7 down vote accepted

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

share|improve this answer
That did the job, thanks so much! –  Tyler Pfaff Feb 23 '11 at 7:58
Oh and although it appeared it was only doing it sometimes(in the terminal), writing the characters to a file revealed that it was in fact happening all the time. –  Tyler Pfaff Feb 23 '11 at 7:59
Question though, why doesnt it just give an array out of bounds error? –  Tyler Pfaff Feb 25 '11 at 18:38
c/c++ do not have bounds checking since the [] operator is "only" some pointer arithmetic. If you want bounds checking you should use std::vector and the at() member function which does bounds checking. –  manol Feb 27 '11 at 8:59

Your Answer


