Thread: Is this programming exercise possible (don't answer with code, just is it possible?)

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    161

    Is this programming exercise possible (don't answer with code, just is it possible?)

    Okay, so I'm not sure if I ran into an errata type question where the exercise answer is not possible yet, given what has been learned up to now.

    In C++ Primer Plus chapter 5, exercise 8 asks:

    Write a program that uses an array of char and a loop to read one word at a time
    until the word ‘done’ is entered. The program should then report the number of
    words entered (not counting done). A sample run could look like this:


    Enter words (to stop, type the word done):
    anteater birthday category dumpster
    envy finagle geometry done for sure
    You entered a total of 7 words.


    You should include the cstring header file and use the strcmp() function to
    make the comparison test.
    So up to now, 'While', 'Do-While', and 'for' loops have been taught.
    'If-else' has not yet.

    So is this exercise possible or is it written incorrectly?
    The reason is it appears like the author meant to have you enter a word and 'enter' but the example input shows a long string then 'enter' and another long string then 'enter'. Otherwise 'for' and 'sure' could not be entered after 'done' is entered.

    Is it possible without using 'if-else'?
    Because don't we have to separate the words by whitespace then compare that word to 'done'?

    Been banging my head too long on this one.

    Thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So is this exercise possible or is it written incorrectly?
    Yes, it's possible. Probably not written incorrectly, although it does look more like C than C++.


    Jim

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by jimblumberg View Post
    Yes, it's possible. Probably not written incorrectly, although it does look more like C than C++.


    Jim

    Yes, he wants to us to use cstring, so yes.
    Thanks. I'll keep slogging away.

  4. #4
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    It just seems to be poorly worded. If the problem was written better, I'd probably get it done in not much more time than it took to type this reply. Doesn't seem very difficult but I could see how you may be confused being new to the language.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Teaching C-style string handling, as a lesson in learning C++, is objectively wrong. Get a better book. Look at the book recommendations on this forum for a few suggestions.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by Elkvis View Post
    Teaching C-style string handling, as a lesson in learning C++, is objectively wrong. Get a better book. Look at the book recommendations on this forum for a few suggestions.

    C++ Primer Plus by Steve Prata is in the list of recommended books.
    I think it is an excellent book. His reasoning for including C-style string handling is in working with other's C++ code, you may run
    into this string handling type.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    His reasoning for including C-style string handling is in working with other's C++ code, you may run
    into this string handling type.
    How early in the book is the C-style sting introduced? If it is introduced before std::string then I would have problems with his reasoning. If you are just learning C++ you should be learning C++ first and not worry about "working with other's C code" until much later in the book.


    Jim

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by jimblumberg View Post
    How early in the book is the C-style sting introduced? If it is introduced before std::string then I would have problems with his reasoning. If you are just learning C++ you should be learning C++ first and not worry about "working with other's C code" until much later in the book.


    Jim
    It comes before std::string.
    I'm coming from C, so I don't mind it.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by FloridaJo View Post
    It comes before std::string.
    And that's why I say it's objectively wrong. Any proper "Learn C++" book should start with the canonical, idiomatic C++ way of doing things, and only later, or perhaps not at all, discuss more advanced (and error-prone) topics like C-strings. If someone wants to learn C, they should read a C book.

    You may be comfortable with it, coming from C, but that doesn't mean a good C++ book.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It comes before std::string.
    Then the book is really not teaching modern C++, perhaps you may want to consider a different book that teaches modern C++.

    Edit: If I remember correctly the first edition of this book was published in the mid 1980s and although the book has been updated a number of times it still retains the same overall structure of C with classes. Even the last edition (6th) was published before the C++11 standard was finalized and it has no coverage of the current standard (C++14). So even the latest edition is fairly outdated, if you have anything prior to the 6th edition you really should trash that book.

    Jim
    Last edited by jimblumberg; 09-20-2017 at 02:32 PM.

  11. #11
    Banned
    Join Date
    Aug 2017
    Posts
    861
    yes just use a while loop and something to read input then use that strcmp like stated to use for an indicator when to kick out of loop, while in loop keeping count of words minus last (done) then store it while reading it, then after getting kick out of loop print it.
    C++ is just n extension of C

    you still got a do loops and input ( cin >> ) and output ( cout << ) in C++

    it's C++ Primer Plus not C++ in 35.6 days
    Last edited by userxbw; 09-22-2017 at 06:23 PM.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Okay, so I'm still having a struggle with answering this programming question correctly.
    Using just do-while, while, or for loops with no use of logical operators or if/else statements, and no breaks (as that has not been covered yet).
    This is what I see as being difficult:

    The logic as I see it is two loops;

    ---(Read a line)
    |
    | ---(Check for 'done')
    |
    | ---('done' found OR end of line)
    |
    ---('done' found)

    In the middle loop, if I set a flag for 'done' being found, it get's reset if the next word/words is not 'done'.
    So a line like this (job crater easily done seahorse sponge) cannot check for both 'done' and end of line.

    Any hints?

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    with no use of logical operators or if/else statements, and no breaks
    Without any of these features you're going to have problems controlling anything. Even a do/while or while() loop use some kind of logical operation. You need to show some code illustrating what you're trying to do and ask questions based on the code you provide.


    Jim

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by jimblumberg View Post
    Without any of these features you're going to have problems controlling anything. Even a do/while or while() loop use some kind of logical operation. You need to show some code illustrating what you're trying to do and ask questions based on the code you provide.


    Jim

    All logical operators so far have been only == and != I should say.
    Still working on the flow so the code below isn't polished yet.
    Here is the code I have so far:

    Code:
    #include <iostream>
    #include <cstring>
    
    
    using namespace std;
    
    
    int main() {
        int x,y = 0,done = 1;
        int Numwords = 0;
        int set = 0;
        char temp_input_line[80];
        char current_word [80] = "";
        
        do {
            cin.getline(temp_input_line,79);
            x=0;  // Set beginning array count to 0
            while (temp_input_line[x] != 13){        //Check for Return
                while (temp_input_line[x] != ' '){        //Check for Space
                    current_word [y] = temp_input_line [x];
                    x++;
                    y++;
                    set = 1;                                //Set Flag that word started
                }                                         //End check for space
                Numwords += set;                            //Increase word count
                set = 0;  //Reset word count flag
                current_word [y + 1] = '\0';                    //need to compare and set flag here if 'done' found.
                
                y = 0;
                done = strcmp("done",current_word);             //Maybe need another 'while' loop here to set 'done' to 1 or 0 flag.
            }                                       //End check for Return
        }while (done != 0);
        
        cout << "Number of words is : " << Numwords << endl;
        
        
        
        
        
        
        
        
        return 0;
    }

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Okay, do you realize that getline() extracts and then discards the end of line character, if present, and that it always properly terminates the C-string.

    Also you can have getline() extract "words" by using the optional third parameter, or you could just use the extraction operator>> instead.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with C programming exercise.
    By dnguyen8 in forum C Programming
    Replies: 7
    Last Post: 10-08-2013, 08:05 PM
  2. C Programming Language Exercise
    By serg_yegi in forum C Programming
    Replies: 17
    Last Post: 11-30-2010, 04:36 AM
  3. c programming exercise
    By Pulock2009 in forum C Programming
    Replies: 3
    Last Post: 10-30-2009, 02:48 AM
  4. programming exercise
    By mashour06 in forum C Programming
    Replies: 1
    Last Post: 06-01-2009, 06:22 AM
  5. The C programming language exercise
    By refuser in forum C Programming
    Replies: 10
    Last Post: 11-20-2008, 03:57 PM

Tags for this Thread