C++ Console Application only partially working on other computer?

Pages: 12
Hi everyone,

I created my first somewhat useful C++ application using Code Blocks. It basically allocates a loss across multiple insurance companies based upon (1) time on the risk and (2) each carrier's policy limits.

The program runs through and asks for when the loss began, when it was disocvered, the identity of each carrier, each carriers respective time on the risk, each carrier's policy limits and the total cost of the loss.

The .exe file runs fine on the computer I created it on, but when I loaded it on my work computer by transfering the .exe file to a thumb drive and copying it to my work computer, the application closes before finishing.

Basically, it runs through everything fine, but when it gets to the last step, which is to aks for the cost of the loss, it just closes and never gets to that step.

Any idea why? Both computers are running Windows 7 and I think both are 32-bit. Thanks,

Kevin
try to run this program from a command prompt. Looks like windows just closes without pausing after completing last step.
Or it din't ask you when should? In that case post your code here, use code tags (looks like <> on right side panel)
Last edited on
Hi. Thanks. The program gets to the point where it asks for the total cost of the loss, and after the user inputs the costs, it just closes. It's supposed to take that number and multiply it by each company's percentage of liablity and calculate what each company owes. Everything works fine until after the user inputs the cost of the loss, then it just exits and never finishes.

I'll post up the code in a minute. Thanks,

Kevin
Maybe it calculates everything, but closes window immideately after, so you cannot read output. Try to run this program from a command prompt: Navigate into folder containing your program with explorer, type "cmd" in address bar, type name of your program in command prompt.
Thanks. That may be it since it's the last calculation the program runs before closing. Is there a way to stop that from happening? Such as to use "system pause" or whatever at the end?

I tried running it from a command prompt, but it kept saying that it couldn't find the program.

I tried to post the code, but I got an error saying it was too long. Here's the last part of the code. Basically, I have a buch of vectors set up to take the different user inputs identified below:

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
 cout << "Total time of insurnace in months is: " << insTotalTimeMonths << endl;

    if (insTotalTimeMonths != LeakObject.calculateTimeOfLeak()){
        cout << "Warning! Total time of insurance is different from trigger period." << endl;
    }



    for (vector<string>::size_type i = 0; i < numInsCos; i++){
        cout << "Enter " << InsCos[i+0] <<"'s annual policy limits for " << InsCosYearBegin[i+0] << " through " << InsCosYearEnd[i+0] << "." << endl;
        cin >> AnnualPolicyLimits[i+0];
        PolicyLimitsPerMonth[i+0] = AnnualPolicyLimits[i+0] / 12;
        cout << InsCos[i+0] << "'s monthly policy limits were $" << PolicyLimitsPerMonth[i+0] << " per month." << endl;
    }

    for (vector<string>::size_type i = 0; i < numInsCos; i++){
        cout << InsCos[i+0] << "'s annual policy limits for " << InsCosYearBegin[i+0] << " through " << InsCosYearEnd[i+0] << " were $" << AnnualPolicyLimits[i+0] << " per year." << endl;
    }

    for (vector<string>::size_type i = 0; i < numInsCos; i++){
        TotalMonthlyLimitsPerCarrier[i+0] = PolicyLimitsPerMonth[i+0] * EachCarriersMonths[i+0];
        cout << InsCos[i+0] << "'s total insurance is: $" << TotalMonthlyLimitsPerCarrier[i+0] << endl;
    }

    int TotalInsuranceAllCarriers = 0;

    for (vector<string>::size_type i = 0; i < numInsCos; i++){
         TotalInsuranceAllCarriers += TotalMonthlyLimitsPerCarrier[i+0];

    }

    cout << "Total insurance from all carriers is: $" << TotalInsuranceAllCarriers << "." << endl;


    for (vector<string>::size_type i = 0; i < numInsCos; i++){
        EachCarriersPercentLiable[i+0] = ((float) TotalMonthlyLimitsPerCarrier[i+0] / TotalInsuranceAllCarriers);
        cout << InsCos[i+0] << "'s percentage of liablity is " << EachCarriersPercentLiable[i+0] * 100 << "%." << endl;

    }

    cout << "Enter the total remediation expenses: " << endl;
    int TotalRemediationExpenses = 0;
    cin >> TotalRemediationExpenses; // this is where the program exits. 

    for (vector<string>::size_type i = 0; i < numInsCos; i++){
             cout << InsCos[i+0] << "'s alliquot share of remediation expenses is: $" << float (EachCarriersPercentLiable[i+0] * TotalRemediationExpenses) << endl;
    }

    };
Last edited on
I just added cin.get() to the end of the program, before the end of main, but it still closes after the last for loop?? I'm confused because the .exe file works fine on my laptop.

Kevin
closed account (Dy7SLyTq)
try cin.clear
edit: forgot the ()
so cin.clear()
Last edited on
closed account (3qX21hU5)
My advice would be to add this code to the bottom of your code

1
2
3
4
5
    std::cout << "Press ENTER to exit...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

    return 0; // The return 0 in main
}


This will make sure the command prompt will stay open until the user presses enter, because most likely the problem is like MiiNi is saying the program believe it has done everything it needs to do and can close. So basically you have 2 options. One is to put the code (Or one like it) which asked them to press a button before closing and stays open until then. Or two is to run your program directly from the command prompt, (IE startmenu->run->cmd then navigate to the exe for your program and run it within the command prompt). Either of them options should work, but let us know if this doesn't fix it.

EDIT: I know I said I wouldn't respond to your posts DTSCode but you are giving bad information here (Surprise Surprise?). But cin.clear() wont fix the problem, all that does is clears the state of the stream. I.E. if the stream is in a error state it will reset the bits so that it is not showing a error state. It is important to know that just because you run cin.clear or a similiar clear function doesn't mean it corrects the error for you (You have to do this manually) it just resets the bits in the stream so that it no longer shows that it is in a error state.
Last edited on
closed account (Dy7SLyTq)
@zereo: what exactly does cin.ignore do?
cin.clear() didn't work either.
closed account (Dy7SLyTq)
you know, ultimately, you can do cin>> to a char getch()
closed account (3qX21hU5)
Kevin try my example and let me know if that works. You probably have multiple stuff stuck in the buffer that is the reason why cin.ignore() and cin.get() won't work.


you know, ultimately, you can do cin>> to a char getch()

That is sloppy and error prone
Last edited on
closed account (Dy7SLyTq)
i know its sloppy im saying it can be worst case scenario actually system("pause") would be worst case scenario
Zereo,

I put the code in you suggested, but I got an error in Code Blocks saying 'numeric_limits' is not a member of 'std'.

I did try to just putting:

1
2
3
int l = 0;
cout << "press something" << endl; 
cin >> l; 


This worked, but is kind of strange to have at the end of the program. At least it confirms that the problem is the system exiting before displaying the answer.

Thanks.

Kevin
closed account (3qX21hU5)
Sigh so why recommend a sloppy worst case scenario? Also NEVER use system("pause"); ever. Especially in this case if the OP is going to use this maybe in his office to help with claim related information. So since system() anything is proven to have major security issues along with other issues it would be a very bad thing to use. Most AV's will give warnings because of system() anything.
Last edited on
closed account (3qX21hU5)
Sorry about that Kevin forgot to say you need to include #include <limits> . other then that the code should work, let me know if you run into any other problems.

If you want to read up more on why the program does this you can check out this topic here http://www.cplusplus.com/forum/beginner/1988/
Last edited on
Zereo,

I'm actually an environmental law attorney... I'm learning C++ for the fun of it, as strange as that may be, lol.

Carter-Wallace is a case in New Jersey that deals with the allocation of a continuing loss over a long period of time when multiple insurance carries insured the subject-property during that period. It says that the loss should be divided up based upon the amount of time each carrier insured the property, and their annual policy limits.

I do these calculations a couple of times a week on average, and I thought it would be helpful to have a little program that would do the work and be less error prone than doing it manually. After all the time I put into this, I probably could have done it manually about 1,000,000 times, lol.

Kevin
I'll post my whole code in two separate posts, so you can see what it looks like. I'm not sure what you are referring to regarding stuff getting stuck in the buffer. Let me know what you think about the code. Thanks again.
Part I:

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
#include <iostream>
#include <vector>
using namespace std;

int getLeakBeginDate();
void setLeakBegin(int x, int y);
void setLeakEnd(int x, int y);
int getLeakEndDate();
int calculateTimeOfLeak();


class LeakCalc{

    private:
        int leakBeginY;
        int leakBeginM;
        int leakBeginDate;
        int leakEndY;
        int leakEndM;
        int leakEndDate;
        int totalTimeOfLeak;

    public:
        void setLeakBegin(int a, int b){
        leakBeginY = a;
        leakBeginM = b;
        };

        void setLeakEnd(int x, int y){
        leakEndY = x;
        leakEndM = y;
        };


        int getLeakBeginDate(){
            leakBeginDate = leakBeginY * 12 + leakBeginM;
            return leakBeginDate;
        };

        int getLeakEndDate(){
            leakEndDate = leakEndY * 12 + leakEndM;
            return leakEndDate;
        };

        int calculateTimeOfLeak(){
            totalTimeOfLeak = leakEndDate - leakBeginDate;
            return totalTimeOfLeak;
        };
};

int main()
{
    int a, b, c, x, y;
    int numInsCos = 0;

    cout << "Enter the year the contamination began" << endl;
    cin >> a;
    while(a < 1900 || a > 2020){
        cout << "Invalid date.  Please enter the date as a four digit number." << endl;
        cout << "(e.g., 1994)" << endl;
        cin >> a;
    };

    cout << "Enter the month of the year the contamination began" << endl;
    cin >> b;
    while (b < 0 || b > 12){
        cout << "Invalid date.  Please enter a valid date in numerical format." << endl;
        cout << "(i.e., 2 for February)" << endl;
        cin >> b;
    };

    LeakCalc LeakObject;
    LeakObject.setLeakBegin(a, b);
    LeakObject.getLeakBeginDate();

    cout << "What year was the contamination discovered?" << endl;
    cin >> x;
    while (x < a){
        cout << "Invalid year." << endl;
        cout << "Year must be " << a << " or after." << endl;
        cin >> x;
    };

    cout << "What month was of the year was the contamination discovered?" << endl;
    cin >> y;
    while (y < 0 || y > 12){
        cout << "Invalid date.  Please enter a valid date in numerical format." << endl;
        cout << "(i.e., 2 for February)" << endl;
        cin >> y;
    };

    LeakObject.setLeakEnd(x, y);
    LeakObject.getLeakEndDate();


    cout << "The tank leaked for a total of " << (LeakObject.calculateTimeOfLeak() / 12) << " years and " << (LeakObject.calculateTimeOfLeak() % 12) << " months." << endl;
    cout << "This is a total of " << LeakObject.calculateTimeOfLeak() << " months." << endl;

    cout << "How many insurance companies insured the property during these " << LeakObject.calculateTimeOfLeak() << " months?" << endl;
    cin >> numInsCos;
Part II:

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
vector <string> InsCos;
	InsCos.resize (numInsCos);

    for (int i = 0; i < numInsCos; i++){
        cout << "Enter name of insurance company " << i+1 << ":" << flush;
        cin >> InsCos[i];
    }

    for (vector<string>::size_type i = 0; i < numInsCos; i++){
    	cout << "Insurance company " << i+1 << " is: " << InsCos[i+0] << endl;
    }

    vector <int> InsCosYearBegin;
    vector <int> InsCosMonthBegin;
    vector <int> InsCosYearEnd;
    vector <int> InsCosMonthEnd;
    vector <int> AnnualPolicyLimits;
    vector <float> PolicyLimitsPerMonth;
    vector <int> EachCarriersMonths;
    vector <int> TotalMonthlyLimitsPerCarrier;
    vector <float> EachCarriersPercentLiable;

    InsCosYearBegin.resize (numInsCos);
    InsCosMonthBegin.resize (numInsCos);
    InsCosYearEnd.resize (numInsCos);
    InsCosMonthEnd.resize (numInsCos);
    AnnualPolicyLimits.resize (numInsCos);
    PolicyLimitsPerMonth.resize (numInsCos);
    EachCarriersMonths.resize (numInsCos);
    TotalMonthlyLimitsPerCarrier.resize (numInsCos);
    EachCarriersPercentLiable.resize (numInsCos);

    for (vector<int>::size_type i = 0; i < numInsCos; i++){

    	cout << "What year did " << InsCos[i+0] << " begin insuring the property?" << endl;
        cin >> InsCosYearBegin[i];
        while (InsCosYearBegin[i] < a){
            cout << "Invalid date.  Please enter a date on or after " << b <<"/" << a << endl;
            cin >> InsCosYearBegin[i];
        };
        cout << "What month of " << InsCosYearBegin[i] << " did "<< InsCos[i+0] << " begin insuring the property?" << endl;
		cin >> InsCosMonthBegin[i];
        while (InsCosMonthBegin[i] < 1){
            cout << "Invalid date.  Please enter a valid month in numerical format." << endl;
            cout << " (i.e., 2 for February)" << endl;
            cin >> InsCosMonthBegin[i];
        };
        cout << "What year did " << InsCos[i+0] << " stop insuring the property?" << endl;
        cin >> InsCosYearEnd[i];
        cout << "What month of " << InsCosYearEnd[i] << " did " << InsCos[i+0] << " stop insuring the property?" << endl;
        cin >> InsCosMonthEnd[i];
    };


   	for (vector<string>::size_type i = 0; i < numInsCos; i++){
    	cout << InsCos[i+0] << " insured the property from " << InsCosMonthBegin[i+0] << " of " << InsCosYearBegin[i+0] << " to " << InsCosMonthEnd [i+0] << " of " << InsCosYearEnd[i+0] << endl;
    }

    int insTimeMonths = 0;
    int insTotalTimeMonths = 0;

    for (vector<string>::size_type i = 0; i < numInsCos; i++){
        insTimeMonths = (InsCosYearEnd[i+0] * 12 + InsCosMonthEnd[i+0]) - (InsCosYearBegin[i+0] * 12 + InsCosMonthBegin[i+0]);
        EachCarriersMonths[i+0] = (InsCosYearEnd[i+0] * 12 + InsCosMonthEnd[i+0]) - (InsCosYearBegin[i+0] * 12 + InsCosMonthBegin[i+0]);
    	cout << InsCos[i+0] << " insured the property for a total of " << insTimeMonths << " months." << endl;
    	insTotalTimeMonths = insTimeMonths + insTotalTimeMonths;
    }


    cout << "Total time of insurnace in months is: " << insTotalTimeMonths << endl;

    if (insTotalTimeMonths != LeakObject.calculateTimeOfLeak()){
        cout << "Warning! Total time of insurance is different from trigger period." << endl;
    }



    for (vector<string>::size_type i = 0; i < numInsCos; i++){
        cout << "Enter " << InsCos[i+0] <<"'s annual policy limits for " << InsCosYearBegin[i+0] << " through " << InsCosYearEnd[i+0] << "." << endl;
        cin >> AnnualPolicyLimits[i+0];
        PolicyLimitsPerMonth[i+0] = AnnualPolicyLimits[i+0] / 12;
        cout << InsCos[i+0] << "'s monthly policy limits were $" << PolicyLimitsPerMonth[i+0] << " per month." << endl;
    }

    for (vector<string>::size_type i = 0; i < numInsCos; i++){
        cout << InsCos[i+0] << "'s annual policy limits for " << InsCosYearBegin[i+0] << " through " << InsCosYearEnd[i+0] << " were $" << AnnualPolicyLimits[i+0] << " per year." << endl;
    }

    for (vector<string>::size_type i = 0; i < numInsCos; i++){
        TotalMonthlyLimitsPerCarrier[i+0] = PolicyLimitsPerMonth[i+0] * EachCarriersMonths[i+0];
        cout << InsCos[i+0] << "'s total insurance is: $" << TotalMonthlyLimitsPerCarrier[i+0] << endl;
    }

    int TotalInsuranceAllCarriers = 0;

    for (vector<string>::size_type i = 0; i < numInsCos; i++){
         TotalInsuranceAllCarriers += TotalMonthlyLimitsPerCarrier[i+0];

    }

    cout << "Total insurance from all carriers is: $" << TotalInsuranceAllCarriers << "." << endl;


    for (vector<string>::size_type i = 0; i < numInsCos; i++){
        EachCarriersPercentLiable[i+0] = ((float) TotalMonthlyLimitsPerCarrier[i+0] / TotalInsuranceAllCarriers);
        cout << InsCos[i+0] << "'s percentage of liablity is " << EachCarriersPercentLiable[i+0] * 100 << "%." << endl;

    }

    cout << "Enter the total remediation expenses: " << endl;
    int TotalRemediationExpenses = 0;
    cin >> TotalRemediationExpenses;

    for (vector<string>::size_type i = 0; i < numInsCos; i++){
             cout << InsCos[i+0] << "'s alliquot share of remediation expenses is: $" << float (EachCarriersPercentLiable[i+0] * TotalRemediationExpenses) << endl;
    }
return 0; 

    }
Pages: 12