문제

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;

    }


    }
}
도움이 되었습니까?

해결책 2

주요 문제는 글로벌 변수가 y1 정의 된 함수와 충돌 할 수 있습니다 math.h (또는 cmath). 로컬로 변수를 main() 글로벌보다는 기능. 이를 통해 변수 이름이 기존 함수를 숨길 수 있으며 충돌을 피합니다.

편집 : 대체 솔루션은 이름을 바꾸는 것입니다 y1 예를 들어 다른 것에 Y1 (상류).

코드에는 몇 가지 다른 문제도 있습니다. 당신이 가진 두 곳에서 cout< 대신에 cout <<, 그리고 당신은 또한 세미콜론을 놓치고 있습니다.

다른 팁

업데이트:

보입니다 y0, y1 그리고 yn ~이다 POSIX의 일부로 지정됩니다 이 문서화 된 이유를 설명 할 것입니다. 또는 C ++ 표준 :

y0 (), y1 () 및 yn () 함수는 각각 두 번째 종류의 주문 0, 1 및 n의 X의 베셀 함수를 계산해야합니다.

해결책 글로벌 네임 스페이스 오염 그렇습니다 자신의 네임 스페이스에 변수를 선언하십시오.

원래의:

당신은 가지고 있습니다 < 대신에 << 당신의 몇 가지 cout 예를 들어 여기에 전화 :

cout<"FORMULA L2+H2";
    ^

해야한다:

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

또한 clang 그리고 gcc, 나는 당신의 글로벌과 충돌하고 있습니다 y1 그리고 글로벌 y1 ~로부터 cmath 헤더는 이것을 만듭니다.

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

그리고 몇 가지 다른 줄이 끊어지면 내 해결책은 이름을 바꾸는 것이 었습니다. y1 그러나 더 나은 해결책은 글로벌을 사용하지 않는 것입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top