the tree Posted November 1, 2005 Posted November 1, 2005 A couple of years back I made an attempt at learning C++. I'm now trying to take it back up, here is an attempt: #include <stdio.h> int main() { int xo,xt,yo,yt; float grad; printf("Enter the first co-ordinate:\n"); scanf("%d",&xo); scanf("%d",&yo); printf("And the second one:\n"); scanf("%d",&xt); scanf("%d",&yt); grad = (yo-yt)/(xo-xt); printf("Chord of (%d",xo);printf(",%d",yo);printf(")"); printf(" and (%d",xt);printf(",%d",yt);printf(")\n"); printf("Gradient: %f",grad); scanf("%f",grad); return 0; } The issue I found was that it wouldn't work when I tried printf("string",variable,"string"); so I ended up having to do lots and lots of printf()s. Now I seem to remember another method of output that went something like cout >>> "string" >>> variable; But I can't remember how to do that.
RyanJ Posted November 1, 2005 Posted November 1, 2005 I believe what you want is this: #include <stdio.h> int main() { int xo,xt,yo,yt; float grad; printf("Enter the first co-ordinate:\n"); scanf("%d",&xo); scanf("%d",&yo); printf("And the second one:\n"); scanf("%d",&xt); scanf("%d",&yt); grad = (yo-yt)/(xo-xt); printf("Chord of (%d,%d) and (%d,%d)\nGradient: %f", xo, yo, xt, yt, grad); scanf("%f",grad); return 0; } Or the cout way #include <stdio.h> #include <iostream.h> int main() { int xo,xt,yo,yt; float grad; printf("Enter the first co-ordinate:\n"); scanf("%d",&xo); scanf("%d",&yo); printf("And the second one:\n"); scanf("%d",&xt); scanf("%d",&yt); grad = (yo-yt)/(xo-xt); cout << "Chord of (" << xo << "," << yo ") and (" << xt << "," << yt << ")\nGradient: " << grad; scanf("%f",grad); return 0; } Incase you are interested I have a few reference sites you may like too http://www.cplusplus.com/doc/tutorial/ http://www.intap.net/~drw/cpp/ http://www.cprogramming.com/tutorial.html And good luck learning C++! Cheers, Ryan Jones
the tree Posted November 1, 2005 Author Posted November 1, 2005 Thanks, I guess I just hadn't got how printf() was meant to work. What is the best way to end a program so it doesn't close until the user does something? My current method seems to cause crashage.
RyanJ Posted November 1, 2005 Posted November 1, 2005 Thanks, I guess I just hadn't got how printf() was meant to work. Actually it can be quite a confusing function All you need to remember is that the order in which you add the replace tags is the order in which you add the stuff to replace them Again good lcuk with learning C++ its a good language Cheers, Ryan Jones
Dave Posted November 1, 2005 Posted November 1, 2005 I don't see why you're using scanf and printf when you have cin and cout? For example, you can clean pretty much everything up and dispense with stdio.h: #include <iostream> using namespace std; // you need this so that you don't have to do std::cout int main() { float xo,xt,yo,yt; float grad; cout << "Enter the first co-ordinate:" << endl; cin >> xo >> yo; cout << "And the second one:" << endl; cin >> xt >> yt; grad = (yo-yt)/(xo-xt); cout << "Chord of (" << xo << "," << yo << ") and (" << xt << "," << yt << ")" << endl << "Gradient: " << grad << endl; return 0; } Note that the current standard says you should drop the '.h' at the end of standard include files. For instance, iostream.h -> iostream, string.h -> string etc. I've changed your co-ordinates to be floats instead of ints, otherwise you won't get a proper value for the gradient. Also, you need the "using namespace std" since all of the standard streams like cout, cin are all kept in std (read up on namespaces). If you don't include that, you'll get undefined variable errors. Hope this helps.
RyanJ Posted November 1, 2005 Posted November 1, 2005 Thanks for pointing that out dave I'm no expert at C++ (I know enough to get by and thats about it ). I do know about the cin command but I thought I had better leave it as the tree had started Again thanks for pointing that out... Cheers, Ryan Jones
the tree Posted November 2, 2005 Author Posted November 2, 2005 I don't see why you're using scanf and printf when you have cin and cout?Really just because they were in the tutorial that came with this complier-thingumy.
LOGIC Posted November 5, 2005 Posted November 5, 2005 What is the best way to end a program so it doesn't close until the user does something? My current method seems to cause crashage. I think the best way is to put the following before return statement system("PAUSE");
the tree Posted November 5, 2005 Author Posted November 5, 2005 Thanks but it says that function is undeclared.
RyanJ Posted November 5, 2005 Posted November 5, 2005 Thanks but it says that function is undeclared. Did you include the correct library and define the function? Have a look here Cheers, Ryan Jones
LOGIC Posted November 5, 2005 Posted November 5, 2005 sorry I forgate to tell you about:-p #include <stdlib.h>
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now