Counting the words in a string C++

This is a discussion on Counting the words in a string C++ within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> using namespace std; int wordcount(char *str){ int i=0; int count=0; while(&str[i] != "

Thread: Counting the words in a string C++

"){ if(&str[i] == ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Question Counting the words in a string C++

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int wordcount(char *str){
        int i=0;
        int count=0;
    while(&str[i] != "\0"){
        if(&str[i] == "\t"){
            count++;}
       i++;
    }
    return count;
    }
    
    
    int main()
    {
       string stringA;
       cout<<"Insert a string"<<endl;
       cin>>stringA;
       cout<<"there are"<<wordcount(stringA)<<"words in your string";
    }
    This is my code.The problem is i get an error on the last line saying:

    "Cannot convert 'std::string' to 'char' for argument '1' to 'int wordcount(char*)' "

    Im really stuck here, pls help .
    Any help is apreciated.Thanks

    Edit:Oups i posted in thw rong section.Sorry!
    Last edited by mblue; 03-30-2009 at 12:38 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You are passing a std::string, to a function which takes a char*. You should pass the parameter as stringA.c_str().

    Also you would need to change the wordcount signature to take a const char* instead of just a char* (since the c_str() function returns a const char*).

    Also, your char comparisons wont work as they stand. Instead of
    Code:
    &str[i] != "\0"
    it should be
    Code:
    str[i] != '\0'
    .

    Finally, please properly format your code before pasting it here. Look how much easier it is to read when it's formatted:
    Code:
    int wordcount(char *str)
    {
        int i=0;
        int count=0;
        while(&str[i] != "\0")
        {
            if(&str[i] == "\t")
            {
                count++;
            }
            i++;
        }
        return count;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    You made the right choice by using std::string, so why do you provide a char* parameter for your wordcount function?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    1. &str[i] == "somestring" will never be true. You should compare a single character, not pointers to strings. To use a character in stead of a string, use '\0', for instance, in stead of "\0".
    2. the "char *str" argument to wordcount should be "const char *" since it won't change it.
    3. You can't pass a string to a char*. To convert it to a const char*, use somestring.c_str().

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    the conditions "\t" and "\0"should maybe be '\t' and '\0'instead. i dont know c++ but it looks like you compare two strings togheter here
    PHP Code:
     while(&str[i] != "\0"){ 
    what i am sure is not right.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    Ok, i understand that i made a function that takes a char as argument and im trying to use it on a string.That wont work.
    What should i do now?Modify the function so that it takes a string as argument or turn the string into char and then pass it to the function?

    I forgot to tell you that im a newb in c/c++.
    Ive got a headache already.lol

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    This is C++ - why do you post on C-forum?
    Why do you pass std::string to the function expecting C-string?
    Why do you write function that manipulates C-string - use std::String directly
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is a post in the C++ forum AS WELL. Maybe one of the mods can merge or lock this thread with a link to the other one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    Quote Originally Posted by matsp
    There is a post in the C++ forum AS WELL. Maybe one of the mods can merge or lock this thread with a link to the other one.
    Right, and merged.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM

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