Help Please

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ifstream read("hello.txt");
     
      string on;
      getline(read,on);
      
      cout<<on<<endl;
      
      
      
      char str[] = on;
      char delims[] = ",";
      char *result = NULL;
      result = strtok( str, delims );
      while( result != NULL ) {
      cout<<result<<endl;
      result = strtok( NULL, delims );



My program have a problem, the data is from a note pad ..... it says initializer fails to determine size of str; help thanks i need answer asap :D GODBLESS
Last edited on
char str[] = on;

The error is probably related to this. When you declare a variable of type char[], it has to be constant or already defined -- e.g.:

char str[] = "Here is a string!";

This is because the compiler needs to know how large it is in advance. When you are trying to read in a string of unknown length, like something from a file, I would recommend using char*. Or, even easier, I'd just do something like this:

1
2
3
4
5
6
7
8
cout<<on<<endl;
char delims[] = ",";
char *result = NULL;
result = strtok( on.c_str(), delims );
while( result != NULL )
{
	cout<<result<<endl;
	result = strtok( NULL, delims );


Best of luck and I hope this helps.
hmm sir so what should be the datatype of my "on" variable??
very sorry for being slow I'm a newbie ^_^ i hope somebody there has a heart to help me..

if possible please edit my source code ty so much appreciated

I will have my exam this november I need to pass thank you so muck Mr. Moeljbcp ^^
Last edited on
Here ;)

This will Read And Print Everything from ur text file :)

string line;
ifstream myfile ("example.txt");
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();

Also i am working on a project and tell me if u wanna team up :)
Last edited on
lets team up bro :D

ur targeting for NC 4 exam?? also?
Last edited on
anyone can help me about this program?
so what should be the datatype of my "on" variable?

It should a string, as you already have it.
1
2
      string on;
      getline(read,on);


As shown above by Moeljbcp, the strtok function requires a plain C-string, which can be obtained by using on.c_str().

It can be a little confusing, as the program is making use of two different types of string, the ordinary c-string (which is just an array of characters terminated by a null), and also the more advanced C++ string.


You solved!! Thanks For The Help Guys.. :D
Topic archived. No new replies allowed.