Hello! Why does visual studio closing my code?!

Hello!!

I wrote some code, and when I built it, everything is going good, there is no errors, but when I try to run it with Ctrl+F5, it runs.. when it finishes running, just closes without writing me "press and key to continue.."

If I put there a "Break point" or "system("pause")" it does work!

(The auto reviewer of Moodle writes me "Out of memory"...)

What can cause it?

Thanks!!
Why would it NOT do that?

Think about what's happening here. You are running a program. It's not Visual Studio "running your code". A computer program is being made. An executable file is being created and placed on your hard-drive, and then it's being run, just like any other executable file. It's just like every other computer program on your PC.

What happens when a computer program finishes? What happens when Microsoft Word finishes, or Firefox finishes? They go away. They close. They stop running and disappear. That's what's meant to happen.

Visual Studio isn't "closing your code". Your computer program, that YOU wrote, is finishing, and when a computer program on your PC finishes, that's what happens.
Last edited on
OK.... so I wasn't clear..

So that why I wrote
I try to run it with Ctrl+F5, it runs.. when it finishes running, just closes without writing me "press and key to continue.."


Because all the other code was not just closed without pressing any key! Even when he had finished running...
Are you saying that your program crashed with an "Out of memory" error?

If so, you'd need to show us your code in order to help identify any problems.
This code is part of exercise we should do in our place of study... and we need to upload it via moodle. Any way, there is an auto reviewer that debug our code, I think that its compiler is Code Blocks, but I'm not sure about it...

So... in visual studio the code just closed without any problem.
In Moodle ( Code Blocks ? ) can not evaluate my exercise - writing "Out of memory".

I wrote it in a brackets cause it might give some clue what went wrong ...
And about to show the code - I don't have any problem... but It's quite long... so I hoped someone has an idia that can lead me to the problem.
(Again, I assume because I have no errors in visual studio, that there is something fundamental...)

Thanks!!
When you say "I have no errors in visual studio" I presume you mean there are no compiler errors. However, it seems you have a run-time error, which is something the compiler cannot identify, since it doesn't happen until the program is actually run.

Again - it's not possible to say what the problem might be without seeing the code.
This is a Linked list:
List.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "List.h"
//------------------------------------------------
//  class Link implementation
//------------------------------------------------
List::Link::Link( int val, Link* nxt) : value(val), next(nxt)   {}

List::Link::Link(const Link& source) : value(source.value),next(source.next)  {}
 
//--------------------------------------------
//  class List implementation
//--------------------------------------------
List::List(): head(NULL)
{
	//cout << "CTOR\n";
}

// Copy constructor
List::List(const List &l) 
{
	//cout << "Copy CTOR\n";
	Link *src, *trg;
	if(l.head == NULL)
		head = NULL;
	else
	{  // copy the list
		head = new Link((l.head)->value, NULL);
		src = l.head;
		trg = head;
		while(src->next != NULL)
		{
			trg->next = new Link((src->next)->value, NULL);
			src = src->next;
			trg = trg->next;
		}
	}
}


void List::operator=(const List& l)
{
	if(&l != this)
	{
		this->clear();
		Link *src, *trg;
		if(l.head == NULL)
			head = NULL;
		else
		{  // copy the list
			head = new Link((l.head)->value, NULL);
			src = l.head;
			trg = head;
			while(src->next != NULL)
			{
				trg->next = new Link((src->next)->value, NULL);
				src = src->next;
				trg = trg->next;
			}
		}
	}
}

// Destructor
List::~List()
{
	//cout << "DTOR: ";
	//print();
	clear();
}

void List::clear()
{
	// empty all elements from the List
	Link* next;
	for (Link *p = head; p != NULL; p = next)
	{
		// delete the element pointed to by p
		next = p->next;
		p->next = NULL;
		delete p;
	}
	// mark that the List contains no elements
	head=NULL;
}

// test to see if the List is empty
// List is empty if the pointer to the head
// Link is null
bool List::isEmpty() const
{
	return head == NULL;
}
 
//Add a new value to the front of a Linked List
void List::add(int val)
{
	head = new Link(val, head);
	if(head == NULL) 
		throw "failed in memory allocation";
}

// return first value in List
int List::firstElement() const
{
	if (isEmpty())
		throw "the List is empty, no first Element";
	return head->value;
}

bool List::search(const  int &val) const
{
	// loop to test each element
	for (Link* p = head; p != NULL ; p = p->next)
		if (val == p->value)
			return true;
	// not found
	return false;
}

void List::print() const
{
	for (Link* p = head; p != NULL ; p = p->next)
		cout << p->value << ((p->next != NULL) ? " -> " : "\n"); 
}

void List::removeFirst()
{
	// make sure there is a first element
	if(isEmpty())
		throw "the List is empty, no Elements to remove";
	// save pointer to the removed node
	Link* p = head;
	// reassign the first node
	head = p->next;
	p->next = NULL;
	// recover memory used by the first element
	delete p;
} 
void List::reverse()//revesing the list, in case each time the bigger element is first.
{
	List temp;
	while (!isEmpty())
	{
		temp.add(firstElement());
		removeFirst();
	}
	*this=temp;
}

ostream& operator<<(ostream& out, List& l)
{
	List::Link*p = l.head;
	if (l.head != NULL)
	{
		do
		{
			out << p->value << " ";
			p = p->next;
		} while (p != NULL);
	}
	return out;
}

istream &operator>>(istream& in, List& l)
{
	int help = 0;
	in >> help;

	if (l.head == NULL)
	{
		l.add(help);
		in >> help;
	}
	do
		{
			l.add(help);
			in >> help;
	} while (l.head->value == help || l.head->value<help);
	l.reverse();//because the last element was the bigger, we need to reverse.
	return in;
}

void List::insert(int key)//inserting a new element, Keeps ascending
{
	List::Link *first = head;//i will use it as the first element in the new list during insertion 
	if (head != NULL)
	{
		List::Link *second = head->next; //will use it as second element during insertion
		if (head->value >= key)//extreme case if it will needed to be insert to the first place
		{
			add(key);
			return;
		}
		do//to the middle...
		{
			if (second->value>=key)
			{
				first->next = new Link(key, second);
				return;
			}
			first = second;
			second = first->next;
		} while (second != NULL);
		first->next = new Link(key, NULL);//if not first and not to the middle..
	}
}

void List::remove(int key)//remove element by value
{
	if (!search(key))
		throw "value not found";
	List::Link *first = head;
	if (head != NULL)
	{
		if (head->value == key)//extreme case if it the first element
		{
			removeFirst();
		}
		else
		{
			List::Link *second = first->next;//will use it as second element during insertion
			do
			{
				if (second->value == key)
				{
					first->next = second->next;
				}
				first = second;
				second = first->next;
			} while (second != NULL);
		}
	}
}

List merge(List lst1, List lst2)
{
	List temp; //the new sorting list
	List::Link *list1 = lst1.head; //list a
	List::Link *list2 = lst2.head; //list b

	while (list1 != NULL && list2 != NULL)
	{
		if (list1->value < list2->value) //cheking if the value of the first smaller then the second and coefficient on
		{
			temp.add(list1->value);
			list1 = list1->next;
		}
		else // else copy from list b and coefficient on
		{
			temp.add(list2->value);
			list2 = list2->next;
		}
	} 

 //one of the lists must be empy (less they are equal..)
	while (list1!=NULL)
	{
		temp.add(list1->value);
		list1 = list1->next;
	}
	while (list2!=NULL)
	{
		temp.add(list2->value);
		list2 = list2->next;
	}
	temp.reverse();//becuse the bigger element was the last..
	return temp;
}


List makeSet(List& lst) //remove duplicate elements.
{
	List temp; //new list
	List::Link *list = lst.head;
	int flag = list->value;//save the value of the last copied element.
	temp.add(list->value);
	while (list != NULL)
	{
		if (list->value != flag)
		{
			temp.add(list->value);
			flag = list->value;
		}
		if (list != NULL)
			list = list->next;
	}
	temp.reverse();
	lst = temp;
	return lst;
}

List.h:
[code]
#pragma once
#include <iostream>
using namespace std;

//------------------------------------------------
//  class List
//      arbitrary size Lists
//      permits insertion and removal 
//      only from the front of the List
//------------------------------------------------
class List
{
	private:
	//--------------------------------------------
	//  inner class link
	//  a single element for the linked List 
	//--------------------------------------------
	class Link
	{
	public:
		// constructor
		Link(int linkValue, Link *nextPtr);
		Link (const Link &);
		// data areas
		int value;
		Link * next;
	};	

	public:
	Link* head;
	// constructors
	List();
	List(const List&); 
	~List(); 
	
	// operations
	void add( int value);
	int firstElement() const;
	bool search(const int &value) const;
	bool isEmpty() const;
	void removeFirst();
	void print() const;
	void operator=(const List& other);
	void clear();
	void insert(int);
	void remove(int);
	void reverse();

	friend ostream &operator<<(ostream &out, List &myList);
	friend istream &operator>>(istream &in, List &myList);

	friend List merge(List, List);
	friend List makeSet(List&);

};


And the main is very simple...

Thanks!!
Last edited on
And the main is very simple...

So simple that you think we can guess what it is?

I think that its compiler is Code Blocks,

That's not a compiler. If you're going to write programs, you need to be able to use the right words to describe things, otherwise other people won't understand you. http://www.cplusplus.com/articles/o8TbqMoL/

Last edited on
Any way my code is long, so I didn't want to add another code that not necessary (the pronlem is not there...)

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


int main()
{
	List lst1, lst2, mergedList;

	cout << "enter sorted values for the first list:" << endl;
	cin >> lst1;
	cout << "enter sorted values for the second list:" << endl;
	cin >> lst2;

	mergedList = merge(lst1, lst2);
	cout << "the new merged list: " << mergedList << endl;

	makeSet(mergedList);
	cout << "the new merged set: " << mergedList << endl;

	return 0;
}


Because I wasn't sure what the compiler is, I have written in brackets, and I said what I use (visual studio..) , and for another help what else happened to me (In the moodle..)


Thanks!

About this article, so after that I tried to figure out which compiler Moodle uses but not realized...
Last edited on
enter sorted values for the first list:
1
10
20
30
40
50
60
70
0
enter sorted values for the second list:
11
23
33
43
53
63
0
the new merged list: 1 10 11 20 23 30 33 40 43 50 53 60 63 70 
the new merged set: 1 10 11 20 23 30 33 40 43 50 53 60 63 70 


Code works fine. No memory problems in it.

1
2
	makeSet(mergedList);
	cout << "the new merged set: " << mergedList << endl;

This looks wrong, though

Last edited on
Instead of taking input from std::cin, I changed main to read from a file.
1
2
    std::ifstream fin1("data1.txt");
    fin1 >> lst1;


My file contained 20 integers in ascending order.

The program never terminates, there's an infinite loop during that input operation.

The question though is what input does the moodle test use?

Aye, it does rely on the values of the input in order to stop waiting for input, in a way that's not very clear from the code.
Hey,

The moodle's values:
Test 1:
1 3 5 1
2 4 1
--- Expected output (text)---
enter sorted values for the first list:
enter sorted values for the second list:
the new merged list: 1 2 3 4 5
the new merged set: 1 2 3 4 5

Test 2:
--- Input ---
1 3 5 1
6 8 1
--- Expected output (text)---
enter sorted values for the first list:
enter sorted values for the second list:
the new merged list: 1 3 5 6 8
the new merged set: 1 3 5 6 8

Test 3:
--- Input ---
3 5 1
1 2 1
--- Expected output (text)---
enter sorted values for the first list:
enter sorted values for the second list:
the new merged list: 1 2 3 5
the new merged set: 1 2 3 5

Test 4:
--- Input ---
1 2 3 4 1
0 2 4 5 1
--- Expected output (text)---
enter sorted values for the first list:
enter sorted values for the second list:
the new merged list: 0 1 2 2 3 4 4 5
the new merged set: 0 1 2 3 4 5

I really do not know what values ​​you use ... Anyway for me it works fine, including the moodle's values. (except for the problem above.. )

Moschops, I don't understand..

Thanks!
How does the program know when you have stopped typing in inputs?

I'm saying that's difficult to pick out from the code, and code that the reader cannot understand is bad code.
Last edited on
The program knows when entering an element which is smaller then the previous one..

About the code... Your'e right!
We got the base on which we needed to add functions..
Anyway for me it works fine, including the moodle's values.

Does that mean the problem is now resolved?

(except for the problem above.. )

I thought there were TWO problems above.
1. your program didn't (apparently) terminate correctly.
2. the moodle automated testing gave an "Out of memory" error.

We got the base on which we needed to add functions..

Though I wouldn't rule out the possibility of problems in any part of the code, it could be useful to state which parts were provided, and which parts you had to write yourself. (that's if there is still a problem).
Last edited on
Works fine, i.e as it was at the begining, if I run it with a break point works good... but there is probably (as you say) a run time error.. and I guess that the problem in visual and moodle is the same one... (because there is no other problem in sight).

The part of the code that I wrote is from reverse function (including) and below...

The reason that I didn't mention it because even when I put all my code as a comment (and in the main almost all the code...) it still closing my program.

So I guess that the problem is not on in my part.... but now it hit me where should focus on, although I have no clue..

(And about what did my friends, they got another code..)

Thanks!
In VS to prevent the console window from closing after executing when running with ctrl+F5 the project subsystem needs to be set to console: Project Properties -> Linker -> System set subsystem to console for all configurations.
But it must be the same all the times...
because only in this code it was close and the other is left open, so it means that the issue is here.
Topic archived. No new replies allowed.