Why does the array of 50 strings only contain 3? And i'm assuming you mean the first 3 contain strings.
(more to come maybe)
Ok. I simplified the code down a lot to something manageable. Here's the problem. You see this code posted below? Well pretend the array char* p[50] contains three strings. the first element in the array is "ho" the second is "ha" and the last one is "he"
Now when I first print p[1] (the second element in the array) it prints "ha" and only "ha", but when I print p[1] for the second time it prints "ha he" (both the second and third element. Does anyone know why this is?
if you have to assume the variable word is an integer that starts off at the value 3
[php]
cout<<"p[1] " << p[1];
while(words > 0)
{
strcpy(init_str, str);
temp = strtok(init_str, " \"");
while(temp != NULL)
{
if(!strcmp(p[words-1], temp))
{
cout<<"eliminating redundancy";
temp = strtok(NULL," \"");
continue;
}
cout<<p[1];
temp = strtok(NULL," \"");
}
words--;
}
[/php]
Thanks in advance everyone. I could really use some help on this. Been scratching my head trying to figure this mess out for weeks, and I finally get it to the point where it should work, except its screwing up, because of this....
Last edited by why06; 11-16-2009 at 12:03 AM.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
Why does the array of 50 strings only contain 3? And i'm assuming you mean the first 3 contain strings.
(more to come maybe)
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
p[0] = 1st element , p[1] = second element and p[2] = 3rd element.
Yes, but the important thing to realize is this:
p[1] at first = "ha"
then the second time I call p[1]
p[1] = "ha he"
if you don't believe me try this yourself... and giving stand in values for the variables.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
I'm tired too... yawn... it probably has something to do with strcpy() oh well I'll look into it tomorrow when I can focus >_>....
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
-.- Why06, i hate you. Because of your program i realized I forgot some of how multi-level pointers worked....and ended up spending half an hour going crazy to reteach myself. It's all good now though.
Anyway the only thing i can think of is if somehow the null character between p[1] and p[2] dissappeared
Last edited by zeco; 11-16-2009 at 01:05 AM.
Is it possible that first time it works but maybe cuz of coding the second time it adds on to first and reads as p2?
possibly, I'll have to look into that... lol sorry I kept you up...I'm sort of like that too, once I get working on something its hard for me to stop. until I figure it out...
It's more then possible. That's what the problem is, but thanks for generalizing it down like that.Originally Posted by bmw
Here see I simplified it even more... so it definitely has something to do with the strcpy() and strtok() functions...
[php]
cout<<"p[1] " << p[1]; // this will print "he"
while(words > 0)
{
strcpy(init_str, str);
temp = strtok(init_str, " \"");
cout<< p[1]; //this will print "ha he"
}
//Assume the following conditions before the code executes:
//temp, init_str, and str are all char*
//temp = "";
//init_str = "";
//str = "ho ha he";
//p[0] = "ho"; p[1] = "ha"; p[3] = "he"
[/php]
Last edited by why06; 11-16-2009 at 08:18 AM.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
Ok. Maybe I simplified it a little too much this has grown far more complicated then I initially suspected. Now I'm of the suspension that it is p[1]'s link to init_str that is causing the problem. I will demonstrate
[php]
char* p[50];
int words = 0;
char* init_str;
strcpy(init_str, str);
char* temp = strtok(init_str, " \"");
while(temp != NULL)
{
p[words] = temp;
temp = strtok(NULL, " \"");
words++;
}
cout<<"p[1] " << p[1] <<endl;
while(words > 0)
{
strcpy(init_str, str);
char* temp2 = strtok(init_str, " \"");//Here I declared a completely new temporary string, just to test weather temp was the problem.
cout<<p[1]<<endl; // Its not. It has something to do with p[1] connection to init_str through temp...
[/php]}
Im so screwed....![]()
Last edited by why06; 11-16-2009 at 09:15 AM.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
Try erase the second "str" in the strcpy line
maybe this is the problem...
i think it isn't but you can try it.
the end of the string is a zero i think that is a delimiter to but there is a possibility it isn't
you string ends with a not delimiter so the third piece of string never was gotten out of the original string.To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token.
source: strtok - C++ Reference
How about you dont use a pointer of character arrays
There are five possible operations for any army. If you can fight, fight; if you cannot fight, defend; if you cannot defend, flee; if you cannot flee, surrender; if you cannot surrender, die." - Sima Yi