#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    4
    Rep Power
    0

    reading in characters and checking capitalization, no strings allowed


    I'm just starting to learn c++ and I'm having problems figuring this out:

    I Have to read in a poem of four lines one character at a time. NO STRINGS.
    My problem is that I don't know how to check to see if the first letter of each line is capitalized nor how to check if the first letter after the end of a sentence is capitalized.

    How do I do that?

    Please respond as soon as possible!

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    India
    Posts
    61
    Rep Power
    14
    FYI
    You can use isupper function for this, just use this at the starting and then on the character after the "\n"(newline) is encountered. You do this until you reach the EOF.

    Regards
    Regards
    JK
  4. #3
  5. Google Relay Server
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2003
    Location
    Oh christ I don't even know any more.
    Posts
    1,811
    Rep Power
    441
    A sentence ends in a period. A line ends with '\n'. So your strategy could be:

    1) Start reading data from the file. When you encounter the first alphabetic character, check if it's capital or not.
    2) Keep reading and ignoring characters until you find a period or a newline '\n'.
    3) Keep reading characters and check the next alphabetic character you find to see if it's capital. Go back to step 2.

    Also, don't forget to stop at any time if you hit the end of the file.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    4
    Rep Power
    0
    So I'm reading in my characters using cin.get(ch), does this mean that I can directly say something like

    PHP Code:
    cin.get(ch); 
    if (
    ch==isupper
    ...I'm so confused. I'm not sure how I would say something like that to check to see if only the first character is capitalized.
  8. #5
  9. Google Relay Server
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2003
    Location
    Oh christ I don't even know any more.
    Posts
    1,811
    Rep Power
    441
    See the documentation for isupper for more details. But, isupper is a function that takes a single char as a parameter, and returns nonzero if it's an uppercase char or 0 if it isn't. After using get() to read a character in to ch, you can check if it's uppercase by passing ch to isupper and examining the return value:
    Code:
    cin.get(ch);
    
    if (isupper(ch) != 0)
      cout << "Character is uppercase!" << endl;
    else
      cout << "Character is not uppercase!" << endl;
    Note that the above if condition can be simplfied to:
    Code:
    if (isupper(ch)) { ...
    You can leave out the "!= 0", the compiler will do it for you (it will do this all the time, so 'if (x)' is the same as 'if (x != 0)', likewise, 'if (!x)' is the same as 'if (x == 0)').

    You'll want to set up some flag variable that you can use to keep track of whether it's time to check for an uppercase character or not. Perhaps:
    Code:
    /* wantUpper - flag indicating whether or not it is
     * time to check for an uppercase character. let the
     * value 0 mean that it's not time, and 1 mean that
     * it is. */
    int wantUpper = 1;
    If it's defined that way, you'd want to initialize it to 1 to begin with, since the first non-whitespace character in the poem should be uppercase. By 'whitespace' I mean characters that are used as spacing, such as the space itself (ASCII 32), tab (9), carriage return (13), newline (10), etc.

    You can use the isspace() function, just like you used isupper(), to check if a character is a space.

    So, you're logic could be like this:

    1) Initialize wantUpper to 1.
    2) Read a character at a time from the file.
    3) If the character is whitespace, ignore it and go back to step 2.
    4) If wantUpper is 1, then the character must be uppercase. If it is, set wantUpper to 0 since you don't care any more, and go back to step 2. If it's not uppercase, then handle that appropriately. But:
    5) If wantUpper is 0, then it doesn't need to be uppercase. However, if the character is a period '.' or a newline '\n', then that means that you want to check the next non-whitespace character. So, if the character is '.' or '\n', set wantUpper to 1, otherwise leave it as 0. Go back to step 2.

    And of course, each time you read a character make sure you check for EOF and stop.

    Does that help any?

    J.C.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    4
    Rep Power
    0
    That helps soooo much! Thanks!
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    4
    Rep Power
    0
    That helps soooo much! Thanks!
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    4
    Rep Power
    0
    Ok. problems again.
    I want to be able to have three '\n's and after the fourth finish the poem. and I'm also having problems getting it to actually check capitalization on everything but for the first letter.

    Help please!

    Here's what I have so far:

    Code:
    #include<ctype.h>
    #include<iostream.h>
    
    int main()
    {        
    
         char ch = ' ';
         int wantUpper = 1;   
    cout << "Please enter a poem of four lines:" << endl;
             
         cin.get(ch);  
         if (isupper (ch) != 0)
            cin.get(ch);
         else
            cout << "Invalid Capitalization." << endl; 
                                      
         if (ch == '.' ||
             ch == '?' ||
             ch == '!' ||
             ch == '\n')
         {
            wantUpper = 1;
            cin.get(ch);
            
            if (isupper(ch) != 0)
            {
               cin.get(ch);
            }
            else
               cout << "Invalid Capitalization" << endl;
         }
         else  
            wantUpper = 0;
            cin.get(ch);
            
         }
  16. #9
  17. Google Relay Server
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2003
    Location
    Oh christ I don't even know any more.
    Posts
    1,811
    Rep Power
    441
    Each time you call cin.get, it reads one character. Just look at your program and think about the logic there.

    I mean, you want to read the entire poem. But if you look at your program you see that this is what it reads:

    It only reads the 2 chars if the first letter is lowercase. If the first letter is capital and the second one is not a punctuation, or if the first letter is a punctuation and the second letter is not capital, then it reads the first 3 letters of the poem. If the first letter is capital and the second is a punctuation and the third letter is not capital, or if the first is a punctuation and the second is capital, then it reads the first 4 letters of the poem. It will read the first 5 letters if the first and third are capital and the second is a punctuation. If the first letter is capital it will never print that message, unless the second letter is a punctuation and the third is not capital, in which case it will print the message once. If the first letter is lowercase, or if the first letter is a punctuation and the second is capital, then it will print the message once. Otherwise, if the first character is a punctuation and the second is lowercase or also a punctuation, then it will print the message twice.

    Confusing? Well hey, you wrote it, not me! :p

    Think through the advice I gave you above, and just implement it exactly as I stated it. The English should translate into the C++ fairly easily.

    But here's a hint: First off, you want to keep looping to read every character input; not just read the first couple of characters then stop. Here is a simple program that echos, for the most part, what the user types back at them:
    Code:
    char ch;
    
    while (cin.good()) {
      cin.get(ch);
      cout << ch;
    }
    And this will do the same thing, but will exit as soon as the user types the letter 'q' anywhere in one of the lines:
    Code:
    char ch;
    
    while (cin.good()) {
      cin.get(ch);
      if (ch == 'q')
        break;
      cout << ch;
    }
    cin.good() will be true as long as EOF hasn't been reached on cin, and as long as there wasn't some weird error. But, when entering information on the keyboard, to signal an EOF you type Ctrl-D in *nix and Ctrl-Z in Windows... not the most convenient way to exit the loop. Fortunately, your program has a way to know when to stop: After it has read 4 lines, don't read any more! So one extra thing you'll have to do is count the number if \n's you've read. Once you read 4, you know that the poem is over, so you can stop reading and ignore anything else the user typed.

    Another approach would be to first read 4 lines of data from cin; then process the entire string of 4 lines all at once after the user has finished entering the poem. But since you aren't allowed to store strings, then you'll just have to go one char at a time and count the newlines.

IMN logo majestic logo threadwatch logo seochat tools logo