.exe file crashes outside VS but work inside

I have a program that works well inside visual studio when I launch it via local windows debugger, but when I open the .exe file, it outputs some stuff and takes some input, then it crashes at some moment when it should display other outputs. How can I fix this?
I ran Dependency Walker on the .exe file and it outputted this:
1
2
3
4
Error: At least one required implicit or forwarded dependency was not found.
Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.
Error: Modules with different CPU types were found.
Warning: At least one delay-load dependency module was not found.

Please note that I'm kind of a beginner.
What my program does is inputting information about the price and ticker of the stock and then doing some calculations and outputting the result. I made a class named stock that has instance variables which are the properties of each stock.
Source code:
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
#include <iostream>
#include <vector>
#include <iomanip>
#include <string>
#include <algorithm>
#include "Stock.h"
using namespace std;



int main() {
	cout <<"                                                Expectancy Calculator" << endl;
	int n, nClone,counter=0;
	cout << "How many expectancies do you want to calculate? " << flush;
	cin >> n;
	nClone = n;
	Stock* stock = new Stock [n];
	vector<double>expectancy;
	while (n--) {

		cout << "Enter ticker> " << flush;
		cin >> stock[counter].ticker;
		for (auto& x : stock[counter].ticker) //captialize ticker
			x = toupper(x);
		cout << "Enter current price> " << flush;
		cin >> stock[counter].currentPrice;
		cout << "Enter target price> " << flush;
		cin >> stock[counter].targetPrice;
		cout << "Enter price stop loss price> " << flush;
		cin >> stock[counter].lossPrice;
		cout << "Enter chance of success> " << flush;
		cin >> stock[counter].successChance;
		stock[counter].lossChance=1-stock[counter].successChance;
		
		stock[counter].expectancy = (((stock[counter].targetPrice- stock[counter].currentPrice)/ stock[counter].currentPrice)) * stock[counter].successChance - ((stock[counter].currentPrice-stock[counter].lossPrice) / stock[counter].currentPrice) * stock[counter].lossChance;
		expectancy.push_back(stock[counter].expectancy);
		counter++;
		if (n != 0) {
			cout << "Onto the next stock" << endl;
		}
		cout << "------------------------------------------------------------------------------------------------------------------------" << endl;

	}
	
	sort(expectancy.begin(), expectancy.end(),greater<float>());
	int j;
	
	printf("%c[4mResults\n%c[0m", 27, 27);

	for (int i = 0; i < nClone; i++) {
		for (int k = 0; k < nClone; k++) {
			if (stock[k].expectancy == expectancy[i]) {
				j = k;
			}
		}
		cout <<i+1 <<". " <<stock[j].ticker << ": " << expectancy[i]*100 <<"%" << endl;
	}

	return 0;
}
Last edited on
The magic 8 crystal ball says: "question too vague, include source code in post."
If you're using visual studio, you can attach a debugger to the program even when you run it outside of visual studio. Debug --> Attach to Process ...
Put a breakpoint after the user input, and then you can step through the code to see when it crashes.

A guess: The difference is that your working directory is different when you directly run the exe outside of visual studio, so you are assuming a particular file exists when it doesn't.

If you're issue isn't that it crashes but simply closes too quickly, then open your command prompt and run the program within the command prompt instead of having it use an evanescent window.

Edit: You added more information in your OP.
Those dependency walker errors might be false positives. If your program runs initially but crashes at a later point, I still suggest my method.
Last edited on
I ran Dependency Walker on the .exe file

Dependency Walker is an ooooooooold tool that doesn't really work any more. There is an updated app available:

https://github.com/lucasg/Dependencies
I did what @Ganado suggested. I made debug and attached to the application process but I don't really understand what's happening. I'm kind of a beginner as mentioned above. Can you please clarify more what I exactly should do? Anyway, I provided the source code in the question above.
Last edited on
First note: You are using double. In a real program, you would never use floating-point numbers to represent exact money amount, but that's a bit beyond the scope of what's necessary here. But second, you use greater<float>() in your sort method. You should use greater<double> here, to match your data type.

Second note: If your exe were actually crashing, it would most likely generate an Event log in your event viewer. After running your exe and letting it "crash", if you open your Event log and navigate to Windows Logs --> Application (wait for it to load), do you see any error messages that relate to your exe?

Another minor note for the future: please don't edit you OP more. It makes the thread less linear; just add additional information in new posts.

I suggest looking up a tutorial on Visual studio debugging or breakpoints. It's hard to visually show you what exactly to do, beyond what I already mentioned: Start your exe. Then in Visual Studio, go to Debug --> Attach to Process --> then find your exe in the list, and click Attach. Then place a break point after your cin >> statements, and step through your code with F11 (or Debug --> Step Into) once you're debugging.

I ran your code (creating my own Stock class)
1
2
3
4
5
6
7
8
9
10
class Stock {
public:
	string ticker;
	double currentPrice;
	double targetPrice;
	double lossPrice;
	double successChance;
	double lossChance;
	double expectancy;
};


I don't see any flagrant errors other than the minor precision issue with float vs. double.

I think your problem is the well-known beginner issue: https://cplusplus.com/forum/beginner/1988/

You are double-clicking on the exe, which produces a temporary window, and once the program is over, this window disappears. This has been discussed ad nauseum in the linked thread above, but what I suggest is running the program through an existing cmd prompt instead of double-clicking it from your File explorer.

From your File explorer, go to the folder that contains your exe.
Then, click into the whitespace of the address bar, and type "cmd" and hit enter.
Then, in the command window, type the name of your exe and hit enter to run it.
Last edited on
@Ganado I figured it out! The problem got be solved when I inputted anything before returning 0. However, I don't know why! You can see here in my new code that I've added int whatever; cin>>whatever; before returning 0 and it just worked! Have you got any idea why? Thanks for your support!
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
#include <iostream>
#include <vector>
#include <limits>
#include <iomanip>
#include <string>
#include <algorithm>
#include "Stock.h"
using namespace std;

string returnOrdinal(int number);

int main() {
	cout << "                                                Expectancy Calculator" << endl;
	int n, nClone, counter = 0;
	cout << "How many expectancies do you want to calculate? " << flush;
	cin >> n;
	nClone = n;
	Stock* stock = new Stock[n];
	vector<double>expectancy;
	while (n--) {

		cout << "Enter " << returnOrdinal(counter + 1) << " ticker> " << flush;
		cin >> stock[counter].ticker;
		for (auto& x : stock[counter].ticker) //captialize ticker
			x = toupper(x);
		cout << "Enter current price> " << flush;
		cin >> stock[counter].currentPrice;
		cout << "Enter target price> " << flush;
		cin >> stock[counter].targetPrice;
		cout << "Enter price stop loss price> " << flush;
		cin >> stock[counter].lossPrice;
		cout << "Enter chance of success> " << flush;
		cin >> stock[counter].successChance;
		stock[counter].lossChance = 1 - stock[counter].successChance;

		stock[counter].expectancy = (((stock[counter].targetPrice - stock[counter].currentPrice) / stock[counter].currentPrice)) * stock[counter].successChance - ((stock[counter].currentPrice - stock[counter].lossPrice) / stock[counter].currentPrice) * stock[counter].lossChance;
		expectancy.push_back(stock[counter].expectancy);
		counter++;
		if (n != 0) {
			cout << "Onto the next stock" << endl;
		}
		cout << "------------------------------------------------------------------------------------------------------------------------" << endl;

	}

	sort(expectancy.begin(), expectancy.end(), greater<double>());
	int j;

	cout << "Results" << endl;

	for (int i = 0; i < nClone; i++) {
		for (int k = 0; k < nClone; k++) {
			if (stock[k].expectancy == expectancy[i]) {
				j = k;
			}
		}
		cout << i + 1 << ". " << stock[j].ticker << ": " << expectancy[i] * 100 << "%" << endl;
	}
	int whatever;
	cin >> whatever;
	return 0;
}

string returnOrdinal(int number) {
	switch (number) {
	case 1: return "first";
	case 2: return "second";
	case 3: return "third";
	case 4: return "fourth";
	case 5: return "fifth";
	case 6: return "sixth";
	case 7: return "seventh";
	case 8: return "eighth";
	case 9: return "ninth";
	case 10: return "tenth";
	}
}
Last edited on
Slightly simplified:

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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cctype>
#include <functional>

struct Stock {
	std::string ticker;
	double currentPrice {};
	double targetPrice {};
	double lossPrice {};
	double successChance {};
	double lossChance {};
	double expectancy {};
};

std::string returnOrdinal(size_t number);

int main() {
	std::cout << "                                                Expectancy Calculator\n";

	size_t n {};

	std::cout << "How many expectancies do you want to calculate? ";
	std::cin >> n;

	const auto nClone {n};

	std::vector<Stock> stock(n);
	std::vector<double>expectancy;

	for (size_t counter = 0; counter < n; ++counter) {
		std::cout << "Enter " << returnOrdinal(counter + 1) << " ticker> ";
		std::cin >> stock[counter].ticker;

		for (auto& x : stock[counter].ticker)	//capitalize ticker
			x = static_cast<char>(std::toupper(static_cast<unsigned char>(x)));

		std::cout << "Enter current price> ";
		std::cin >> stock[counter].currentPrice;

		std::cout << "Enter target price> ";
		std::cin >> stock[counter].targetPrice;

		std::cout << "Enter price stop loss price> ";
		std::cin >> stock[counter].lossPrice;

		std::cout << "Enter chance of success> ";
		std::cin >> stock[counter].successChance;

		stock[counter].lossChance = 1 - stock[counter].successChance;
		stock[counter].expectancy = (((stock[counter].targetPrice - stock[counter].currentPrice) / stock[counter].currentPrice)) * stock[counter].successChance - ((stock[counter].currentPrice - stock[counter].lossPrice) / stock[counter].currentPrice) * stock[counter].lossChance;

		expectancy.push_back(stock[counter].expectancy);

		if (counter != n - 1)
			std::cout << "Onto the next stock\n";

		std::cout << "------------------------------------------------------------------------------------------------------------------------\n";
	}

	std::sort(expectancy.begin(), expectancy.end(), std::greater<double>());

	size_t j {};

	std::cout << "Results\n";

	for (size_t i = 0; i < nClone; ++i) {
		for (size_t k = 0; k < nClone; ++k)
			if (stock[k].expectancy == expectancy[i])
				j = k;

		std::cout << i + 1 << ". " << stock[j].ticker << ": " << expectancy[i] * 100 << "%\n";
	}
}

std::string returnOrdinal(size_t number) {

	const static char* const nums[] {"zero", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"};

	if (number <= 10)
		return nums[number];

	return ("Unknown");
}

Last edited on
when a program works in visual and not outside it, the first places I would look:

1) did you open a file and fail to validate if that went well with appropriate error messages? The path inside VS and the path outside are not the same, and a file that you think it should see may suddenly not be in its visible scope.

2) libraries. this is similar to the above -- a dll or whatever that your program needs is visible in the tool and not outside it. These are much more obvious: it flat out tells you 'cannot open blah.dll" somewhere in the error spew for the crash.

3) some sort of bug. the visual studio debugger env may do things to your program for debug reasons that make it work (eg initialize all to zero for you) which may not happen in (release mode or running outside the tool). To see this, try running the program in release mode in the tool. If it fails, it only works because the debugger is helping you, and you have some bug hunting to do.

4) microsoftisms. Here again these usually give you a blatant error message, often having something to do with a manifesto or other gibberish. You can fix the settings in the tool to make it knock it off, or you can move the required files around or fix the path (there that is again!) to cure it.

It sounds like you found the issue, thankfully. But these may help later. Your fix indicates that it maybe was just a speed of light problem. Your program is rather quick and small, it can open, execute, and close in the blink of an eye. Adding a cin makes it wait for you to type something, and you can see the output until you type. Does the program window close when you type something into whatever? If so, that is all it ever was. To 'fix' it you can open a cmd window and run inside that, where it will keep the output buffer on screen, or you can do what you did (effectively press a key to end program). All that is going on is the program (a console window) that has the output display is being closed before you can read it.
Last edited on
Bassel, it's because the program is over once main returns. And once the program is over, the temporary window disappears. So you prevented the program from ending by having it wait for user input.
Registered users can post here. Sign in or register to post.