只是学习 C ++, ,而且我很喜欢制作我的第一个程序,这只是解决数学问题,就我知道我的逻辑是对的,我在距离公式上遇到了一些问题 sqrt((x2-x1)+(y2-y1)). 。但是我遇到了错误

错误C2113:' - ':只能从另一个使我失望的指针中减去指针。

奖励问题...如果有人可以指出一种将用户指导回到第一个菜单的方法,当他们完成先前的选择很酷的方法时。我假设有某种循环,但我还没有在我的 C ++ 班级。

这是代码(对不起,如果这很混乱,但是我还没有学会正确的格式)

#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, y1yn指定为POSIX的一部分 这将解释为什么您找不到它在 C 或者 C ++ 标准:

Y0(),Y1()和Yn()函数应分别计算第二种订单0、1和N的X的Bessel函数。

解决方案 全球名称空间污染在您自己的名称空间中声明您的变量.

原来的:

你有 < 代替 << 在你的一些 cout 呼叫,例如在这里:

cout<"FORMULA L2+H2";
    ^

应该:

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

也在 clanggcc, ,我正在与您的全球冲突 y1 和全球 y1 来自 cmath 标题,这使它:

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

还有其他几行断裂,我的解决方案是重命名 y1 但是更好的解决方案是不使用全球范围。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top