1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need help writing C++ program that counts non-whitespace characters ?

  1. Oct 1, 2011 #1
    Need help writing C++ program that counts non-whitespace characters....?

    1. The problem statement, all variables and given/known data

    Here are instructions....

    Code (Text):
    Write a program - stored in the file charcounter.cpp - that will read and count non-whitespace characters from the standard input stream - until some special character is entered. Let's use & for now. (This value is declared as a named constant so that it can easily be changed in only one place in the program - SO ... the & should (must??) not appear anywhere else in the program!)

    Your program will also count the number of digits in the input stream, as shown below. Note that the cctype library contains a boolean function isdigit(achar) ...

    (Note that your program should handle the unusual case in which the & is the first non-whitespace character.)

    Your program must produce behaviour very similar to the following ...

    $ ./a.out

    Enter some text - on 1 or more lines - followed by &.
    1   25
    cat
    0g 6&
    There were 9 (non-whitespace) characters before the &
    5 of them were digits.
    $
     


    2. Relevant equations



    3. The attempt at a solution

    This is what I have so far. Really having trouble with this one

    #include <iostream>
    #include <string>
    using namespace std;

    int main()
    {
    const char END_MARK = '&';
    int i, numspaces;
    char nextChar;
    string msg;

    numspaces=1;

    cout << "Enter some text - on 1 or more lines - followed by &.\n";
    getline(cin, msg);

    // checks each character in the string
    for (i=0; i<int(msg.length()); i++)
    {
    nextChar = msg.at(i); // gets a character
    if (isspace(msg))
    numspaces++;
    }
    cout << "\nThere were " << numspaces << " (non-whitespace) characters before the &.";
    cin.ignore();
    return 0;
    }
     
    Last edited: Oct 1, 2011
  2. jcsd
  3. Oct 1, 2011 #2
    Re: Need help writing C++ program that counts non-whitespace characters....?

    This line is unnecessary; the variable nextChar does not appear anywhere else in the code.

    The count of spaces should start with zero, not one (it should be obvious why).

    It looks like the code you posted would result in a count of the number of spaces in a line of text (until '\n'), and not the number of spaces until '&'. There is an easy way to modify your program to fix this issue. You can use an overload of the function getline to change the character that is used to denote the end of a line. This may be done by providing a third parameter to the getline function, which is a character to be used as the delimiter.
     
  4. Oct 1, 2011 #3
    Re: Need help writing C++ program that counts non-whitespace characters....?

    How do I change it so it counts just characters, not words??

    THanks so much for your help!

    And yes, I want it to end with &, insted of \n - Cant I just swap them?
     
  5. Oct 1, 2011 #4
    Re: Need help writing C++ program that counts non-whitespace characters....?

    I haven't looked through program, but if you want to count chars you would need to use the library <cctype>. Hint hint: I would use isalpha(); and test if character is alphabetical or isdigit for numbers.
     
  6. Oct 1, 2011 #5
    Re: Need help writing C++ program that counts non-whitespace characters....?

    "library <cctype>. Hint hint: I would use isalpha(); and test if character is alphabetical or isdigit for numbers.``

    I am not too sure how to use that...
     
  7. Oct 1, 2011 #6
    Re: Need help writing C++ program that counts non-whitespace characters....?


    Ok, So I kind of see what you are trying to do. What I notice is that isspace is not a member of cstring library. It is a member of cctype library. To get the line that the user entered and receive the characters, I would us cin.get() to receive the line by characters. Then I would put the characters into an array. Then I would use a for-loop to see if there are characters or spaces and increment an int by 1.
     
  8. Oct 2, 2011 #7
    Re: Need help writing C++ program that counts non-whitespace characters....?

    We have not even go to arrays yet, so I cant put it into that.

    Can you help me with some sample code that would just count characters and digits...?? Im so lost on this one :(
     
  9. Oct 2, 2011 #8
    Re: Need help writing C++ program that counts non-whitespace characters....?

    Code (Text):
    [B]#include <cctype>[/B]
    #include <iostream>

    using namespace std;
    int main()
    {
        char c;
        cin >> c;
        if([B]isalpha(c)[/B])
            cout << "You entered an alphabetic character." <<endl;

    }

     


    Ignore his comment about using an array. Strings have all the array functionality you might need.

    Counting characters and digits is similar to counting spaces, you'd just use different functions to determine if each character is a digit or a space. You may use the functions isalpha and isdigit.
     
    Last edited: Oct 2, 2011
  10. Oct 2, 2011 #9
    Re: Need help writing C++ program that counts non-whitespace characters....?

    MisterX, yes isalph and isdigit is what I must use, but i dont know how to wwrite it (I am VERY new to C++ )

    Any help, or tutorials on how I can write this?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Need help writing C++ program that counts non-whitespace characters ?
  1. C program help needed (Replies: 3)

Loading...