Get a Word From a Pointer

Given a string (or a char* pointing to the first letter), is it possible to return the whole word, knowing the length?

I cannot edit the .h file. The function I need help with is GetWord() in class Word. Right now, it only returns the first letter, but I need it to return the whole word.

Words.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Word;
#include "words.h"
#include <string>
using namespace std;

char* ptr;
int len;

Word::Word(const char* word)
{
    ptr = new char;
    *ptr = *word;
    len = strlen(word);
}

Word::~Word()
{
    ptr = NULL;
    len = 0;
}

char Word::GetFirstLetter()
{
    return ptr[0];
}

char* Word::GetWord()
{
    return ptr;
}


Words.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;

#ifndef WORD_H
#define WORD_H

class Word
{
private:
    char* ptr;
    int len;

public:
    Word(const char* word);
    ~Word();
    char GetFirstLetter();
    char* GetWord();
};

#endif 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "words.h"
#include <iostream>
#include <string>
#include <istream>
#include <fstream>
#include <ostream>
using namespace std;

const int FILE_PATH_SZ = 512;
Word** wordArray;
int arrSz;

//@opens an input file and checks for errors
void openFile(ifstream& infile, string filename);

//@gets the number of words in the file and
//@makes arrSz equal to that number
int getSize(ifstream& infile, string filename);

void makeArray(ifstream& infile);

int main()
{
    ifstream infile;
    ofstream outfile;
    string filename;

    //cout << "Enter a filename to open: ";
    //cin >> filename;
    filename = "sample.txt";

    openFile(infile, filename);
    arrSz = getSize(infile, filename);

    cout << "The array is " << arrSz << endl;

    makeArray(infile);

    return 0;
}

//@opens an input file and checks for errors
void openFile(ifstream& infile, string filename)
{
    infile.open(filename.c_str());
    if (infile.fail())
    {
        cout << "Error in opening the input file." << endl;
        cout << "Ending program now." << endl;
        exit(1);
    }
}

//@gets the number of words in the file and
//@makes arrSz equal to that number
int getSize(ifstream& infile, string filename)
{
    string oneItem;
    int count = 0;

    infile >> oneItem;
    while (!infile.eof())
    {
        infile >> oneItem;
        count++;
    }
    infile.close();
    infile.open(filename.c_str());

    return count;
}

void makeArray(ifstream& infile)
{
    string newWord;
    wordArray = new Word*[arrSz];
    for(int j = 0; j < arrSz; j++)
    {
        infile >> newWord;
        wordArray[j] = new Word(newWord.c_str());
        cout << wordArray[j]->GetWord() << ", "; // I used this to test GetWord()
    }
}


sample.txt
1
2
3
4
5
6
7
8
so much depends upon

a red wheel barrow

glazed with rain water

beside the white
chickens


Any help will be appreciated. Thanks!
1
2
3
ptr = new char;
*ptr = *word;
len = strlen(word);
is the problem. You create and copy only one char here. The right way would be
1
2
3
ptr = new char[strlen(word)+1];
memcpy(ptr, word, strlen(word)+1);//or a for loop that copies every char..
len = strlen(word);//you don't really need this, but if you must.. 

Also, what are lines 6, 7 of Words.cpp supposed to do? I'm not sure, but they may be breaking your code too..
They are private class variables that we are supposed to use. Thank you so much for your help!
The private member variables were declared in Words.h. In Words.cpp you're declaring global variables with the same names.
Topic archived. No new replies allowed.