char name[my_string.size()];
C++ wants you to use a constant for an array size. If you want to do dynamic memory allocation then lookup new and delete
for (int a=0;a<=TempNumOne;a++)
This for loop will iterate 1 past the number of elements needed
When you pass val to cout you're passing the memory address. * is used to dereference the pointer so you can pass what it's pointing to.
strncpy(word,&y,KEYWORD_MAX_LEN-1);
This is not the intended purpose of strncpy. You're passing it y as the source string which is just a single char and telling it to copy KEYWORK_MAX_LEN-1 elements from a single char to word. If you just want to set the first element of word to y then a simple assigment will do. word[0] = y;
Ignore everything that was commented out from my code in the first post. By commented out, I mean code after " //"
I want to pass an equivalent of char* argv[ ] to my strncpy. Earlier in the code, it was passed through optarg, but I am not in main anymore and the value of "optarg" will be determined by me.
Prior code:
1 2 3 4 5
case's': //optarg is the keyword passed after s- optarg. i.e. the value is stored in optarg
cout<<"\noptarg is "<<optarg<<"\n";
strncpy(word,optarg,KEYWORD_MAX_LEN-1); //copies the first (KEYWORD_MAX_LEN-1) characters of optarg to word
doSearch = true;
break;
Basically, I want to implement the same, but with an arbitrary optarg outside of code main.
BTW, word is:
while(i!=-1) {
cout<<"search method about to exec";
rcode = search(word);
cout<<"\nNow, enter -1 to quit\n";// or word to continue\n";
cin>>i;
cout<<"\nenter new word\n";
char my_stringC[100];
cin>>my_stringC;
cout<<"\nmystring is: "<<my_stringC<<"\n";
cout<<"str copy about to exec with myStringC = "<< my_stringC; //this doesnt execute
strncpy(word,my_stringC,KEYWORD_MAX_LEN-1); //
}
The cursor gets lost in the line before the last. It never executes that code.
enter -1 to end loop. otehrwise word to search
server says 'thnking' was *NOT* found.
server's answer was verified.
search method about to execinside search method
Now, enter -1 to quit
2
enter new word
hello
mystring is: hello
There's no endl or \n on line 15. What you've output to cout is just sitting in a buffer and is not flushed to the terminal until you output an endl or \n.