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 ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    5

    Code Help. Terminate on 00.

    I have this code I finally got written, but the only thing I can't get it to do is terminate on consecutive zero's.

    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){
                    cin >> num;
                    while(num!=0&&num!=1){
                            ct++;
                            prevnum=num;
                            cin>>num;
                    }
                    if(num==0){
                            cout << " ";
                            //prevnum=num;
                    }
                    
                    switch (prevnum){
                    case 2:
                            if(ct==1)cout << "a";
                            if(ct==2)cout << "b";
                            if(ct==3)cout << "c";
                            break;
                    case 3:
                            if (ct==1)cout << "d";
    						if (ct==2)cout << "e";
    						if (ct==3)cout << "f";
                                    break;
                    case 4:
                            if (ct==1)cout << "g";
    						if (ct==2)cout << "h";
    						if (ct==3)cout << "i";
                                    break;
                    case 5:
                            if (ct==1)cout << "j";
    						if (ct==2)cout << "k";
    						if (ct==3)cout << "l";
                            break;
                    case 6:
                            if (ct==1)cout << "m";
    						if (ct==2)cout << "n";
    						if (ct==3)cout << "o";
                                    break;
                    case 7:
                            if (ct==1)cout << "p";
    						if (ct==2)cout << "q";
    						if (ct==3)cout << "r";
    						if (ct==4)cout << "s";
                                    break;
                    case 8:
                            if (ct==1)cout << "t";
    						if (ct==2)cout << "u";
    						if (ct==3)cout << "v";
                                    break;
                    case 9:
                            if (ct==1)cout << "w";
    						if (ct==2)cout << "x";
    						if (ct==3)cout << "y";
    						if (ct==4)cout << "z";
                                    break;
                    }
                    ct=0;
            }
            return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    These were the instructions.

    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.


    Thats the project. Some sample i/o

    The input:
    4 44 1 0 5 5 5 1 4 4 4 1 5 5 1 3 3 1 0 4 4 4 1 2 2 2 1 3 3 1 0 2 2 2 1 7 7 7 1 3 3 1 2 1 6 1 0 0

    would yield:
    i like ice cream


    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.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    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

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    I'm not sure exactly if you changed anything, but basically I just want when I enter two zeros in a row. The CMD box to ask to exit. Still don't have it figured out.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    scwizzo probably just got confused, and thus gave some criticism for which the provided solution is exactly the same as the original code

    but basically I just want when I enter two zeros in a row. The CMD box to ask to exit.
    So what is the current behaviour?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    I'm confused what you're trying to do here:
    Code:
                while(num!=0 || prevnum!=0){
                    cin >> num;
                    while(num!=0&&num!=1){
                            ct++;
                            prevnum=num;
                            cin>>num;
                    }
    Also, prevnum never gets set after you exit out of that inner while loop.
    Last edited by King Mir; 09-16-2008 at 05:22 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by laserlight View Post
    scwizzo probably just got confused
    No I purposely posted the same code. I highlighted it red so he knew what to look at/change. I didn't want to change it for him

    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

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    Quote Originally Posted by scwizzo
    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.
    Last edited by laserlight; 09-16-2008 at 11:46 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    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.)
    Code:
    int num, count, next;
    
    cout << "Enter a number 0-9 for (x) amount of times." << endl;
    cin >> num;
    
    while (true) {
       count = 1;
       
       do {
          cin >> next;
          if (next == num) count++;
          if (num == 0 && count == 2) break;
       } while (next == num);
       
       if (num == 0 && count == 2) break;
    
       /* Insert switch block here */
       
       num = next;
    }
    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.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    BLAH! I had it right in my head... I swear... haha >.>

    In response to what arpsmack has, you could do something like this.
    Code:
    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){
                    cin >> num;
                    if(num == 0){
                        prevnum = num;
                        cin>> num;
                        if(num == 0) break;
                    }
                    else{
                        while(num!=0&&num!=1){
                                ct++;
                                prevnum=num;
                                cin>>num;      
                        }
    //rest of stuff down here
    Last edited by scwizzo; 09-16-2008 at 07:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer Help
    By ggraz in forum C Programming
    Replies: 3
    Last Post: 09-30-2008, 12:35 AM
  2. compile c code using -00 option of gcc
    By George2 in forum Tech Board
    Replies: 1
    Last Post: 08-29-2006, 04:47 AM
  3. binary data file
    By tucker81 in forum C Programming
    Replies: 8
    Last Post: 06-08-2006, 01:50 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21