### Getting Input for a Simple Pythagorean Theorum Program

I just started learning C++ today. My only background was in TI83Basic (yeah, the graphing calculators)

I'm attempting to write a simple program to calculate the sides of a right triangle. I think my problems are stemming from a misuse of
`getline(cin, var)`

I realize the information is almost certainly already out there, but I'm having trouble making sense of it because I'm brand new. Could some kind person please point out my mistake? I've narrowed it down as far as I could, but I'll post the full code since I may have misidentified my mistake.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````#include #include #include using namespace std; int main() { //create variables double a; double b; double c; string sel; cout<<"This is a basic program to calculate the sides of a right triangle.\n" <<"I am attempting to find the length of: "; cout<<"the (l)ong side.\n"; cout<<"one of the (s)hort sides.\n"; getline (cin, sel); if (sel == "l") { getline (cin, a); getline (cin, b); a=a*a; b=b*b; c=a+b; c=sqrt(c); cout<<"Side C= "; cout<

I can include the error messages if it helps, but I don't want to flood the forum with text. If my problem really does stem from my use of "getline" it might be a simple explanation. I was told, by the tutorials on this site that it was better to use "getline" than "cin" because sometimes you need to get data with spaces in it.

Also, how am I doing for 1 day of practice. Do I seem to be grasping it okay?
getline() will only load a string with data, not a double. You need to load it as a string then convert it to a double.

Please include compiler error messages in future as it's very useful. Otherwise we have to manually read the code and try to process it in our heads =\
 I was told, by the tutorials on this site that it was better to use "getline" than "cin" because sometimes you need to get data with spaces in it.

It is better to use it where it's appropriate, like many other tools. getline only works with strings. It doesn't make much sense to try and stuff a 'line' into a variable of type double.

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include #include #include using namespace std; int main() { cout<<"This is a basic program to calculate the sides of a right triangle.\n" <<"I am attempting to find the length of: "; cout<<"the (l)ong side.\n"; cout<<"one of the (s)hort sides.\n"; char choice ; cin >> choice ; if ( choice == 'l' ) { double a, b ; if ( cin >> a >> b ) cout << "Side C = " << sqrt(a*a + b*b) << '\n' ; else cout << "Invalid input.\n" ; } else if ( choice == 's' ) { double b, c ; if ( cin >> b >> c ) cout << "Side A = " << sqrt(c*c - b*b) << '\n' ; else cout << "Invalid input.\n" ; } else cout<<"Not a valid option."; }``````

getline() stores a string, You can either convert this to a double OR since you don't need data with spaces at all here you should just use cin on it's own.

Lesson: don't over-complicate things, If you don't need to get a string of data with spaces, don't do it... Just get what you need!
It may be useful to look through this recent thread which is also on the Pythagorean theorem. http://www.cplusplus.com/forum/beginner/89964/

kkschmidt wrote:
Also, how am I doing for 1 day of practice. Do I seem to be grasping it okay?
Yes, you're doing just fine. Don't be afraid of making mistakes, that's the best way to learn. Or at any rate, you will learn by actually writing code, and that's what you are doing.
Thank you for the help. As a beginner, sometimes it's hard to make sense of the myriad of posts on a topic that may or may not be using concepts I haven't been introduced to. cire: that bit of code was really helpful. I pieced through it and gained some understanding. Everyone else was helpful as well. I rewrote it using functions for the different types of calculation. Probably not as optimal or as compact as cire's solution, but it works as well, with no compiler errors to report!

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````//pythagorean theorum app using functions #include #include #include using namespace std; //function for shortside double shortside (double c, double b) { double r; r = sqrt((c*c-b*b)); return (r); } //function for hypotenuse double hypo (double a, double b) { double r; r = sqrt((a*a+b*b)); return (r); } int main () { int menu; double a, b, c; cout << "This is a program to determine the length of the sides of a right triangle. Make a selection:\n" << "1 Find the Hypotenuse\n" << "2 Find the Short Side\n"; cin >> menu; //call function for hypotenuse calculation if (menu == 1) { cin >> a >> b; c = hypo (a,b); cout << c; } //call function for shortside calculation else if (menu == 2) { cin >> c >> b; a = shortside (c,b); cout << a; } //inform user of invalid selection else cout << "Not a valid option."; return 0; }``````

Once again, thank you for the help.
Topic archived. No new replies allowed.