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] != "
"){
if(&str[i] == ...
-
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.
-
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 it should be .
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;
}
-
C++ Witch
You made the right choice by using std::string, so why do you provide a char* parameter for your wordcount function?
-
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().
-
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.
-
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
-
CSharpener
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.
-
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.
-
C++ Witch

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.
Popular pages Recent additions
Similar Threads
-
By digioz in forum C# Programming
Replies: 2
Last Post: 09-07-2008, 04:30 PM
-
By guitarist809 in forum C++ Programming
Replies: 7
Last Post: 09-04-2008, 06:02 AM
-
By bobthebullet990 in forum C++ Programming
Replies: 5
Last Post: 05-05-2006, 09:19 AM
-
By occ0708 in forum C++ Programming
Replies: 6
Last Post: 12-07-2004, 11:47 AM
-
By client in forum C Programming
Replies: 2
Last Post: 05-05-2002, 01:38 PM