Ошибка компиляции: «указатель может быть вычтен только из другого указателя»

Только учится 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 <<и вам также не хватает точки с запятой.

2

Другие решения

Обновить:

Это выглядит как y0, y1 а также yn являются указано как часть POSIX что объясняет, почему вы не найдете это в документации С или же C ++ стандарты:

Функции y0 (), y1 () и yn () должны вычислять функции Бесселя для x второго порядка: 0, 1 и n соответственно.

Решение для глобальное загрязнение пространства имен это к объявите свои переменные в собственном пространстве имен.

оригинал:

У тебя есть < на месте << в нескольких ваших cout звонки, например здесь:

cout<"FORMULA L2+H2";
^

должно быть:

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

Также в clang а также gccЯ получаю конфликт с вашим глобальным y1 и глобальный y1 от cmath заголовок, который делает это:

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

и несколько других строк, мое решение было переименовать y1 но лучшее решение — не использовать глобалы.

3

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector