Hi guys I just started C++ and I bet my code pasted below will be screaming out NOOB from all directions... All i wanted to ask you guys is how do I make the program terminate when an invalid value is typed up, such as -1 or ctrl z. I worked long and hard on this and I am quite stumped...
#include <iostream>
usingnamespace std;
int main()
{
float dep1, dep2, dep3, total, rate;
rate=1.08;
int n = 1;
while(dep1)
while(dep2)
while(dep3)
{
cout << "Enter the amount deposited 1st July two years ago: $";
cin >> dep1;
cout << "Enter the amount deposited 1st July one year ago: $";
cin >> dep2;
cout << "Enter the amount deposited 1st July of this year: $";
cin >> dep3;
if (cin.eof() || cin.fail() || dep1 < 0 || dep2 < 0 || dep3 < 0)
{
cout << "Error found in data entered (close program)" << endl;
break;
}
if(dep1 <= 0 && dep2 <= 0 && dep3 <= 0)
break;
total=(dep1*rate+dep2)*rate+dep3;
cout << "The total amount accumulated on the 1st of July of the current year is: " << total << endl;
system ("PAUSE");
return 0;
}}
When I compile and run the program, it does work.. somewhat. It recognizes that if a negative or invalid value in entered for either dep1 dep2 or dep3. But it only does so when all the values are entered.. I want it so that, say if -1 was entered for dep1 the program recognizes that it is invalid and shuts down automatically. Thanks in advance.
I tried the exit function before, but I have no clue as to where I am supposed to place it. I dont think it works for my situation because the program shuts down without warning. There has to be some kind of message before it shuts down but being as inexperienced as I am, I have no clue how to use the exit function. :/.
I did what you said pelotron and it works in a way. The program does shut down which is sick! But it still is pretty abrupt, my teachers wont know what they did wrong whilst entering the values LOL! Are there other ways?