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,
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)
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.
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:
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.
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.
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()
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.
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.
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/
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.
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.
#include <iostream>
#include <vector>
usingnamespace 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;