A calculator issue

Hey guys,

I'm new to all of this, just starting out and I have a question. I'm finally attempting to get my grasp on if else loops and the such and have written a program for one of my classes on getting 3 sides of a triangle and deciding on if it is a right triangle and which side is the hypotenuse. I've got it down pretty good, just if someone inputs a letter instead of a number, my prog just keeps repeating with no break and the only way for me to get out is to kill the console window. I'd appreciate some direction on learning how to get the program itself to ignore letters and throw out the same message I'm using if it takes in a negative number.

Thanks in Advance!

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

int main()

{	
		int setprecision(2);
		double side1 = 0;
		double side2 = 0;
		double side3 = 0;
			
	do{
			cout << " Enter side 1 of the unknown triangle: ";
			cin >> side1;
			cout << " Enter side 2 of the unknown triangle: ";
			cin >> side2;
			cout << " Enter side 3 of the unknown triangle: ";
			cin >> side3;
			if (side1 <= 0 || side2 <= 0 || side3 <= 0)
			{	cout << " Invalid Entry! Use positive numbers only! No characters or negative numbers! " << endl;
			}
			else
			cout << "You entered side 1 as: " << side1 << ",side 2 as: " << side2 << " and side 3 as: " <<side3 << endl;
			cout << " " << endl;
			cout << " " << endl;
	}while (side1 <= 0 || side2 <= 0 || side3 <=0);

	
if (pow (side3,2.0) == ((pow (side2,2.0) + (pow (side1,2.0)))))
cout << " Side 3 is the hypotenuse and this IS a right triangle! " << endl;
else 
		if (pow (side2,2.0) == ((pow (side3,2.0) + (pow (side1,2.0)))))
		cout << " Side 2 is the hypotenuse and This IS a right triangle! " << endl;
		else
			if (pow (side1,2.0) == ((pow (side2,2.0) + (pow (side3,2.0)))))
			cout << " Side 1 is the hypotenuse and this IS a right triangle! " << endl;
			else 
			cout << " This is NOT a right triangle! " << endl;
		

	cout << " " << endl;
	cout << " " << endl;
	cout << "Press any key to Exit." << endl;
	cin.ignore(2);

	
	
	return 0;

}


closed account (Dy7SLyTq)
if else loops

if/else are constructs that control data flow. while, do-while, for, and for(:) are loops. switch/goto is kind of a combination of the two

for checking input, i would make a sanitize function using <stringstream> and <string>
DTS,

Am I completely off base with my programming then?

I haven't learned anything about strings yet, although I am going to read about them now.

Thanks for the input!
closed account (Dy7SLyTq)
Am I completely off base with my programming then?

not at all. just some vocab issues. your asking questions that i never thought of at this level.
Dts,

I think I might be getting ahead of myself too, I'm not quite sure where I should quit with this one. The program functions just fine as long as you're entering numbers. As soon as I enter in a letter for one of the sides, it just blasts off into a running loop of asking for the sides again and again, and again, and again... Ha!

So I'm not sure if I should just call it quits and submit it, or to figure out how to kill off accepting letters as a whole...

Thanks for the input again!
closed account (Dy7SLyTq)
i wouldnt quit it. its a good excuse to learn something new. i would use bajarne stroustrops calculator for inspiration
Do this:
1
2
3
4
5
6
7
8
9
10
while (true)
    {
        cout << "Enter Integer: ";
        if (cin >> var1)
        {
            break;
        }
        cin.clear() ;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

replace var1 with your variables. Do this for each entered variable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int setprecision(2);
		double side1 = 0;
		double side2 = 0;
		double side3 = 0;
			
	do{
			
	while (true)
        {
        cout << " Enter side 1 of the unknown triangle: ";
        if (cin >> var1)
        {
            break;
        }
        else
        {
        cin.clear() ;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        //put error message here so
        if (side1 <= 0 || side2 <= 0 || side3 <= 0)
			{	cout << " Invalid Entry! Use positive numbers onlt. No characters or negative numbers! " << endl;
			}
        }
	...
If it's for a class, and your teacher/professor didn't tell you to check your input for invalid characters, then I wouldn't worry too much about it...or at least don't stress out over it when doing something for homework.

If you really want to, though, this is one way of doing it:
1
2
3
4
5
6
7
8
9
cout << "Enter a number: ";
int num;
while (!(cin >> num))
{
    cout << "Hey! I want an integer, please: ";
    cin.clear(); // Clear error flags
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clear out the junk in the input stream
}
// Do stuff with 'num' 

You'll need to #include <limits> for this method.

Another method uses stringstreams, like DTSCode mentioned.
This works if you want to keep it compact:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    cout << "Enter a number: ";
    int num;
    string input;
    while (getline(cin, input), !(istringstream(input) >> num))
        cout << "I said enter a *number*: ";
    cout << "You entered: " << num;
}
Enter a number: No.
I said enter a *number*: I don't want to.
I said enter a *number*: I absolutely refuse!
I said enter a *number*: Okay, fine...
I said enter a *number*: 42
You entered: 42

(bwahaha, I used the comma operator)

Anyways, if you search around this site, you'll find a lot of information about error checking for inputs and such.
I did some searching as recommended and I'm currently tweaking. I really appreciate all of the help and good information you guys tossed out for a noob like myself.

First time posting to this forum, everyone was very helpful and positive. I have to say that is the first time that has EVER happened. HA!

I will post my final version shortly. I'm also currently working on another project too, which is kicking my butt. Maybe another thread coming soon.

But anyways, thanks again!

Another method uses stringstreams, like DTSCode mentioned.
This works if you want to keep it compact:




1234567891011121314
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
cout << "Enter a number: ";
int num;
string input;
while (getline(cin, input), !(istringstream(input) >> num))
cout << "I said enter a *number*: ";
cout << "You entered: " << num;
}
Enter a number: No.
I said enter a *number*: I don't want to.
I said enter a *number*: I absolutely refuse!
I said enter a *number*: Okay, fine...
I said enter a *number*: 42
You entered: 42



Is there a way for me to flip this and only accept letters?!

That would put the icing on the cake...

Thanks in advance!
closed account (Dy7SLyTq)
yeah. the only thing you would need to do is replace int num; with char num;. although i would change the name to something that makes more sense
Sweet,

I've got another question regarding formatting...

1
2
3
4
5
6
7
8
cout <<empname << endl;
			cout << fixed << setprecision(2) << "Gross Amount:" << setfill('.') << setw(35) << "$" << gpay << endl;
			cout << fixed << setprecision(2) << "Federal Tax:" << setfill('.') << setw(37) << "$" << fedtaxw <<endl;
			cout << fixed << setprecision(2) << "State Tax:" << setfill('.') << setw(40) << "$" << stataxw  <<endl;
			cout << fixed << setprecision(2) << "Social Security / Medicare:" << setfill('.') << setw(22) << "$" << sockw << endl;
			cout << "Health Insurance:" << setfill('.') << setw(33) << "$" << hiw << endl;
			cout << fixed << setprecision(2) << "Net Pay:" << setfill('.') << setw(40) << "$" << netpay << endl


Is there a better way to do this, like set a left column and a right column, then fill the difference? Because if the dollar amount changes in the right column, then it gets all wonky looking...

Thanks guys!
closed account (Dy7SLyTq)
uhhh yeah... dont quote me on this because i havent used iomanip in forever, but i think there is a right and left ie cout<< left; cout<< right;
Here is the code for the whole program so far...

Here are my issues so far.

I need to account for user input error... ie, no numbers in the name field, no letters in the number field...
Also the whole output format as well.

I know what you're talking about with the left and right I've read it, I just can't execute it with a fill.

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
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

using namespace std;
	
	

int main()

{
	setprecision(5);
	const double ftax(0.15);
	const double stax(0.035);
	const double ssec(0.085);
	const double ins(75.00);


	double gpay, netpay, fedtaxw, stataxw, sockw, hiw;	
	string empname = ""; 
	
		cout << "Please enter the Employee's Name:\n";
			getline(cin,empname);
		cout << "Enter Employee's Gross Pay:\n" << endl;
			cin >> gpay;
		cout << "You entered " << empname << " as the employee name and $" << fixed << setprecision(2) << gpay << " as gross pay." << endl;
		
			cout << " " << endl;
			cout << " " << endl;
			
			
			fedtaxw = (gpay * ftax);
			stataxw = (gpay * stax);
			sockw = (gpay * ssec);
			hiw = ins;
			netpay = gpay - (fedtaxw + stataxw + sockw + hiw); 
		
			system("pause");
			
			cout << "\n ";
			cout << "\n ";
			cout <<empname << endl;
			cout << fixed << setprecision(2) << "Gross Amount:" << setfill('.') << setw(35) << "$" << gpay << endl;
			cout << fixed << setprecision(2) << "Federal Tax:" << setfill('.') << setw(37) << "$" << fedtaxw <<endl;
			cout << fixed << setprecision(2) << "State Tax:" << setfill('.') << setw(40) << "$" << stataxw  <<endl;
			cout << fixed << setprecision(2) << "Social Security / Medicare:" << setfill('.') << setw(22) << "$" << sockw << endl;
			cout << "Health Insurance:" << setfill('.') << setw(33) << "$" << hiw << endl;
			cout << fixed << setprecision(2) << "Net Pay:" << setfill('.') << setw(40) << "$" << netpay << endl;
			
	
		
		system("pause");
		return 0;
	}		
Topic archived. No new replies allowed.