Thread: How to count characters, but not spaces?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    49

    How to count characters, but not spaces?

    Hello,

    I am trying to figure out how I can count characters in a line, but not include the spaces in this count. I realize that cin won't see "whitespace", but it isn't being very kind to me. I first tried cin.get(), but rather than going through the while loop, it just read the first line, then stopped. So far using the code I have, I can count the characters with the spaces included of each line, and display the line number, the line text itself and how many characters were in that line. Now if I could just get rid of those pesky spaces!
    Oh yes, one other troubling feature of my code is an annoying C4267 warning concerning the variable i. That variable is being set equal to line.size(). When I give "i" type size_t, then I get: C4267: 'argument' : conversion from 'size_t' to 'unsigned int', possible loss of datang. When I assign "i" as an unsigned int, then the warning remains the same. I am not sure what to do in this case. Any ideas?
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cassert>
    using namespace std;
    
    int charCount = 0;
    int main()
    {
    	string fileName;
    	cout << "Enter name of file: ";
    	getline(cin, fileName);
    
    	ifstream inFile(fileName.data() );
    	assert(inFile.is_open());
    
    	unsigned lineCount = 0;
    	unsigned i = 0;
        string line;
    
    	while (!inFile.eof())
    	{		
            getline(inFile, line);
            lineCount++;
    		cout << "\nline #" << lineCount;		
    		cout << " " << line << "\n";
    	    i = line.size();
    			cout << i << " characters in this line\n\n";
    				
          }
        inFile.close(); 
    	return 0;
    }
    Thank you for your time.
    Semper Fi!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The easiest way would be to use get:
    Code:
    #include <cctype>
    ...
    while ( inFile.get ( c ) ) {
      cout<< c;
      if ( c == '\n' )
        linecount++;
      else if ( !isspace ( c ) )
        charcount++;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    Hi prelude, you've helped me before. Thanks a bunch. I will see what that does for me. Any idea of what to do about that conversion warning?
    Semper Fi!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Any idea of what to do about that conversion warning?
    The problem is here:

    >i = line.size();
    i is an unsigned int, but std::string's size member function returns string::size_type, which on your implementation is size_t. Declare i as
    Code:
    string::size_type i;
    And the warning will go away because your types will match.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    Whoah, that compounded 'me eager little problems. Now every where that "i" is used, I get a warning. (I have added some statements to calculate the line with the shortest and longest amount of characters, and to find the average)
    Semper Fi!

  6. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    I haven't figured out a way to use your code that doesn't count the spaces. When I put it in my program, it printed out a running total for the characters, rather than a subtotal of characters for each line.
    Semper Fi!

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Just count the characters in one line, when you reach \n simply output whatever message you see fit, and set charCount = 0; ready for the next line.

    If you're having trouble, post your latest code.

    Tip: Don't use this code to control a loop:
    while (!inFile.eof())
    It's a bad habbit. There's an explanation of why here:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    That's a C FAQ, but the principle still stands. You can code your way out of it and still use eof(), but its better not to.

    [edit]
    Doh! Didn't see alphaoide's last message
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  2. consecutive number
    By stewie1986 in forum C Programming
    Replies: 3
    Last Post: 12-03-2007, 03:53 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. ascii characters video displaying on tv screen
    By deian in forum C Programming
    Replies: 6
    Last Post: 10-12-2004, 09:46 PM
  5. Characters in a txt file.
    By tay_highfield in forum C Programming
    Replies: 3
    Last Post: 01-31-2003, 09:19 AM