[prog] simple c++ question

Laura OConnor evenstar_76 at yahoo.com
Sun Jun 22 15:25:00 EST 2003


Dear all,
 
I've just started programming in C++ and have come across a problem I just can't figure out. Most likely its one of the stupid errors that you just don't see because you are looking too hard, but I've trying it for awhile. Anyway, I'd appreciate any help.
 
This is a simplified version of what I am trying to do:
 
#include <iostream>
#inlcude <cstring>
 
using namespace std;
 
void readText(char *text[])
{
   char tempText[50];
   int j = 0;
 
   cout <<"Please enter a line of text";
   cin.getline(tempText, 50)
   text[j] = strtok(tempText, " ");
        //text[j] = "foo";    - this would work
       //removed while loop to get rest of tokens for simplicity
}
 
int main()
{
   char *words[100];
   readText(words);
   cout << words[0];  //check the first element
 
  return 0;
}
 
The actual program loops the cin.getlines and tokenizes each line. I want the function to create an array of strings from several lines that are read in from the console. The problem is, that when I print out the values of the words array in main, it does not work (gives some funky value). However, if in the function, I set the first element to a string or any char* variable, and not the value returned by the tokenizer, main gives me the correct answer. I can't figure out why it won't work with the char* returned by the tokenizer. I guess I am missing some understanding of pointers. I thought I had this down. If more specific code is needed, I can provide it. There may be a better way to accomplish what I am trying to do, but  I would stilll like to understand why this doesn't work.  Thanks in advance for any advice or pointers (um...hints). This is driving me absolutely crazy. 
 
~gene


---------------------------------
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!


More information about the Programming mailing list