Jump to content

Recommended Posts

Posted

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.

Posted

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

Posted

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.

Posted
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

Posted

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.

Posted

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

 

Cheers,

 

Ryan Jones

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

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");

Posted
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.