Will It Compile?

Will this program compile on Windows? I need to turn in a project that compiles on Windows (specifically, Visual C++) for a class I'm taking. However, I am a Mac/UNIX user (please don't shoot me!) and I have been compiling and running programs in Terminal since I started learning to program two years ago, never using Windows to do so. I don't have a working Windows computer at my house, and I have no idea how to compile my project on a school computer. The program runs fine on my Intel-based Mac, but I know there are differences when you try to run it on a Windows computer.

The program reads in a text file containing a poem to an array. You can then delete words starting with a specific letter and print the array. At the end, it will make a text file containing what remains in the array.

Will someone please compile and run this program on their Windows computer so I can be sure that my instructor will be able to run it?

Thank you so much!

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "words.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

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

void openFile(ifstream& infile, string filename);

int getSize(ifstream& infile, string filename);

void makeArray(ifstream& infile);

void setArray(ifstream& infile, string& filename);

void printArray();

void deleteWords();

void writeToFile(ofstream& outfile);

char printMenu();

void printErrorMessage();

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

    cout << endl;

    do
    {
       choice = printMenu();
       switch (choice)
       {
          case 'a': setArray(infile, filename);
                  break;
          case 'b': deleteWords();
                  break;
          case 'c': printArray();
                  break;
          case 'd': break;
          default:
             printErrorMessage();
       }
    } while (choice != 'd');

    writeToFile(outfile);

    return 0;
}

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);
    }
}

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());
    }
    infile.close();
}

void setArray(ifstream& infile, string& filename)
{
    cout << "Enter a filename to open: ";
    cin >> filename;
    openFile(infile, filename);
    arrSz = getSize(infile, filename);
    makeArray(infile);
    cout << "Loaded file and read " << arrSz << " words." << endl << endl;
}

void printArray()
{
    for(int i = 0; i < arrSz; i++)
    {
        if (wordArray[i] != NULL)
        {
            cout << wordArray[i]->GetWord() << " ";
        }
    }

    cout << endl << endl;
}

void deleteWords()
{
    char letter;
    int j = 0;

    cout << "Enter the starting letter: ";
    cin >> letter;
    cout << endl;
    for(int i = 0; i < arrSz; i++)
    {
        if (wordArray[i] != NULL)
        {
            if (letter == wordArray[i]->GetFirstLetter())
            {
                delete wordArray[i];
                wordArray[i] = NULL;
                j++;
            }
        }
    }

    cout << "Removed " << j << " words." << endl << endl;
}

void writeToFile(ofstream& outfile)
{
    outfile.open("default.txt");
    if (outfile.fail())
    {
        cout << "Error in opening the output file." << endl;
        cout << "Ending program now." << endl;
        exit(1);
    }

    for(int i = 0; i < arrSz; i++)
    {
        if (wordArray[i] != NULL)
        {
            outfile << wordArray[i]->GetWord() << " ";
        }
    }

    outfile << endl;
    outfile.close();
}

char printMenu()
{
    char choice;

    cout << "Please make a selection:" << endl;
    cout << "a) Read a text file" << endl;
    cout << "b) Remove words starting with letter" << endl;
    cout << "c) Print words to console" << endl;
    cout << "d) Quit" << endl;
    cout << "-> ";
    cin >> choice;
    choice = tolower(choice);
    cout << endl;

    return choice;
}

void printErrorMessage()
{
   cout << endl;
   cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
   cout << "!!!! Input only listed menu options     !!!" << endl;
   cout << "!!!! Please try again.                  !!!" << endl;
   cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
   cout << endl;
}


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 


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
31
32
33
class Word;
#include "words.h"
#include <iostream>
#include <string>
using namespace std;

char* ptr;
int len;

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

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

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

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


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

a red wheel
barrow

glazed with rain
water

beside the white
chickens.


Thank you!
Last edited on
It compiles fine but output is not valid:


Please make a selection:
a) Read a text file
b) Remove words starting with letter
c) Print words to console
d) Quit
-> a

Enter a filename to open: sample.txt
Loaded file and read 15 words.

Please make a selection:
a) Read a text file
b) Remove words starting with letter
c) Print words to console
d) Quit
-> c

so> muchα♥> depends uponá♥> a♥> red wheel♥> barrow> glazed> withα☻> rain└☻> wate
r☻> beside> the white☻>


This is because strlen returns length of a string without the null character.
1
2
3
4
5
6
7
8
9
10
11
12
13
Word::Word(const char* word)
{
    len = strlen(word)+1; // length of the string + 1 null ('\0') character
    ptr = new char[len];
    memcpy(ptr, word, len);
}

Word::~Word()
{
    delete[] ptr; // free unused memory
    ptr = NULL;
    len = 0;
}


You also need to #include <cstring> instead of <string> in word.cpp, and remove using namespace std; from word.h
Last edited on
Topic archived. No new replies allowed.