Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


New to C++, 1st program.
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
shadowclaw3
Expert Cheater
Reputation: 0

Joined: 05 Aug 2007
Posts: 114

PostPosted: Wed Apr 01, 2009 5:26 pm    Post subject: New to C++, 1st program. Reply with quote

Hey guys i been learning C++ for about 2 or 3 days and i just wanted to actually ask for help, and show my 1st program.

Im learning from the book : "C++ for dummies 5th edition", and i need some type of assurance for what some things are. So if you think you can/ want to help post here with msn, aim, or just tell me to pm here on the website.

Well heres my program, its used to find the area of a non right triangle using herons formula. Explains most of the parts, and i think i could of shortened it alot but, im new so this is what i did.



Code:
// 
// Program to calculate area in a non right angle
// triangle
// a,b,c are sides, s = perimeter divided by 2,
// perimeter = a+b+c lol...
// Forumula is  sqrt(s * (s-a) * (s-b) * (s-c))
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;

int main()
{
   
    // Enter side a
    float a;
    cout << "Enter Length of side a:";
    cin >> a;
   
    // Enter side b
    float b;
    cout << "Enter Length of side b:";
    cin >> b;
   
    // Enter side c
    float c;
    cout << "Enter Length of side c:";
    cin >> c;
   
    //Calculate d (a+b)
    float d;
    d = a + b;
   
    float e;
    e = d + c;
   
    float s;
    s = e / 2;

    // Output the value of s
    cout << "Value of s is:";
    cout << s << endl;
   
    // Now for the area
    float f;
    f = s - a;
   
    float g;
    g = s - b;
   
    float h;
    h = s - c;
   
    float i;
    i = s * f * g * h;
   
  double param, area;

  param = i;

  area = sqrt (param);
   
   
    cout << "Area of the Triangle is:";
    cout << area << endl;
   
    system("PAUSE");
  return 0;
   
}

_________________
<(~.~<) BOO!
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Wed Apr 01, 2009 6:34 pm    Post subject: Reply with quote

shorter... and a lot less confusing.

Code:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   //enter side lengths
   float a, b, c;
   cout << "Enter Length of side a, b and c:" << endl;
   cin >> a;
   cin >> b;
   cin >> c;

   //s = 1/2(a + b + c)
   float s;
   s = (a + b + c) / 2;

   //output s ... herons: s = sqrt(s * (s - a)(s - b)(s - c))
   s = sqrt(s * (s - a)*(s - b)*(s - c));
   cout << "Value of s is: " << s << endl;

   cin.sync();
   cin.ignore();
   return 0;
}
Back to top
View user's profile Send private message
shadowclaw3
Expert Cheater
Reputation: 0

Joined: 05 Aug 2007
Posts: 114

PostPosted: Wed Apr 01, 2009 7:02 pm    Post subject: Reply with quote

i did try that sqrt() thing but it kept telling me an error, forgot what exactly but to declare it? So i googled how to sqrt and found that, so i cheated a bit.

And wow that alot shorter.

Also if you have time, i have a few questions. add my msn: [email protected]

_________________
<(~.~<) BOO!
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Wed Apr 01, 2009 7:07 pm    Post subject: Reply with quote

I'm guessing you tried it as...

Code:
s = sqrt(s * (s - a)(s - b)(s - c));
and left out the *'s
Back to top
View user's profile Send private message
shadowclaw3
Expert Cheater
Reputation: 0

Joined: 05 Aug 2007
Posts: 114

PostPosted: Wed Apr 01, 2009 7:12 pm    Post subject: Reply with quote

Code:
1:   // Listing 5.1 - demonstrates the use of function prototypes
2:
3:   typedef unsigned short USHORT;
4:   #include <iostream.h>
5:   USHORT FindArea(USHORT length, USHORT width); //function prototype
6:
7:   int main()
8:   {
9:     USHORT lengthOfYard;
10:    USHORT widthOfYard;
11:    USHORT areaOfYard;
12:
13:    cout << "\nHow wide is your yard? ";
14:    cin >> widthOfYard;
15:    cout << "\nHow long is your yard? ";
16:    cin >> lengthOfYard;
17:
18:    areaOfYard= FindArea(lengthOfYard,widthOfYard);
19:
20:    cout << "\nYour yard is ";
21:    cout << areaOfYard;
22:    cout << " square feet\n\n";
23:             return 0;
24:  }
25:
26:  USHORT FindArea(USHORT l, USHORT w)
27:  {
28:       return l * w;
29: }


Im learning from a site and they give this as an example, i dont get line 26. How does it just says USHORT l, USHORT w, if its actually lengthOfYard, and widthOfYard. So on line 28 it only says l * w.

and the return part... i dont get return, i know return 0 ends program, but the return l * w whats it do?

If you didnt already notice im a begginner and im trying learn, no prior exp of programming, so if u know a web site that explain C++ in a really begginner way, it would be great.

Might be a stupid question but i dont get it.

_________________
<(~.~<) BOO!
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Wed Apr 01, 2009 9:14 pm    Post subject: Reply with quote

Line 5 is the function prototype.

26 is the start of the function. It returns a USHORT (which as you can see at the beginning, is a typedef for unsigned short). It takes 2 arguments, l and w. It returns l * w. areaOfYard is getting assigned what FindArea returns (which is l * w)


cplusplus.com
Back to top
View user's profile Send private message
shadowclaw3
Expert Cheater
Reputation: 0

Joined: 05 Aug 2007
Posts: 114

PostPosted: Thu Apr 02, 2009 7:36 pm    Post subject: Reply with quote

Finished my next lesson, so i made another program to divide 2 numbers. I just learned Functions and how to break it out of the main and call to it to begin it.


Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef float USI;

USI Divide(USI First, USI Second);

int main()
{
    USI First;
    USI Second;
    USI Answer;
   
    cout <<"Please enter 2 #'s to be divided.\n"
         <<"Enter number to be divided:\t";
    cin  >> First;
    cout <<"Enter number used to divide:\t";
    cin  >> Second;
    if ( Second == 0 )
    {
         cout <<"Error second number Can't=\t0 \n";
         system("Pause");
         return 0;
         }
    Answer = Divide(First, Second);
    cout <<First<<" Divided by "<<Second<<" is =\t\t"<<Answer<<endl;
   
    system("Pause");
    return 0;
}

USI Divide(USI Top,USI Bottom)
{
    return(Top / Bottom);
}


Again might be much larger than some of you can but im trying.. =D

Also learned inline function but i chose to do it like this.

_________________
<(~.~<) BOO!
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Apr 02, 2009 9:01 pm    Post subject: Reply with quote

Your compiler will probably inline that automatically if you have it enabled...

What's with the typedef for float
Back to top
View user's profile Send private message
shadowclaw3
Expert Cheater
Reputation: 0

Joined: 05 Aug 2007
Posts: 114

PostPosted: Mon Apr 06, 2009 4:18 pm    Post subject: Reply with quote

sorry for late response, got stuck in the password reset lol, didnt even know i needed to request it for like 2 days.

But yea the typedef for float was at first unsigned short int but then in the end cause decimal i needed to change it to float, and so just changed it up their easily. Also i now know that short and int is same thing. Correct me if im wrong.

Also i am done with the video C++ tutorial,
http://www.mediafire.com/?0te1lgrx23g < Video Tuts

if anyone wants to recommend me another tutorial i would like to go through another one, to kinda get a better hang of it, also someone wanna suggest a program i should make with C++ something like: A program which averages numbers user enters, weekly payroll average or something, like that that would require me to use what i learned in C++, id like to test myself.

And the tutorial i might go with now is C++ for Dummies edition 5, if anyone thinks theres a better one online, please let me know, thanks for the help slovach. Looking forward to more help/tips =.

_________________
<(~.~<) BOO!
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Apr 06, 2009 5:11 pm    Post subject: Reply with quote

get a gook book.

C++ Primer Plus comes to mind
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Mon Apr 06, 2009 5:18 pm    Post subject: Reply with quote

shadowclaw3 wrote:
Also i now know that short and int is same thing. Correct me if im wrong.


Depends on the compiler.


shadowclaw3 wrote:
if anyone wants to recommend me another tutorial i would like to go through another one, to kinda get a better hang of it,


As already stated, http://www.cplusplus.com/doc/tutorial/.

shadowclaw3 wrote:
also someone wanna suggest a program i should make with C++ something like: A program which averages numbers user enters, weekly payroll average or something, like that that would require me to use what i learned in C++, id like to test myself.


Since you are learning C++, and not C, you should make the following after completing the aforementioned tutorial.

The program will will be for calculating the area of various shapes. The input will be the dimensions of the shape. They should all be derived from an abstract base class named Polygon. The shapes you will need to account for are: Circle, Triangle, and Square. They should overload the << operator (a friend to ostream, not one in itself), to do it's own formatting if requested. Finally, all classes should be templated, so the user can specify the data type they want to use.

The following test program should function off of your classes:

Code:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
   Triangle<double> tri(10, 20);
   cout << "The area is: " << tri.Area() << endl;

   Square<float> squ;
   squ.Dimensions(20, 30);
   cout << squ << endl;

   Polygon<int> *poly = new Circle<int>;
   poly->Dimensions(10);
   cout << "The area is: " << poly->Area() << endl;

   cin.sync();
   cin.ignore();

   return 0;
}


Should output:

Code:
The area is: 100
The area of the polygon is: 600
The area is: 314


There. That covers templates, virtual functions, inheritance, polymorphism, and operator overloading all in one assignment. Smile

After you do yours, I'll post the code to show how I would do it.
Back to top
View user's profile Send private message
shadowclaw3
Expert Cheater
Reputation: 0

Joined: 05 Aug 2007
Posts: 114

PostPosted: Mon Apr 06, 2009 6:23 pm    Post subject: Reply with quote

um yea the tutorial didnt explain what the using *** std; is but i have to use it with dev c++, its like using namespace std; thats only one i put on.

And yea my tutorial didnt explain alot of the stuff you asked so might be a while till i post my script.

also thats not a full script right? cause the function you use tri.Area is undeclared if it is even a function, can a function name have a . in it?

Also, when you type Triangle<double> is just like typing double Triangle right?

" That covers templates, virtual functions, inheritance, polymorphism, and operator overloading all in one assignment."

my guide missed all of that except overloading functions, may not be same as operator overloading...

im guessing still lots to learn.

_________________
<(~.~<) BOO!
Back to top
View user's profile Send private message MSN Messenger
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Mon Apr 06, 2009 7:44 pm    Post subject: Reply with quote

shadowclaw3 wrote:
also thats not a full script right? cause the function you use tri.Area is undeclared if it is even a function, can a function name have a . in it?


Those are hints to you to include those functions in your classes. You are supposed to define them; the source I posted is for testing what you should make according to the instructions.

shadowclaw3 wrote:
Also, when you type Triangle<double> is just like typing double Triangle right?


No, that is a templated class.

shadowclaw3 wrote:
" That covers templates, virtual functions, inheritance, polymorphism, and operator overloading all in one assignment."

my guide missed all of that except overloading functions, may not be same as operator overloading...

im guessing still lots to learn.


Do the tutorial I posted, it will explain everything.
Back to top
View user's profile Send private message
shadowclaw3
Expert Cheater
Reputation: 0

Joined: 05 Aug 2007
Posts: 114

PostPosted: Tue Apr 07, 2009 6:25 pm    Post subject: Reply with quote

i added you, flyte to msn but your never on =/ would of been easier to explain this question.


Code:
#define WIDTH 5
#define HEIGHT 3

int jimmy [HEIGHT][WIDTH];
int n,m;

int main ()
{
  for (n=0;n<HEIGHT;n++)
    for (m=0;m<WIDTH;m++)
    {
      jimmy[n][m]=(n+1)*(m+1);
    }
  return 0;
}


this code you see where it says for (m=0;m<WIDTH;m++)
i dont get the end, does it change the value of m to 1 FIRST then do the block? or does it do it with 0 first then 1? If it doest it with 1 first, may be a bit of a problem in the cplusplus guide.

_________________
<(~.~<) BOO!
Back to top
View user's profile Send private message MSN Messenger
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Tue Apr 07, 2009 6:33 pm    Post subject: Reply with quote

shadowclaw3 wrote:
i added you, flyte to msn but your never on =/ would of been easier to explain this question.


I never go on it anymore. I'll probably end up fusing my personal email with it so I actually have a reason to use it.

Anyways, all for loops start at whatever value you initialize it to (in this case zero).

You can look at a for loop this way:
Code:
for( beginning_expression; conditional_expression; iterative_expression)


The iterative_expression is done at the end each time you go through the loop. So it would do the loop with 0, then increment it, then do the loop with 1, etc.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites