Question

Just learning C++, and I'm enjoying making my first program it's not much, it just solves math problems I'm having some issues with the distance formula as far as I know my logic is right sqrt((x2-x1)+(y2-y1)). However I am getting the error

error C2113: '-' : pointer can only be subtracted from another pointer which is throwing me off.

BONUS question... if anybody could point me to a method of directing users back to the first menu when they have finished with a previous selection that would be cool. I'm assuming some sort of loops but I haven't covered that yet in my C++ class.

Here is the code(sorry if it is messy but I haven't learned proper formatting yet)

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;


int selection;
int choice;
int choice2;
double height;
double length;
double a;
double x;
double y;
double const pi = 3.14;
double r;
double m;
double x1;
double x2;
double y1;
double y2;
double distance;

int main(){
cout<<"-------------------------"<<endl;
cout<<"     MATH DESTROYER      "<<endl;
cout<<"-------------------------"<<endl;
cout<<"  1-Geometry Solver      "<<endl;
cout<<"  2-Algebra Solver       "<<endl;
cout<<"  3-"<<endl;
cout<<endl;
cout<<"  Select Option number:  "<<endl;
cin>>selection;
cout<<endl;
cout<<endl;
    switch(selection){
    case 1:
    cout<<"-------------------------"<<endl;
    cout<<"     GEOMETRY SOLVER     ";cout<<endl;
    cout<<endl;
    cout<<" 1-Perimeter of a square "<<endl;
    cout<<" 2-Area of a square      "<<endl;
    cout<<" 3-Perimeter of a rectangle"<<endl;
    cout<<" 4-Area of a rectangle   "<<endl;
    cout<<" 5-Perimeter of a circle "<<endl;
    cout<<" 6-Area of a circle      "<<endl;
    cout<<" Select Option number:   "<<endl;
    cout<<"-------------------------"<<endl;
    cin>>choice;
    cout<<endl;
    cout<<endl;
    switch(choice){
            case 1:
                cout<<"-------------------------"<<endl;
                cout<<"  PERIMITER OF A SQUARE  "<<endl;
                cout<<endl;
                cout<<"Enter Height"<<endl;
                cin>>height;
                cout<<"Enter Length"<<endl;
                cin>>length;
                cout<<"Perimiter of Square ="<<(height*2)+(length*2)<<endl;
                cout<<"-------------------------"<<endl;
                cout<<endl;
                cout<<endl;

                system("pause");
                return 0;

            case 2:
                cout<<"AREA OF A SQUARE"<<endl;
                cout<<endl;
                cout<<"Enter Length"<<endl;
                cin>>length;
                cout<<endl;
                cout<<endl;
                cout<<"FORMULA L^2";
                cout<<endl;
                cout<<endl;
                cout<<"AREA of Square ="<<pow(length,2)<<endl;//(pow)(length,2)= length to the power of 2 or lenght^2
                cout<<endl;
                cout<<endl;

                system("pause");
                return 0;

            case 3:
                cout<<"PERIMITER OF A RECTANGLE"<<endl;
                cout<<endl;
                cout<<"Enter Height"<<endl;
                cin>>height;
                cout<<"Enter Length"<<endl;
                cin>>length;
                cout<<endl;
                cout<<endl;
                cout<"FORMULA L2+H2";
                cout<<endl;
                cout<<endl;
                cout<<"Perimiter of Rectangle ="<<(height*2)+(length*2)<<endl;
                cout<<endl;
                cout<<endl;

                system("pause");
                return 0;

            case 4:
                cout<<"AREA OF A RECTANGLE"<<endl;
                cout<<endl;
                cout<<"Enter Height"<<endl;
                cin>>height;
                cout<<"Enter Length"<<endl;
                cin>>length;
                cout<<endl;
                cout<<endl;
                cout<"FORMULA L2+H2";
                cout<<endl;
                cout<<endl;
                cout<<"Perimiter of Rectangle ="   <<height*length<<endl;
                cout<<endl;
                cout<<endl;

                system("pause");
                return 0;

                case 5:
                cout<<"PERIMITER OF A CIRCLE"<<endl;
                cout<<endl;
                cout<<"Enter Radius"<<endl;
                cin>>r;
                cout<<endl;
                cout<<endl;
                cout<<"FORMULA peremiter=PI X D";
                cout<<endl;
                cout<<endl;
                cout<<"Perimiter of circle ="<<pi*(r*2)<<endl;
                cout<<endl;
                cout<<endl;

                system("pause");
                return 0;

                case 6:
                cout<<"AREA OF A CIRCLE"<<endl;
                cout<<endl;
                cout<<"Enter Radius"<<endl;
                cin>>r;
                cout<<endl;
                cout<<endl;
                cout<<"FORMULA PI X R^2";
                cout<<endl;
                cout<<endl;
                cout<<"Area of Circle ="<<pi*(pow(r,2))<<endl;
                cout<<endl;
                cout<<endl;

                system("pause");
                return 0;

    }
    case 2:
    cout<<"      ALGEBRA MASTER     "
    cout<<"-------------------------"<<endl;
    cout<<"    1-Distance Formula   "<<endl;
    cout<<"    2-Slope              "<<endl;
    cout<<"    3-Pythagorean Theorm "<<endl;
    cout<<"-------------------------"<<endl;
    cout<<endl;
    cout<<"Select Option number:"<<endl;
    cin>>choice2;
        switch(choice2){
            case 1:
                cout<<"Distance Formula"<<endl;
                cout<<endl;
                cout<<"Enter first y point (y1)"<<endl;
                cin>>y1;
                cout<<endl;
                cout<<"Enter second y point (y2)"<<endl;
                cin>>y2;
                cout<<endl;
                cout<<"Enter first x point (x1)"<<endl;
                cin>>x1;
                cout<<endl;
                cout<<"Enter second x point (x2)"<<endl;
                cin>>x2;
                cout<<"D=sqrt (x2-x1)+(y2-y1)";
                cout<<endl;
                cout<<endl;
                cout<<"Distance ="<<sqrt((x2-x1)+(y2-y1))<<endl;
                cout<<endl;
                cout<<endl;

                system("pause");
                return 0;

                case 2:
                cout<<"AREA OF A CIRCLE"<<endl;
                cout<<endl;
                cout<<"Enter Radius"<<endl;
                cin>>r;
                cout<<endl;
                cout<<endl;
                cout<<"FORMULA PI X R^2";
                cout<<endl;
                cout<<endl;
                cout<<"Area of Circle ="<<pi*(pow(r,2))<<endl;
                cout<<endl;
                cout<<endl;

                system("pause");
                return 0;

                case 3:
                cout<<"AREA OF A CIRCLE"<<endl;
                cout<<endl;
                cout<<"Enter Radius"<<endl;
                cin>>r;
                cout<<endl;
                cout<<endl;
                cout<<"FORMULA PI X R^2";
                cout<<endl;
                cout<<endl;
                cout<<"Area of Circle ="<<pi*(pow(r,2))<<endl;
                cout<<endl;
                cout<<endl;

                system("pause");
                return 0;

    }


    }
}
Was it helpful?

Solution 2

I think the main problem is that your global variable y1 may be conflicting with a function defined in math.h (or cmath). You can get around that by declaring your variables locally in the main() function, rather than as globals. That allows the variable name to mask the existing function, and avoids the conflict.

EDIT: The alternative solution would be to rename y1 to something else, e.g. Y1 (upper-case).

There are also a couple of other issues in the code. In two places you've got cout< instead of cout <<, and you're also missing a semi-colon.

OTHER TIPS

Update:

It looks like y0, y1 and yn are specified as part of POSIX which would explain why you won't find this documented in the C or C++ standards:

The y0(), y1(), and yn() functions shall compute Bessel functions of x of the second kind of orders 0, 1, and n, respectively.

The solution to global namespace pollution is to declare your variables in your own namespace.

Original:

You have < in place of << in a few of your cout calls, for example here:

cout<"FORMULA L2+H2";
    ^

should be:

cout<<"FORMULA L2+H2";
    ^^

Also in clang and gcc, I am getting a conflict with your global y1 and a global y1 from the cmath header, which makes this:

cout<<"Distance ="<<sqrt((x2-x1)+(y2-y1))<<endl;
                                     ^^

and a few other lines break, my solution was to rename y1 but the better solution is to not use globals.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top