Code Help. Terminate on 00.

This is a discussion on Code Help. Terminate on 00. within the C++ Programming forums, part of the General Programming Boards category; I have this code I finally got written, but the only thing I can't get it to do is terminate ...

For this project, design and implement a Visual C++ .NET program that will simulate a simple text messaging system. The only characters that you can send are A to Z and space. A space is represented by sending a single 0. Each character that is entered is separated from the next character by a 1. Two consecutive 0 digits will end the text message. We will not need to enter a 1 after a space (0), since text messages will not have 2 consecutive spaces, except at the end of the message. For example, to send the two-character text message "CS" you would see the input stream "2 2 2 1 7 7 7 7 1 0 0". Your project should read in a sequence of integers that represent the encoding of multi-character text message, and then print out that text message. For this project, you can assume that the input will be legal. A complete message will contain several 1’s plus the appropriate number of the digits 2 through 9 and 0 to create and terminate a message.

Pseudo-code:
Start:
Declare & initialize variables needed
Read the 1st number
Loop until “finished”: (How do you know when you‟re finished?)
Is number a 0?
Yes: Save the number in “last number” and set the counter to zero;
Is number a 1?
Yes: (You should have read the numbers for a specific letter. Print it out.)
Print out the correct number for the “last number” entered, and the value
of the counter.
For example, if the last number is a 2 and the counter is 1, print "A.‟
Or if the last number is an 8 and the counter is a 2, print "U.‟
After you‟ve printed the letter, set the counter to 0 and the last number to 1.
Is the number a 2, 3, 4, 5, …, 9?
Yes: Save it in “last number,” increment the counter.
Otherwise (number is not in 0 – 9) print an error message, including the number.
Read another number, and then go back to the top of the loop.
End of loop (when you‟re “finished)
Quit.

I don't fully understand the numbers and letters thing, but you said you want it to terminate when there's two zero's in a row? Well... your main while loop has num!=0 OR prevnum!=0 which kind of defeats the purpose. If one of those is zero it is going to continue no matter what the other one is. If they both are zero you said you want it to end... so....

Code:

# include <iostream>
using namespace std;
int main (){
int num=1, prevnum=0, ct=0;
cout << "Enter a number 0-9 for (x) amount of times.";
// (x) is a desired numerical value set between 1 & 3 unless pressing the 7 or 9 in which case it is between 1 & 4.
while(num!=0 || prevnum!=0){
//...rest of code here

Take a look at that while loop zionstorm. I said it continues if prevnum OR num == 0. and you wanted it to stop when prevnum AND num == 0.

Unless I did in fact miss something then yea.. im confused haha

Yes, you are confused
Recall De Morgan's laws: (!a OR !b) == !(a AND b), hence:
(num != 0 || prevnum != 0) == !(num == 0 && prevnum == 0)
Continuing when either prevnum or num is not 0 is the same as stopping when prevnum and num are both 0.

Consequently, Zionstorm is correct for that loop condition, but there is a mistake elsewhere, perhaps with the portion of code that King Mir pointed out.

EDIT:
Oh wait, scwizzo, your mistake is actually in "If one of those is zero it is going to continue no matter what the other one is." That is clearly not true, since if one of those (as in num and prevnum, but it can also refer to the boolean evaluation of num!=0 and prevnum!=0) is zero the loop will continue if the other is non-zero. Conversely, if one of those is non-zero the loop will continue no matter what the other one is.

I have always found that trying to add clever loop conditions to terminate cases like this is non-intuitive and error prone. (As proof I notice that one poster has already gotten confused).

I prefer to make the terminating conditions much more explicit by making it an infinite loop and breaking out when the specified condition is met. Although the concept of a "while (true)" loop looks a little weird at first, I think it makes it clear when the loop ends.

As an example, here's code that will count how many times in a row a certain number was input. Clearly it terminates when you enter 0 with a count of 2. (I highlighted the part that accomplishes this in red).

(In fact, I think it might be even clearer if you use a "goto terminate_program;" in place of the first break. Then the second break is unnecessary. Of course this might incur the wrath of those who believe goto has no good purpose.)

It should be trivial to add a switch block in the appropriate place that tests num and count, and outputs the appropriate character. Plus, the way this is implemented, it isn't necessary to put a "1" between letters, although it won't hurt anything.