http://calcg.org/newlogo2.png Not Logged in.
Login | Register

General Discussion Board \ Computer Programming \ C++ cout problems

Click here to log in (you must be logged in to post comments).

AuthorComment
gulyman
Goliath
avatar
Posted: 2 Apr 2008
13:23 GMT
Total Posts: 144
When you get to the last cout command the cursor flashes and it skips it. I don't have any idea what would cause a cout not to work. For a scary look into how code works in my mind, refer to below.

#include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double length, angle, xtotal = 0, ytotal = 0, radians, x, y, lengtht, anglet;
double totalangle, xtp, ytp;

cout << endl << "Component vector problem solver." << endl
<< "To finish inputing vectors, input 0 for length."
<< endl << endl << "Length: " << flush;
cin >> length;

while(length != 0)
{
cout << "Angle: " << flush;
cin >> angle;
radians = ((angle / 360) * (3.1415926535897932384626433832 * 2)); //converts angles to radians so the trig functions work
x = length * cos(radians);
xtotal = xtotal + x;
y = length * sin(radians);
ytotal = ytotal + y;
cout << endl << endl << "Length: " << flush;
cin >> length;
}
lengtht = sqrt((ytotal * ytotal) + (xtotal * xtotal)); //Use pythagorean theorem to find the length of the resultant vector

if(xtotal < 0) //gives the absolute value of the resultany x and y value
xtp = xtotal * -1;
else
xtp = xtotal;

if(ytotal < 0)
ytp = ytotal * -1;
else
ytp = ytotal;

anglet = atan(xtp / ytp); //calculates resultant angle from the x axis

totalangle = ((anglet / (3.1415926535897932384626433832 * 2)) * 360); //changes angle into degrees

if(xtotal < 0 && ytotal > 0) //changes the angle into cartesian/360 degrees
totalangle = 180 - totalangle;

if(xtotal < 0 && ytotal < 0)
totalangle = totalangle + 180;

if(xtotal > 0 && ytotal < 0)
totalangle = 360 - totalangle;



cout << endl << endl << "Resultant Length: " << lengtht // outputs the final results. The total angle won't output.
<< endl << "Resultant Angle: " << fixed << showpoint << setprecision(9)
<< totalangle;
return 0;
}
zarg
Dragoon
avatar
Posted: 2 Apr 2008
14:20 GMT
Total Posts: 67
I don't think that this will work but perhaps?:
try changing the line:
"anglet = atan(xtp / ytp);"
to
"anglet = atan2 (xtp,ytp)"

I don't really know what your doing but I hope this helps.
I don't think it will work though.

I found this on:
http://www.cplusplus.com/reference/clibrary/cmath/atan2.html

Also could you explain what you use atan for?

---
In most countries selling harmful things like drugs is punishable. Then howcome people can sell Microsoft software and go unpunished? (By hasku@rost.abo.fi, Hasse Skrifvars http://www.linuxweb.com/lw_quotes.html)
Xphoenix
Ultralisk
avatar
Posted: 2 Apr 2008
14:22 GMT
Total Posts: 210
Disclaimer: I'm still learning C++, so try to lay off me a little if any of my comments make no sense.

First, how'd you get it to compile? fixed and showpoint are undeclared.

Second, can you put cout on multiple lines like that (at the end), or is it just a result of your copy+pasting?

---
~Xphoenix
zarg
Dragoon
avatar
Posted: 2 Apr 2008
14:32 GMT
Total Posts: 67
I also know very little about C++. That would be why I sound so unconfident in my previous post.

---
In most countries selling harmful things like drugs is punishable. Then howcome people can sell Microsoft software and go unpunished? (By hasku@rost.abo.fi, Hasse Skrifvars http://www.linuxweb.com/lw_quotes.html)
BrandonW
Goliath
Posted: 2 Apr 2008
18:30 GMT
Total Posts: 100
Yes, of course you can use cout with multiple lines...a line doesn't end in C++ until you hit the semicolon.

It's been years since I touched this, but I suspect the issue is with fixed/showpoint/setprecision. Take all those out and see if it works, then add them back one at a time until you find the problem. I'd also look at documentation on each of those to see how they behave and how they'd be used properly.
gulyman
Goliath
avatar
Posted: 4 Apr 2008
11:25 GMT
Total Posts: 144
The purpose of this code is to have you input several vectors and it will output the resultant vetor. I just finished doing this stuff in grade 11 physics and I wanted to see if I could make a program to do it for me.
atan is inverse tan, or tan to the negative one.
the_second_post
I use it to find the angle of the resultant vector from the x-axis.
Showpoint and fixed are supposed to be declared in the #include <iomanip>. I will try taking them out and see if it works.
gulyman
Goliath
avatar
Posted: 4 Apr 2008
13:54 GMT
Total Posts: 144
It works now. I added a flush and changed some variables and stuff. Here is the finished code. If anyone wants to bug test it I would encourage them to. I can only do so much of that before my brain dies from bordom.

#include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double length, angle, xtotal = 0, ytotal = 0, radians, x, y, lengtht, anglet;
double totalangle, xtp, ytp;

cout << endl << "Component vector problem solver." << endl
<< "To finish inputing vectors, input 0 for length."
<< endl << endl << "Length: " << flush;
cin >> length;

while(length != 0)
{
cout << "Angle: " << flush;
cin >> angle;
radians = ((angle / 360) * (3.1415926535897932384626433832 * 2)); //Changes angles
x = length * cos(radians); //to radians for
xtotal = xtotal + x; //trig functions
y = length * sin(radians);
ytotal = ytotal + y;
cout << endl << endl << "Length: " << flush;
cin >> length;
}
lengtht = sqrt((ytotal * ytotal) + (xtotal * xtotal));
//finds the resultant length
if(xtotal < 0) //assignes the absolute value of x and y for finding the angle
xtp = xtotal * -1;
else
xtp = xtotal;

if(ytotal < 0)
ytp = ytotal * -1;
else
ytp = ytotal;

anglet = atan(ytp / xtp); //finds the absolute angle from the x axis

totalangle = ((anglet / (3.1415926535897932384626433832 * 2)) * 360);
//Changes radians to degrees
if(xtotal < 0 && ytotal > 0) //changes the angle from navigator to carteasen
totalangle = 180 - totalangle;

if(xtotal < 0 && ytotal < 0)
totalangle = totalangle + 180;

if(xtotal > 0 && ytotal < 0)
totalangle = 360 - totalangle;

cout << endl << endl << "Resultant Length: " << lengtht
<< endl << "Resultant Angle: " << fixed << setprecision(9)
<< totalangle << flush;

return 0;
}


zarg
Dragoon
avatar
Posted: 26 Apr 2008
05:27 GMT
Total Posts: 67
On your program:
start
Component vector problem solver.
To finish inputing vectors, input 0 for length.

Length: 50
Angle: 0


Length: 50
Angle: 180


Length: 0


Resultant Length: 6.12303e-015
Resultant Angle: 90.000000000
end
I don't understand the resultant. Why wouldn't the resultant length be 0 and the resultant angle 0.

---
In most countries selling harmful things like drugs is punishable. Then howcome people can sell Microsoft software and go unpunished? (By hasku@rost.abo.fi, Hasse Skrifvars http://www.linuxweb.com/lw_quotes.html)
gulyman
Goliath
avatar
Posted: 12 May 2008
11:32 GMT
Total Posts: 144
I think it's because I didn't use an "exact" value of pi. :) There's probably a pi function that I don't know about.





Portal | My Account | Register | Lost Password or Username | TOS | Disclaimer | Help | Site Search | File Archives Copyright © 2002-2019 CalcG.org