runs on Mac but not Windows

Hello, I was learning c++ on Mac all the time, now have a teacher who requires code to compile on windows Visual Studio. While my code is fine on VS 2017 Mac just fine it has errors when i try to do that on widows. What is the difference and how do you think i can fix it?

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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <map>
#include <cstring>
#include <stdlib.h>
#include <string>
#include <vector>
#include <string>
#include <regex>
#include <sstream>
#include <fstream>
#include <stack>
#include <iostream>
#include <stdexcept>

using std::invalid_argument;
using std::stringstream;
using std::ifstream;
using std::stack;
using std::smatch;
using std::regex_search;
using std::regex;
using std::vector;
using std::string;
using std::map;
using std::cout;
using std::endl;

class XmlNode {

	string name;
	string contents;
	vector<XmlNode*> sub_nodes;

public:
	XmlNode(const string& name, const string& contents) : name(name), contents(contents) {};

	string getName();
	string getContents();
	vector<XmlNode *> getSubNodes();
	bool hasSubNodes();
	void addSubNode(XmlNode* sub);

	void setContents(const string &string1);

	~XmlNode();
};
string XmlNode::getName() {
	return name;
}

string XmlNode::getContents() {
	return contents;
}

vector<XmlNode *> XmlNode::getSubNodes() {
	return sub_nodes;
}

bool XmlNode::hasSubNodes() {
	return sub_nodes.size() != 0;
}

void XmlNode::addSubNode(XmlNode *sub) {
	sub_nodes.push_back(sub);
}

void XmlNode::setContents(const string &string1) {
	contents = string1;
}

XmlNode::~XmlNode() {
	for (XmlNode *node : sub_nodes) {
		delete node;
	}
}


class XMLParser {

	string file;

public:
	XMLParser(const string &file);
	XmlNode *parse();
};


XMLParser::XMLParser(const string &file) {
	this->file = file;
}

XmlNode *XMLParser::parse()
{
	ifstream fin(file);
	if (!fin) {
		return nullptr;
	}

	stack<XmlNode*> nodes;

	regex open{ "<\\s*?(\\w*?)\\s*?>" };
	regex data{ "<(.*?)>(.*?)</\\1>" };
	regex close{ "</(.*?)>" };

	string str;
	smatch matches;
	smatch smatch1;
	XmlNode* rt;

	while (getline(fin, str)) {
		if (regex_search(str, matches, open)) {
			XmlNode* node = new XmlNode(matches[1], "");
			if (!nodes.empty()) {
				nodes.top()->addSubNode(node);
			}
			else {
				rt = node;
			}
			nodes.push(node);
		}
		if (regex_search(str, matches, data)) {
			nodes.top()->setContents(matches[2]);
		}
		if (regex_search(str, matches, close)) {
			nodes.pop();
		}
	}

	fin.close();

	return rt;
}

class Code3of9ToCharConverter {

	struct Code3of9CharPair
	{
		char character;
		string code;
	};

	static const int PAIRS_ARR_SIZE = 44;
	const Code3of9CharPair code_char_pairs[PAIRS_ARR_SIZE]{
		{ ' ', "011000100" },
	{ '-', "010000101" },
	{ '+', "010001010" },
	{ '$', "010101000" },
	{ '%', "000101010" },
	{ '*', "010010100" },
	{ '.', "110000100" },
	{ '/', "010100010" },
	{ '0', "000110100" },
	{ '1', "100100001" },
	{ '2', "001100001" },
	{ '3', "101100000" },
	{ '4', "000110001" },
	{ '5', "100110000" },
	{ '6', "001110000" },
	{ '7', "000100101" },
	{ '8', "100100100" },
	{ '9', "001100100" },
	{ 'A', "100001001" },
	{ 'B', "001001001" },
	{ 'C', "101001000" },
	{ 'D', "000011001" },
	{ 'E', "100011000" },
	{ 'F', "001011000" },
	{ 'G', "000001101" },
	{ 'H', "100001100" },
	{ 'I', "001001100" },
	{ 'J', "000011100" },
	{ 'K', "100000011" },
	{ 'L', "001000011" },
	{ 'M', "101000010" },
	{ 'N', "000010011" },
	{ 'O', "100010010" },
	{ 'P', "001010010" },
	{ 'Q', "000000111" },
	{ 'R', "100000110" },
	{ 'S', "001000110" },
	{ 'T', "000010110" },
	{ 'U', "110000001" },
	{ 'V', "011000001" },
	{ 'W', "111000000" },
	{ 'X', "010010001" },
	{ 'Y', "110010000" },
	{ 'Z', "011010000" }
	};
	map<string, char> codes;

public:
	Code3of9ToCharConverter();

	char convertSymbolToChar(const string& symbol_string);
	string convertSymbolsToString(const string &symbols);
};


char Code3of9ToCharConverter::convertSymbolToChar(const string& symbol_string)
{
	return codes.at(symbol_string);
}

Code3of9ToCharConverter::Code3of9ToCharConverter() {
	for (int i = 0; i < PAIRS_ARR_SIZE; ++i) {
		codes[code_char_pairs[i].code] = code_char_pairs[i].character;
	}
}

string Code3of9ToCharConverter::convertSymbolsToString(const string &symbols) {
	string result_str;

	for (int i = 0; i < 5; i++) {
		result_str += codes.at(symbols.substr(i * 9, 9));
	}

	return result_str;
}


int main()
{
	XMLParser products_parser("Products.xml");
	XMLParser carts_parser("Carts.xml");

	XmlNode* products_root = products_parser.parse();
	XmlNode* cart_root = carts_parser.parse();

	map<string, double> prod_to_price;
	map<string, string> upper_to_lower;

	for (XmlNode *node : products_root->getSubNodes()) {
		vector<XmlNode *> subs = node->getSubNodes();
		string name = subs[0]->getContents();
		string upper_name;
		for (int i = 0; i < 5; ++i) {
			upper_name += toupper(name[i]);
		}
		double price = atof(subs[1]->getContents().c_str());

		prod_to_price[upper_name] = price;
		upper_to_lower[upper_name] = name;
	}

	Code3of9ToCharConverter cv;

	for (XmlNode *node : cart_root->getSubNodes()) {
		cout << node->getName() << endl;
		double sum = 0;
		for (XmlNode *item : node->getSubNodes()) {
			string upp;
			try {
				upp = cv.convertSymbolsToString(item->getContents());
				double price = prod_to_price[upp];
				cout << upper_to_lower[upp] << "\t" << price << endl;
				sum += price;
			}
			catch (...) {
				cout << "error" << endl;
			}

		}
		cout << "SUM: " << sum << endl << endl;
	}

	delete products_root;
	delete cart_root;

	return 0;
	system("pause");
}

Last edited on
What error message do you get?
The exact errors are indeed paramount.


Meanwhile, you are inconsistent:
1
2
#include <cstring>
#include <stdlib.h> 

should be
1
2
#include <cstring>
#include <cstdlib> 

On line 130 there is an error: potentially uninitialized local pointer variable 'rt' used

What warning level do you use on Mac ?
all I get is a warning as Thomas1965 mentions above.
Are you running on C++11 or newer?, cause you are using ranged base for loops
Last edited on
If you copied the entire project over then all of your linking is going to be to the wrong files. Create a new project, copy and paste your source code but re-link all of the libraries on the Windows machine by hand.
agreed. But, you may be able to fix it without making a new project... I thought there was a target platform / etc settings area and since you copied, those are probably still set to mac, but easy enough to flip? That may be a rabbit hole... its really easy to make new project, but finding that last hard to spot setting to change (seems to happen any time you dig into the deep settings) can be frustrating ... whatever you think is best.

closed account (E0p9LyTq)
asder wrote:
Are you running on C++11 or newer?

Visual Studio 2017 uses C++14 or newer, with C++14 as default.

@amayakichi,

I changed line 107:
XmlNode* rt = NULL;

That removes your uninitialized variable problem, it compiles now.

Not having your data file the program does nothing. It runs and immediately ends.

Depending on how you run your program, using the VS IDE or from a command prompt, your data file may not be located where the program expects to find it.

Using the VS 2017 IDE on Windows requires the file to be in the same directory with your source file(s).
Last edited on
Topic archived. No new replies allowed.