Sunday, August 2, 2009

One more c++ question (hopefully last)?

ok so my program seems to work now, the only thing that doesnt quite work is the output that prompts the user to input an encryption or decription


heres how my program goes


int option;


cout%26lt;%26lt;"Enter 1 to encrypt, 2 to decrypt, 3 to quit"%26lt;%26lt;endl;


cin%26gt;%26gt;option;


while (option!=3){


if (option==1) cout%26lt;%26lt;"Enter a message to encrypt (! to quit)"%26lt;%26lt;endl;


char letter;


cin%26gt;%26gt;letter;


while (letter!='!'){


a bunch of if statements


}


if (option==2) cout%26lt;%26lt;"Enter a message to decrypt (0 to quit)"%26lt;%26lt;endl;


rest of program isnt relevant


when i run the program if i choose to input a 2 in the initial prompt it will not output the "Enter a message to decrypt (0 to quit)"


what do i need to do so that it will?

One more c++ question (hopefully last)?
I'd try using an else if statement, but here's the real problem, you need to encase everything after the if (option == 1) statement to the if(option ==2) in brackets, or it will start off from the cin%26gt;%26gt;letter statement even if you choose 2.
Reply:I first write your program in indented mode then tell you what is wrong with it. you too try to use indentation in writing your codes.


and I also write line numbers to which i refer in my explanation.


/*************************************...


1:int option;


2:


3:cout%26lt;%26lt; "Enter 1 to encrypt, 2 to decrypt, 3 to quit"%26lt;%26lt; endl;


4:cin%26gt;%26gt;option;


5:


6:while (option!=3)


7:{


8: if (option==1)


9: cout%26lt;%26lt;"Enter a message to encrypt (! toquit)"


10: %26lt;%26lt;endl;


11:char letter;


12:cin%26gt;%26gt;letter;


13:while (letter!='!')


14:{


15: a bunch of if statements


16:}


17:if (option==2)


18: cout%26lt;%26lt; "Enter a message to decrypt (0 to quit)"%26lt;%26lt; endl;


/*************************************...


WHY YOUR PROGRAM GOES WRONG !


first your program read in the option variable and if it is not 3 enters the while loop.


consider you enter 2. Now the value of option equals 2.


in the while loop the first if statement is not executed because option == 1 is false.


then your program executes lines 11, 12, 13, 14, 15, 16.


You must put theses lines plus lines 9 and 10 in a { } block so these lines execute only if option == 1.


You can ask any other questions if you want by sending an e-mail to me at os56k@yahoo.com or send an IM if I'm on-line in Yahoo Messenger.


No comments:

Post a Comment