Help on CString
-
Hi guys, I am working on C String and I'm having this weird error. here's the code
#include #include #include //#include using namespace std;
//Function Declarations
void get_longest_word (char data[]);int main()
{
char * MyString;cout<<"--------------------------------------------------------------"<<endl; cout<<"\\nTask 1:\\n"<<endl; ifstream taskonefile("CStringText.txt"); if(! taskonefile.is\_open()) { cout<<"Cannot Open File!"<<endl; exit(1); } int charCount; taskonefile.seekg(0, ios::end); charCount = taskonefile.tellg(); taskonefile.seekg(0, ios::beg); charCount++; MyString = new char\[charCount\]; taskonefile.get(MyString,charCount); cout<<MyString<<endl; cout<<"\\nTask 2:\\n"<<endl; cout<<"Longest word in the above paragraph: "; get\_longest\_word(MyString); cout<<"\\nTask 3:\\n"<<endl; int l\_num = 0; cout<<MyString<<endl; for(int i =0; i<=strlen(MyString); i++) { if (MyString\[i\]=='l' || MyString\[i\]=='L') { l\_num++; } } cout << "Number of Ls" << l\_num; //EXIT+++++++++++++++++++++++++++++++++++++++++++++++++++ cin.get(); return 0;
}
//FUNCTIONS================================================
//TASK 2 FUNCTION
void get_longest_word (char data[])
{
char * token;
char * longest_word = "a";token = strtok(data, " ,.-"); while (token != NULL) { if (strlen (token) >= strlen(longest\_word)) { longest\_word = token; } token=strtok(NULL, " ,.-"); } cout<<longest\_word<<endl;
}
The problem is when I some to task 3 after calling void get_longest_word the content of MyString which is local to int main() becomes only the first character of the entire string... I have no clue what is happening wrong!
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
-
Hi guys, I am working on C String and I'm having this weird error. here's the code
#include #include #include //#include using namespace std;
//Function Declarations
void get_longest_word (char data[]);int main()
{
char * MyString;cout<<"--------------------------------------------------------------"<<endl; cout<<"\\nTask 1:\\n"<<endl; ifstream taskonefile("CStringText.txt"); if(! taskonefile.is\_open()) { cout<<"Cannot Open File!"<<endl; exit(1); } int charCount; taskonefile.seekg(0, ios::end); charCount = taskonefile.tellg(); taskonefile.seekg(0, ios::beg); charCount++; MyString = new char\[charCount\]; taskonefile.get(MyString,charCount); cout<<MyString<<endl; cout<<"\\nTask 2:\\n"<<endl; cout<<"Longest word in the above paragraph: "; get\_longest\_word(MyString); cout<<"\\nTask 3:\\n"<<endl; int l\_num = 0; cout<<MyString<<endl; for(int i =0; i<=strlen(MyString); i++) { if (MyString\[i\]=='l' || MyString\[i\]=='L') { l\_num++; } } cout << "Number of Ls" << l\_num; //EXIT+++++++++++++++++++++++++++++++++++++++++++++++++++ cin.get(); return 0;
}
//FUNCTIONS================================================
//TASK 2 FUNCTION
void get_longest_word (char data[])
{
char * token;
char * longest_word = "a";token = strtok(data, " ,.-"); while (token != NULL) { if (strlen (token) >= strlen(longest\_word)) { longest\_word = token; } token=strtok(NULL, " ,.-"); } cout<<longest\_word<<endl;
}
The problem is when I some to task 3 after calling void get_longest_word the content of MyString which is local to int main() becomes only the first character of the entire string... I have no clue what is happening wrong!
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
strtok searches for the specified tokens within the string and when it finds a token, it replaces it with a NULL character, effectively shortining the original string to everything before the first token. Hope that helps.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
strtok searches for the specified tokens within the string and when it finds a token, it replaces it with a NULL character, effectively shortining the original string to everything before the first token. Hope that helps.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
Yes, I know that but I am using parameter by value and not reference. Right now I have re-initializing the
MyString
variable after I make call to any functions or procedures...- Stop thinking in terms of limitations and start thinking in terms of possibilities -
-
Yes, I know that but I am using parameter by value and not reference. Right now I have re-initializing the
MyString
variable after I make call to any functions or procedures...- Stop thinking in terms of limitations and start thinking in terms of possibilities -
p.a.r.t.h wrote:
but I am using parameter by value and not reference.
Yes, but you pass the pointer by reference. It means that the function will make a copy of the pointer, but the copy will still point at the same memory location. If you think of a pointer being a simple integer variable (for sake of simplicity) containing a number (which is an address), when you pass that variable to your function, you will get a copy but which still contains the same number (the same address). Thus, if you modify the content of the pointer (changing what is at the specified address), the changes will be visible in your main because the pointer points at the same address.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
p.a.r.t.h wrote:
but I am using parameter by value and not reference.
Yes, but you pass the pointer by reference. It means that the function will make a copy of the pointer, but the copy will still point at the same memory location. If you think of a pointer being a simple integer variable (for sake of simplicity) containing a number (which is an address), when you pass that variable to your function, you will get a copy but which still contains the same number (the same address). Thus, if you modify the content of the pointer (changing what is at the specified address), the changes will be visible in your main because the pointer points at the same address.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
I see, thanks for that. But is there a way to avoid this?
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
You may copy the
CString
and do whatever you want with the copy without altering the original one. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]