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

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
                done = strcmp("done",current_word);             //Maybe need another 'while' loop here to set 'done' to 1 or 0 flag.
    Not really. Although, you may want to make your Boolean comparison explicit. done = strcmp("done",current_word) != 0; Even better would be to use the string type - it makes things so much easier here that it almost hurts.
    Last edited by whiteflags; 09-30-2017 at 04:36 PM.

  2. #17
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by jimblumberg View Post
    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

    Okay, what I have works as far as extracting the words, (in a separate test case) but the main question of how to make the logic work to only count the words before 'done' still avoids me.

  3. #18
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by whiteflags View Post
    Code:
                done = strcmp("done",current_word);             //Maybe need another 'while' loop here to set 'done' to 1 or 0 flag.
    Not really. Although, you may want to make your Boolean comparison explicit. done = strcmp("done",current_word) != 0; Even better would be to use the string type - it makes things so much easier here that it almost hurts.
    The author wants c-style strings here. So, no string types at this time.

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look at this snippet:
    Code:
         
        do {
            cin.getline(temp_input_line,79);
            x=0;  // Set beginning array count to 0
            while (temp_input_line[x] != 13){        //Check for Return
    When will that while() statement ever end? Answer never.

    what I have works as far as extracting the words,
    What I'm saying is that your code is much too complicated. If you want words then just extract words from the file.

    Jim

  5. #20
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by jimblumberg View Post
    Look at this snippet:
    Code:
         
        do {
            cin.getline(temp_input_line,79);
            x=0;  // Set beginning array count to 0
            while (temp_input_line[x] != 13){        //Check for Return
    When will that while() statement ever end? Answer never.


    What I'm saying is that your code is much too complicated. If you want words then just extract words from the file.

    Jim


    The input is the keyboard, and we get a line, then we have to store that line somewhere.
    I could say:
    while (x <= strlen(temp_input_line)
    How would one extract words from a stored line?

    So while I did say the code could be cleaned up and polished, it's the flow that I can't seem to understand how to do with the limited tools supplied up to now in the book.

  6. #21
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by jimblumberg View Post
    Look at this snippet:
    Code:
         
        do {
            cin.getline(temp_input_line,79);
            x=0;  // Set beginning array count to 0
            while (temp_input_line[x] != 13){        //Check for Return
    When will that while() statement ever end? Answer never.

    Jim

    So the the only way to make this work is to include an && as below (which violates the unwritten rule [using only what you've learned up till now]) .
    But another problem is it only stops when 'done' is the last word. It won't quit while 'done' is inside the sentence.


    Code:
    #include <iostream>
    #include <cstring>
    
    
    using namespace std;
    
    
    int main() {
        int x,y = 0,done = 1;
        unsigned long z = 0;
        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
            z = strlen(temp_input_line);
            while (x <= z ){                             //Run only as long as the string length
                while (temp_input_line[x] != ' ' && x <= z){        //Check for Space and less than strlen
                    current_word [y] = temp_input_line [x];
                    x++;
                    y++;
                    set = 1;                                //Set Flag that word started
                }
                x++;
                                                                        //End check for space
                Numwords += set;                            //Increase word count
                set = 0;                                           //Reset word count flag
                current_word [y + 1] = '\0';                  
                y = 0;
                done = strcmp("done",current_word);             //If 'done' found, result is 0
            }                                      
        }while (done);
        
        cout << "Number of words is : " << Numwords << endl;
        
        
        
        
        
        
        
        
        return 0;
    }

    Output:

    eat me eat me
    eat me eat me
    eat me done eat me
    done
    Number of words is : 14
    Last edited by FloridaJo; 10-01-2017 at 11:37 AM.

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The input is the keyboard, and we get a line, then we have to store that line somewhere.
    You absolutely can use cin.getline(word, size, ' ') to read a word though.

    You'll have to forgive me, I never read this book, but there is a much simpler answer just using cin >> word; so unless you have to use cin.getline(), I think your code is not really what the author wants to see. Imagine for example that you had only one while loop with the strcmp("done", word) != 0 condition in it. What would you have to write around that to make the program count words?
    Last edited by whiteflags; 10-01-2017 at 03:33 PM.

  8. #23
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    but there is a much simpler answer just using cin >> word; so unless you have to use cin.getline(),
    Actually since this assignment is using C-strings it is probably better to use getline() because getline() enforces using the limiting size parameter, unlike the extraction operator where the limit is optional.

    Jim

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Personally I would use extraction anyway with the field width limit, because it would actually handle example inputs that we've seen better.

    eat me eat me
    eat me eat me
    eat me done eat me

    If you strictly used cin.getline() you would run into trouble with the delimiter near the end of the lines.

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