cannot compile

#include<iostream>
using namespace std;

class HowEasy {
string ch[50];
int i;
int count;

public:
int pointVal(string text)
{
ch[50]= text;
i=0;
while (ch[i] == "\0")
{
if (ch[i]==" ")
continue;
else
count = count +1;

i=i+1;

}
return count;



}

};

int main()
{
string mystr;
HowEasy a;
int n;


mystr = "Ths is the text";

n = a.pointVal(mystr);


cout<<n<<endl;


return 0;
}
What exactly is it that you are trying to do?
+1 to what m4ster r0shi said.

Forgetting about the purpose of your program, I noticed a few things that might help:
1. You never initialise the value of count. So the statement (count = count +1;) is a bit useless.
2. 'ch' is declared as an array of 50 string objects. I think you want an array of 50 chars, right? Or do you want a single string object here? I'm not sure...
3. Characters should be in single quotes, like ' ' and '\0'.
closed account (jwC5fSEw)
Okay there's a bunch of problems here.

1. ch[50]= text;

In this statement, you're taking a string and putting it into the 51st element of a 50 element array of chars. I'd recommend using the reference on this website and looking up strcpy (and c_str() if you want to use C++ strings with char arrays).

As sammy said above, it's not clear what you're trying to do. If you're trying to set the last element of an array of strings to something, you need to use ch[49] (though I'm not sure why you'd want 49 blank strings left over).

2. while (ch[i] == "\0")

For character literals, you need to use single quotes (e.g. '\0'). Also, read the conditional again; as of right now, it won't ever reach the body of the while loop unless all of ch is 0's.

3. if (ch[i]==" ")

Again, use single quotes.

4. count = count +1;

For incrementing, it's just cleaner to use ++count instead of count = count + 1 or count += 1.

EDIT: Sammy beat me to it!

EDIT2: Durrr misread his code.
Last edited on
Topic archived. No new replies allowed.