Finding Number of Chars in String?

This is a discussion on Finding Number of Chars in String? within the C++ Programming forums, part of the General Programming Boards category; Hi I've got the following, which is supposed to allow for finding the number of characters in a string, and ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    118

    Finding Number of Chars in String?

    Hi

    I've got the following, which is supposed to allow for finding the number of characters in a string, and return that amount +1:

    Code:
    int function(string databaseDays)
    {
            int numberOfDays = 1;
    
    	for (int i=0; i < databaseDays.length(); i++)
    	{
    		if(databaseDays.at(i) == ".")
    		{
    			numberOfDays++;
    		}
    	}
    }
    When I try to compile though, I get the error: "ISO C++ forbids comparison between pointer and integer" regarding my if statement.

    My understanding of the string::at(pos) function is to return the char at pos, and since "." is a char, where is this comparison error coming from? I must be overlooking something obvious.

    Thanks

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    40
    Instead of using " ", use ' '.
    Like this:
    Code:
    if(databaseDays.at(i) == '.')
    {
    	numberOfDays++;
    }
    [EDIT]
    About the error. The compiler sees "." as type const char*. A array of char objects
    Last edited by idleman; 12-05-2009 at 04:49 PM. Reason: error explanation

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Well, I tried that, but all I got was a return of the full number of characters in the string, not just the periods.

    Why would it count every character?

    Thanks!
    FlyingIsFun1217

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    40
    You don't return anything from that function, so you need to return the value you just counted (numberOfDays), if not you just get a random value.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Well, don't I just feel retarded now?

    Thanks!
    FlyingIsFun1217

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you just want to count the occurrences of a letter in a string, there's also the count algorithm:

    Code:
    int numberOfDays = std::count(databaseDays.begin(), databaseDays.end(), '.') + 1;
    It's really up to you to decide, whether you want a specific function to do that, but even then you can just use this algorithm.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Checking number of chars in a string
    By Tropicalia in forum C++ Programming
    Replies: 10
    Last Post: 09-26-2006, 05:15 PM
  3. Replies: 8
    Last Post: 03-31-2006, 08:15 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21