Question:
Simple C++ Help Please?
CuteNERD
2011-01-21 09:37:27 UTC
I'm working on a program that reads a line of code and stores each word into an index or a array of string and I'm stuck. Its a small part of a larger project I must used cin.getline:

#include
#include
#include
#include
#include
using namespace std;
int main ()
{
char anything;
sting word[3000];

while (!cin.eof())
{
cin,getline(anything);
line++;

(somehow I have to put each word from the line into its own index of the string array and I am so lost on how to do that)
}
}
Four answers:
2011-01-24 23:57:18 UTC
Do you mean to create an array of indexes to the original string? Or an array of strings? Is your project to tokenize the words in your code? I'll assume all of the above here... This hasn't been compiled or tested



// Function Prototypes (A.K.A. Function Declarations)

int main (void);

int FindNextWord (char* line, int* startIndex);

//---------------------------------------------------------

// Function Definitions

//---------------------------------------------------------

// Note: This function is destructive (it reuses the same memory, it doesn't make copies)

int main(void)

{

#define MAX_WORDS 256

#define MAX_LINE_LENGTH 1024



int lineIndex;

int wordIndex;

int wordLength;

int wordCount;

char* oneWord;

int token;

char codeLine[MAX_LINE_LENGTH];

char* wordStringsArray[MAX_WORDS];

int wordIndexesArray[MAX_WORDS];



// Get the line of code (however you do this)

while (GetNextLine(codeLine))

{

// Separate words

wordCount = 0;

lineIndex = 0;



wordLength = FindNextWord(codeLine, &lineIndex);

while (wordLength)

{

// NULL terminate the word found. Note: This alters the original codeLine string.

codeLine[lineIndex + wordLength] = NULL;



// This literally divides the line string into word strings (same memory)

// Copy the address of the beginning of the word to the Word Strings Array

wordStringsArray[wordCount] = &codeLine[lineIndex];



// Store index to the word found in the index array

wordIndexesArray[wordCount] = lineIndex;



wordCount++;

wordLength = FindNextWord(codeLine, &lineIndex);

}



// To access the word via the Word Indexes Array:

oneWord = codeLine[wordIndexesArray[wordIndex]];



// To access the word via the Word Strings Array:

oneWord = wordStringsArray[wordIndex];



// This or whatever you do with a word once it's found

token = TokenizeWord(oneWord);



// Reloop: Get the next line (until there are no more)

}



// All is well

return (0);

}



//---------------------------------------------------------

// Finds the next word (using spaces)

// Returns length of the word (or 0 if end of line)

// Updates startIndex to be at the beginning of the word (in case of multiple spaces in a row)

int FindNextWord(char* line, int* startIndex)

{

int lineIndex;

int lineLength;





// Find index to the start of the next word

lineIndex = *startIndex;

while ((line[lineIndex] != NULL) && (line[lineIndex] == ' '))

{

lineIndex++;

}

if (line[lineIndex] != NULL)

{

*startIndex = lineIndex;

}

else

{

return (0);

}



// Find index to the end of the word

lineLength = 0;

while ((line[lineIndex] != NULL) && (line[lineIndex] != ' '))

{

lineLength++;

lineIndex++;

}



return (lineLength);

}

//---------------------------------------------------------
2011-01-21 17:59:13 UTC
If you're already using the string standard library, you can use the version of getline() found in the code below. Note that this code still has multiple problems - one is the limit on the number of strings (hard-coded to 3000), another is the cumbersome means of terminating input.



/---------------- code ----------------/



#include

#include



using namespace std;



int main()

{

string words[3000];

int index = 0;



while (cin)

getline(cin, words[index++]);



for (int i = 0; i < index; i++)

count << words[i] << endl;

}
2011-01-21 18:33:32 UTC
int main()

{

//declare variables

string word[3000];



// you have to use ofstream and ifstream (open and read a file)

For example: ofstream infile.open("file.txt", ios:in);



while(!infile.eof())

{



}



return 0;

}



try this!





THN
PleaseInsertACoin
2011-01-21 17:44:05 UTC
http://www.oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html



See 7.3.



I don't know for sure that's what you're trying to do, but it's my guess.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...