This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-09-05 11:46:22

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

C++ Help

I'm really new to C++ and just getting started with classes (it would be helpful if someone could post a simple a class example below), and this was my first attempt of using them.  With school starting up again, I figured that I'd make it something that could be useful.  In this case a program that calculates the area of basic shapes (once I get classes working, I'll add more).  So here it is.

Code:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

const float pi = 3.14;

class ShapeMath
{
    protected:float x, y, r;

    public:
        void RectValues (float,float,string);
        float RectReturn ();
        void CircleValues (float,float,string);
        float CircleReturn ();
};

void ShapeMath::RectValues (float a, float b, string input)
{
    cout << "Width: ";
    cin >> input;
    stringstream(input) >> a;
    cout << "Height: ";
    cin >> input;
    stringstream(input) >> b;
    cout << "\n";

    x = a;
    y = b;
    return;
}

float ShapeMath::RectReturn ()
{
    return (x * y);
}

void ShapeMath::CircleValues (float a, float b, string input)
{
    cout << "Radius: ";
    cin >> input;
    stringstream(input) >> a;
    cout << "\n";

    r = a;
    return;
}
float ShapeMath::CircleReturn ()
{
    return (r * r * pi);
}

int main ()
{
    string input; // Variable used to get user input
    float answer; // Variable used to print results
    bool loop = true; // Variable used to continue the loop
    ShapeMath Rect, Circle; // Objects

    while (loop == true)
    {
        cout << "1) Rectangle\n2) Circle\n3) Quit\n\n";
        cin >> input;

        if (input == "1")
        {
            Rect.RectValues;
            cout << answer;
        }

        else if (input == "2")
        {
            Circle.CircleValues;
            cout << answer;
        }

        else if (input == "3")
        {
            loop = false;
            break;
        }

        cout << "\n";
    }
}

Help. please?  For users of VC++ and other enviroments that don't have a decent debugger (as far as I'm aware), my error message says something about overloading.  I didn't really understand that chapter of cplusplus.com/doc/tutorial/.

Edit:

Code:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

const float pi = 3.14;

class ShapeMath
{
    protected:float x, y, r;

    public:
        void RectValues (float,float,string);
        float RectReturn ();
        void CircleValues (float,float,string);
        float CircleReturn ();
        void TrigValues (float,float,string);
        float TrigReturn ();
};

void ShapeMath::RectValues (float a, float b, string input)
{
    cout << "Width: ";
    cin >> input;
    stringstream(input) >> a;
    cout << "Height: ";
    cin >> input;
    stringstream(input) >> b;
    cout << "\n";

    x = a;
    y = b;
    return;
}

float ShapeMath::RectReturn ()
{
    return (x * y);
}

void ShapeMath::CircleValues (float a, float b, string input)
{
    cout << "Radius: ";
    cin >> input;
    stringstream(input) >> a;
    cout << "\n";

    r = a;
    return;
}
float ShapeMath::CircleReturn ()
{
    return (r * r * pi);
}

void ShapeMath::TrigValues (float a, float b, string input)
{
    cout << "Width: ";
    cin >> input;
    stringstream(input) >> a;
    cout << "Height: ";
    cin >> input;
    stringstream(input) >> b;
    cout << "\n";

    x = a;
    y = b;
}

float ShapeMath::TrigReturn ()
{
    return (x * y * 0.5);
}

int main ()
{
    string input; // Variable used to get user input
    float answer; // Variable used to print results
    bool loop = true; // Variable used to continue the loop
    ShapeMath Rect, Circle, Trig; // Objects of ShapeMath class

    while (loop == true)
    {
        cout << "1) Rectangle\n2) Circle\n3) Triangle\n4) Quit\n\n";
        cin >> input;

        if (input == "1")
        {
            Rect.RectValues (0, 0, "");
            answer = Rect.RectReturn ();
            cout << answer;
        }

        else if (input == "2")
        {
            Circle.CircleValues (0, 0, "");
            answer = Circle.CircleReturn ();
            cout << answer;
        }

        else if (input == "3")
        {
            Trig.TrigValues (0, 0, "");
            answer = Trig.TrigReturn ();
            cout << answer;
        }

        else if (input == "4")
        {
            loop = false;
            break;
        }

        cout << "\n";
    }
}

Perfect.  Added triangles, too.

Last edited by maxskywalker (2011-09-07 16:34:21)

Offline

 

#2 2011-09-06 04:51:27

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: C++ Help

I have (I think) made your code work in c. The following calculates the area of a rectangle and a circle. I used a lot of things in the program to show you some features of c.

Ok code
[file name: mainpart.c]

Code:

#include <stdio.h>
#include "mathss.h"
int enter(int inFunction)
{
    
    if (inFunction == 1)
    {
    double xx, yy;
    float x,y,result;
    printf("Enter x value:");
    scanf("%f", &x);
    printf("Enter y value:");
    scanf("%f", &y);
    
    xx = (double)x;
    yy = (double)y;
    result = (float)rectangleArea(xx,yy);
    printf("Result: %f", result);
    }else {
        if (inFunction == 2)
        {
        double rr;
        float r, result;
        printf("Enter radius:");
        scanf("%f", &r);
        rr = (double)r;
        result = (float)circleArea(rr);
        printf("Result: %f", result);
        }else{
        printf("Not A valid operation");
        };
    };

    
    return 0;
}


int main(void)
{
    int functionTwoPerform;
    printf("This program can calulate the area or a rectangle and a circle \n");
    printf("Enter ""1"" to calc area of rectangle \n");
    printf("Enter ""2"" to calc area of circle \n");
    scanf("%d", &functionTwoPerform); 
    enter(functionTwoPerform);
    return 0;
}

[file name: mathss.h]

Code:

double circleArea(double inRadius);

double rectangleArea(double inX, double inY);

[file name: mathss.c]

Code:

#include <stdio.h>
#define PI 3.141519

double circleArea(double inRadius)
{
    double theCircleArea;
    theCircleArea = (inRadius * inRadius * PI);
    
    return theCircleArea;
}

double rectangleArea(double inX, double inY)
{
    double theRectangleArea;
    theRectangleArea = inX * inY;
        

    
    return theRectangleArea;
}

Compile with gcc using the following
gcc -Wall -pedantic -ansi -o mainpart mainpart.c mathss.c

Classes are more a java and C#, VB.Net thing.


And I know that this is not c++ but this will work on many OS's going way back as it's C89.


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#3 2011-09-06 06:06:50

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: C++ Help

what-the wrote:

Classes are more a java and C#, VB.Net thing.

sir ..
C++ was originally known as C with Classes and it has decent classs support

*_*  maybe just me though..  yeah i might be code blind lol


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#4 2011-09-06 14:08:29

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: C++ Help

what-the wrote:

I have (I think) made your code work in c. The following calculates the area of a rectangle and a circle. I used a lot of things in the program to show you some features of c.

Ok code
[file name: mainpart.c]

Code:

#include <stdio.h>
#include "mathss.h"
int enter(int inFunction)
{
    
    if (inFunction == 1)
    {
    double xx, yy;
    float x,y,result;
    printf("Enter x value:");
    scanf("%f", &x);
    printf("Enter y value:");
    scanf("%f", &y);
    
    xx = (double)x;
    yy = (double)y;
    result = (float)rectangleArea(xx,yy);
    printf("Result: %f", result);
    }else {
        if (inFunction == 2)
        {
        double rr;
        float r, result;
        printf("Enter radius:");
        scanf("%f", &r);
        rr = (double)r;
        result = (float)circleArea(rr);
        printf("Result: %f", result);
        }else{
        printf("Not A valid operation");
        };
    };

    
    return 0;
}


int main(void)
{
    int functionTwoPerform;
    printf("This program can calulate the area or a rectangle and a circle \n");
    printf("Enter ""1"" to calc area of rectangle \n");
    printf("Enter ""2"" to calc area of circle \n");
    scanf("%d", &functionTwoPerform); 
    enter(functionTwoPerform);
    return 0;
}

[file name: mathss.h]

Code:

double circleArea(double inRadius);

double rectangleArea(double inX, double inY);

[file name: mathss.c]

Code:

#include <stdio.h>
#define PI 3.141519

double circleArea(double inRadius)
{
    double theCircleArea;
    theCircleArea = (inRadius * inRadius * PI);
    
    return theCircleArea;
}

double rectangleArea(double inX, double inY)
{
    double theRectangleArea;
    theRectangleArea = inX * inY;
        

    
    return theRectangleArea;
}

Compile with gcc using the following
gcc -Wall -pedantic -ansi -o mainpart mainpart.c mathss.c

Classes are more a java and C#, VB.Net thing.


And I know that this is not c++ but this will work on many OS's going way back as it's C89.

Um, okay....  I really only feel that I need compatability for Mac OS X and Windows NT, so I'd rather stick with C++.  I have other reasons, too (like newdawnengine.weebly.com).

I have NO idea what GCC is, and I'd rather not work with multiple files until later.

Maybe if you could tell me what was wrong with my code, I could fix it.

Last edited by maxskywalker (2011-09-06 14:08:48)

Offline

 

#5 2011-09-06 23:15:51

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: C++ Help

gcc is a compiler for c c++ Objective-C and much more. (works on Linux and Windows).

http://gcc.gnu.org/

Oh and by the way your not passing your parameters into the functions.
void RectValues (float,float,string);
void CircleValues (float,float,string);


Circle.CircleValues; Is what you have written, where are you paremeters the floats and the string?
Circle.CircleValues( parameters go here);

You seem to have done it in the most confusing way possible.


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#6 2011-09-07 09:06:16

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: C++ Help

what-the wrote:

gcc is a compiler for c c++ Objective-C and much more. (works on Linux and Windows).

http://gcc.gnu.org/

Oh and by the way your not passing your parameters into the functions.
void RectValues (float,float,string);
void CircleValues (float,float,string);


Circle.CircleValues; Is what you have written, where are you paremeters the floats and the string?
Circle.CircleValues( parameters go here);

You seem to have done it in the most confusing way possible.

Oh.  Thanks!

Offline

 

#7 2011-09-07 09:11:45

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: C++ Help

what-the wrote:

gcc is a compiler for c c++ Objective-C and much more. (works on Linux and Windows).

http://gcc.gnu.org/

Oh and by the way your not passing your parameters into the functions.
void RectValues (float,float,string);
void CircleValues (float,float,string);


Circle.CircleValues; Is what you have written, where are you paremeters the floats and the string?
Circle.CircleValues( parameters go here);

You seem to have done it in the most confusing way possible.

Thanks!  This fixed it.  Like I said, I'm still really new to C++.   Also, the main reason that this is confusing and illogical is that it was meant as practice to test most of my knowledge of C++.

Offline

 

Board footer