SRL Archive - C/C++ Help and Tutorials

[C++]PythagorasV2!


Floor66

2009-11-19 08:59:19

I've been learning C++ and needed something to easen up my homework, so I wrote this! Filling in letters instead of numbers will crash it (on my todo list to fix that).
I now announce: PythagorasV2! No GUI, console does the job just fine.

What does it do? It finds you the unknown side in a triangle, using Pythagoras' theorem! It doesn't only apply "sqrt(a*a + b*b)" (for finding the hypotenuse), it also applies to finding the other sides of a triangle.

Download link (also includes the source): http://rapidshare.com/files/308773441/PythagorasV2.rar
Link 2: http://specs.thamob.nl/PythagorasV2.rar

If you have comments, ideas, ways to shorten certain pieces of code, etc. don't hesitate to reply!
Scan: http://virusscan.jotti.org/en/scanresult/3184bb73ddf594f1a7cc3fbd712bc9e2ff0163fa

EDIT:
Need some help; how would I do this properly?

[php]
string s[2];
double d[2];

int main()
{
d[0] = 3.0;
d[1] = 4.0;
s[0] = "First sentence: " << d[0];
s[1] = "Second sentence: " << d[1];
cout << s[0] << endl;
cout << s[1] << endl;
cout << endl;
system("PAUSE");
return 0;
}
[/php]

Because this will return:


1>c:\documents and settings\den hartog\mijn documenten\visual studio 2008\projects\tests\tests\main.cpp(13) : error C2296: '<<' : illegal, left operand has type 'const char [17]'
1>c:\documents and settings\den hartog\mijn documenten\visual studio 2008\projects\tests\tests\main.cpp(13) : error C2297: '<<' : illegal, right operand has type 'double'
1>c:\documents and settings\den hartog\mijn documenten\visual studio 2008\projects\tests\tests\main.cpp(14) : error C2296: '<<' : illegal, left operand has type 'const char [18]'
1>c:\documents and settings\den hartog\mijn documenten\visual studio 2008\projects\tests\tests\main.cpp(14) : error C2297: '<<' : illegal, right operand has type 'double'

KyleGut3

2009-11-19 14:10:10

Could you possibly upload to another site (MegaUpload, etc.)?

Rapidshare is giving me lovely errors about how I must be a premium member to download your file.

Now, since this is a console application- is it safe to assume that you're using the cin object to read data? (if so, I have a fix for you)

Floor66

2009-11-19 14:14:20

Yes, using cin. Will upload to my own web, sec.

E:
Added to main post.

KyleGut3

2009-11-19 14:21:02


bool cinFail()
{
bool result = false;
if(cin.fail())
{
cin.clear();
string c;
cin >> c;
result = true;
}
return result;
}

Note that you need to include the string object for this method… (I haven't tested it using a dynamic character array yet-)

Simply run an if(!cinFail()) statement just after using the cin object (and of course loop the cin object so that if it does fail- the user is prompted to enter a new integer.


EDIT: Basically, cinFail will result in true (meaning it has failed) whenever a user enters the wrong variable type.

i.e. if prompting char, they give int, if prompting int, they give a string, etc.

the
cin.clear();
string c;
cin >> c;
clears the cin object of any extra material, and allows for another run.

(If you used a regular char instead of string to clear it, it would have to run once for each character that was given)

Floor66

2009-11-19 14:33:07

Maybe a while(!cinFail()) and in there put the questions.
I'm getting more and more repeated code, I really want to shorten some parts up. I'll create arrays and stuff so I can make 1 loop for all 3 questions + I need to find a way to shorten the calculation printing.

KyleGut3

2009-11-19 14:37:01

Feel free to upload another run-through of your code after you've condensed it. I'd love to see what you get, and see if I can learn anything from it (like the system statements.. why the hell was I never taught that?)

Floor66

2009-11-19 14:39:08

These system(""); ones, I found by accident going through someone's code (are there alternatives to e.g: system("PAUSE");?). Anyway, condensing it in the weekend probably, (almost) no time to do it during the week.

E:
A quick peak of the new AB/BC/AC asking:
[php]
side[0] = "AB";
side[1] = "BC";
side[2] = "AC";

for(i = 0; i < 3; i++)
{
cout << "Enter side " << side << ": " << endl;
cin >> ans;
cout << endl;
if(cinFail())
{
cout << "ERROR: Invalid input." << endl;
return;
}
if(ans < 0)
{
cout << "ERROR: You can't have negative sides." << endl;
return;
}
}
[/php]

Letters won't crash it anymore :)

E2:
Need help @ main post.

KyleGut3

2009-11-20 17:18:57

@New HelpPost:
It took me a bit to find examples of it- but here:

[PHP]#include <iostream>
#include <string>
#include <sstream>
using namespace std;

string stringify(double);


int main()
{
string s[2];
double d[2];
d[0] = 3.0;
d[1] = 4.0;
s[0] = "First sentence: " + stringify(d[0]);
s[1] = "Second sentence: " + stringify(d[1]);
cout << s[0] << endl;
cout << s[1] << endl;
cout << endl;
system("PAUSE");
return 0;
}

string stringify(double x)
{
ostringstream o;
if (!(o << x))
cout << "Messed Up!";
return o.str();
}
[/PHP]

Modified the example a bit so you wouldn't need their classes, etc.

From their tutorial/FAQ:
From: http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.1
The std::ostringstream object o offers formatting facilities just like those for std::cout. You can use manipulators and format flags to control the formatting of the result, just as you can for other std::cout.

In this example, we insert x into o via the overloaded insertion operator, <<. This invokes the iostream formatting facilities to convert x into a std::string. The if test makes sure the conversion works correctly — it should always succeed for built-in/intrinsic types, but the if test is good style.

The expression o.str() returns the std::string that contains whatever has been inserted into stream o, in this case the string value of x.

Floor66

2009-11-21 04:18:37

Ah shit I always forget to include the right includes, that's why stringstreams never seemed to work :P