### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include 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: " <

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?

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:
 ``12345678910`` ``````while (true) { cout << "Enter Integer: "; if (cin >> var1) { break; } cin.clear() ; cin.ignore(numeric_limits::max(), '\n'); }``````

replace var1 with your variables. Do this for each entered variable.
 ``123456789101112131415161718192021222324`` ``````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::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:
 ``123456789`` ``````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::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 `stringstream`s, like DTSCode mentioned.
This works if you want to keep it compact:
 ``1234567891011121314`` ``````#include #include #include 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 #include #include 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...

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...

 ``12345678`` ``````cout <

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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include #include #include #include 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 <
Topic archived. No new replies allowed.