C++ gradient function returning false value - c++

User will enter 4 different points and findSlope (function) will calculate the value for slope and return back the value to be cout in main function.
But when I ran the program, it had logic error for slope value. Any idea why?
#include <iostream>
#include <iomanip>
using namespace std;
float findSlope(float a,float b,float c,float d)
{
return (d-b/c-a);
}
int main()
{
float slope,p1,q1,p2,q2;
cout << "Enter x1: ";
cin >> p1;
cout << "Enter y1: ";
cin >> q1;
cout << "Enter x2: ";
cin >> p2;
cout << "Enter y2: ";
cin >> q2;
slope=findSlope(p1,q1,p2,q2);
cout << "Point1" << "\t\tPoint2" << "\t\tSlope" << endl;
cout << fixed << setprecision(2) << p1 << "," << q1 << "\t" << p2 << "," << q2 << "\t" << slope << endl;
return 0;
}

return (d-b/c-a);
Please look at order of operations again. What you intended to calculate was (d-b)/(c-a). Right now you are calculating d - (b/c) - a. This is a basic mathematical thing and your calculator would most likely give you the same output if you had tried to check this yourself.

Your slope function has
return (d-b/c-a)
did you mean
return ((d-b)/(c-a))
Operator precedence will do the division first, without the brackets.

Related

How do i fix this error: undefined reference to `distance(float, float, float, float)'

So I am not really sure what to do here, I have gone back over my code multiple times and it all seems to be right but I keep getting the error code
Stubblefield9.cpp:74: undefined reference to `distance(float, float, float, float)'
collect2.exe: error: ld returned 1 exit status
Here is my code if anyone can help me.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
const float pi=3.14;
int choice;
char again;
do
{
cout << " IHCC Computer Science Registration Menu\n";
cout << " ======================================\n";
cout << " 1. The Volume of a Cone\n";
cout << " 2. The Volume of a Sphere\n";
cout << " 3. The Area of an Octagon\n";
cout << " 4. The Destance between two Points\n";
cout << " ======================================\n";
cout << " Enter your selection: ";
cin >> choice;
switch (choice)
{
case 1:
float coneRadius,coneHeight,coneVolume;
cout<<"Enter the Radius of the cone:";
cin>>coneRadius;
cout<<"\nEnther the Height of the Cone: ";
cin>>coneHeight;
coneVolume=pi*coneRadius*coneRadius*coneHeight/3;
cout<<"\nThe Volume of a Cone with Radius ("<< coneRadius << ") and Height (" << coneHeight << setprecision(2) << fixed << ") is " << coneVolume;
break;
case 2:
float sphereRadius,sphereVolume;
cout << "Please insert the Radius: ";
cin >>sphereRadius;
sphereVolume = (4/3)*(pi)*(sphereRadius*sphereRadius*sphereRadius);
cout<<"Volume with radius "<< setprecision(1) << fixed << sphereRadius << setprecision(2) << fixed << " is "<<sphereVolume;
break;
case 3:
float octagonSide, octagonArea;
cout << "Input side length: ";
cin >> octagonSide;
octagonArea =(2 * (1 + sqrt(2)) * octagonSide * octagonSide);
cout << "\nThe area of the octagon with side length (" << octagonSide << setprecision(2) << fixed << ") is "<< octagonArea;
break;
case 4:
float x, y, a, b, answer;
float distance(float x, float y, float a, float b);
cout << "Enter the points for the coordinates";
cout << endl;
cout << "Point x for first coordinates: ";
cin >> x;
cout << endl;
cout << endl;
cout << "Point y for first coordinate: ";
cin >> y;
cout << endl;
cout << endl;
cout << "Point x for the second coordinate: ";
cin >> a;
cout << endl;
cout << endl;
cout << "Point y for the second coordinate: ";
cin >> b;
cout << endl;
cout << endl;
answer = distance(x, y, a, b);
cout << "The answer is " << answer;
break;
default:
cout << "Not a Valid Choice. \n";
cout << "Choose again.\n";
cin >> choice;
break ;
}
cout << "\n\n Would you like to do it again(y or n)";
cin >> again;
} while( again == 'y' || again == 'Y' );
return 0;
}
You get that error because you try to call a function distance() that is declared in your code by
float distance(float x, float y, float a, float b);
but not defined.

inf output computing line slopes

I am very new at C++.
I wrote this code below which is supposed to tell me if 2 lines have an intersection point, so I figured two lines with equal "M" in the y=Mx+B equation should not intersect and all others would.
The program seems to be understanding this, but unless the slope of the inputted line segment is 0 it outputs inf or -inf.
why is this happening?
#include <iostream>
using namespace std;
int main ()
{
typedef double vector2d[2];
vector2d pointA, pointB, pointC, pointD;
double LineSeg1, LineSeg2;
double yes, no;
cout << "Enter x for point A: ";
cin >> pointA[0];
cout << "Enter y for point A: ";
cin >> pointA[1];
cout << "Point A = (" << pointA[0] << "," << pointA[1] << ")" << endl;
cout << "Enter x for point B: ";
cin >> pointB[0];
cout << "Enter y for point B: ";
cin >> pointB[1];
cout << "Point B = (" << pointB[0] << "," << pointB[1] << ")" << endl;
cout << "Enter x for point C: ";
cin >> pointC[0];
cout << "Enter y for point C: ";
cin >> pointC[1];
cout << "Point C = (" << pointC[0] << "," << pointC[1] << ")" << endl;
cout << "Enter x for point D: ";
cin >> pointD[0];
cout << "Enter y for point D: ";
cin >> pointD[1];
cout << "Point D = (" << pointD[0] << "," << pointD[1] << ")" << endl;
LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointB[0]));
cout << "slope segment 1 = (" << LineSeg1 << endl;
LineSeg2 = ((pointD[1]-pointC[1])/(pointD[0]-pointC[0]));
cout << "slope segment 2 = (" << LineSeg2 << endl;
if ( LineSeg1 == LineSeg2 ) {
cout << "no\n";
}
else ( LineSeg1 != LineSeg2 ) ;{
cout << "yes\n";
}
return 0;
}
This line:
LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointB[0]));
has a divide by zero error.
I believe the equation should be:
LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointA[0]));

Basic Console Calculator (Storing a string in a variable) C++

I'm trying to create a basic console calculator in C++. I'm having a bit of trouble storing a string in a variable from a cin command.
Here is the program for some clarification:
#include <iostream>
using namespace std;
int main()
{
string type_cal;
cout << "Please enter the type of calculation you would like to use: \n";
cout << "1. Addition \n";
cout << "2. Subtraction \n";
cout << "3. Multiplication \n";
cout << "4. Division \n \n";
cin >> type_cal;
if (type_cal = "Addition" or "1")
{
int a;
int b;
int sum;
cout << "Please enter a number to add: \n";
cin >> a;
cout << "Please enter another number: \n";
cin >> b;
sum = a + b;
cout << "The sum of those numbers is: " << sum << endl;
return 0;
}
}
Currently I am in the addition phase since I recently ran into this problem. Quick answers would be appreciated, thank you!
if(type_cal = "Addition" or "1") simply does not make sense.
if(type_cal == "Addition" || type_cal == "1") {
}
Ok I found the problem, or is actually used as || in c++ (thanks aerkenemesis), and = is not the same as == which means equal to (another thanks to Lorehed). Program is working fine now.
For those who are curious, here is the new and revised version of my simple calculator:
#include <iostream>
using namespace std;
float addition();
float subtraction();
float multiplication();
float division();
int main()
{
string type_cal;
cout << "Please enter the type of calculation you would like to use: \n";
cout << "1. Addition " << endl;
cout << "2. Subtraction " << endl;
cout << "3. Multiplication " << endl;
cout << "4. Division" << endl << endl;
cin >> type_cal;
if(type_cal == "Addition")
{
addition();
}
if(type_cal == "Subtraction")
{
subtraction();
}
if(type_cal == "Multiplication")
{
multiplication();
}
if(type_cal == "Division")
{
division();
}
return 0;
}
float addition()
{
float a;
float b;
float sum;
cout << "Please enter a number to add: " << endl;
cin >> a;
cout << "Please enter another number: " << endl;;
cin >> b;
sum = a + b;
cout << "The sum of those numbers is: " << sum << endl;
}
float subtraction()
{
float c;
float d;
float difference;
cout << "Please enter a number to subtract: \n";
cin >> c;
cout << "Please enter another number: \n";
cin >> d;
difference = c - d;
cout << "The difference of those numbers is " << difference << endl;
}
float multiplication()
{
float e;
float f;
float product;
cout << "Please enter a number to multiply: \n";
cin >> e;
cout << "Please enter another number: \n";
cin >> f;
product = e * f;
cout << "The product of those numbers is " << product << endl;
}
float division()
{
float g;
float h;
float quotient;
cout << "Please enter a number to divide: \n";
cin >> g;
cout << "Please enter another number: \n";
cin >> h;
quotient = g / h;
cout << "The quotient of those numbers is " << quotient << endl;
}

KSP DELTA V FINDER: Why does C++ assume that log( ) is a function?

I'm attempting to create a program to figure out Delta-V for my Kerbal Space Program game, and C++ (being run in the Eclipse IDE) does not allow for use of log() without assuming I'm trying to call a function. Thank you so much for help! It's really nice of you.`
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "Hello. Welcome to the Kerbal Space Program Delta V Calculator. \n";
cout << " \n";
cout << "Note that each stage must use the same engine for this calculator.";
cout << "\n";
cout << "\nHow many stages make up your rocket? :";
int stageNumber;
cin >> stageNumber;
//cout << "Your rocket has " << stageNumber << " stages.\n";
cout << "\n\nStart from the bottom stage, please. ";
//ACTUAL DELTA V CALCULATIONS
for(int currentStage = 1; currentStage <= stageNumber; currentStage = currentStage + 1){
cout << "What is the total mass of this stage? :";
int totalMass;
cin >> totalMass;
cout << "What is the fuel mass of this stage? :";
int fuelMass;
cin >> fuelMass;
cout << "\n";
int dryMass;
dryMass = totalMass - fuelMass;
cout << "Your dry mass is" << dryMass << "\n";
cout << "\n";
cout << "Give the specific impulse of this stage's engine. \n";
int iSP;
cin >> iSP;
cout << "Here is the Delta V of your rocket.\n";
int deltaMass;
deltaMass = totalMass/dryMass;
int deltaV;
deltaV = iSP * log(deltaMass);
cout << deltaMass
exit(0);
}
}
`
log() is a function in the C standard library that takes the natural logarithm of a number. The name is effectively reserved — pick something else.

1.#QNAN error C++

I am new to programming and trying to write a new program. While checking through my program it is returning the error code 1.#QNAN. I have tried isolating the variable and researching for answers but cannot find any solutions.
My code:
// This is a program to decide what culvert should be properly used for a specific installation
// using given measurements and data
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
// initializing functions
double slope_function();
double cbasin();
// initializing classes: Subdivisions specs
//intitializing global variables
double edge_road =0;
double up_stream =0;
double down_stream =0;
double tbm =0.0;
//double culv_length =0;
double slope = 0.0 ;
char street_name[1001];
int min_culv = 15;
double up_strm_culv =0;
double dwn_strm_culv =0;
int main (int nNumberofArgs, char* pszArgs[])
{
cout<< "This program will allow the surveyor to double check their calculations\n";
cout << "in deciding what size, type, and requirements are allowed for the\n";
cout << "installation of culverts in Terrebonne Parish.\n\n";
// begin input
cout << "what is the name of the street\nwhere the culverts will be installed: ";
cin.getline (street_name,1000);
cout << endl;
cout << "What is the Benchmark: ";
cin >> tbm;
cout << endl;
cout << "What is the elevation of the edge of the road: ";
cin >> edge_road;
cout << endl;
cout << "What is the up-stream culvert size: ";
cin >> up_strm_culv;
cout << endl;
cout << "What is the culverts up-stream inverted elevation: ";
cin >> up_stream;
cout << endl;
cout << "What is the down-stream culvert size: ";
cin >> dwn_strm_culv;
cout << endl;
cout << "What is the culverts down-stream inverted elevation: ";
cin >> down_stream;
cout << endl;
cout << "What is the length of culvert requested: ";
cin >> culv_length;
cout << "Your slope is : ";
cout << slope_function();
cout << endl;
cout << street_name;
cout << endl;
cout << cbasin();
cout << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system ("pause");
return 0;
}
// slope function
double slope_function()
{
double riseoverrun = 0.0;
slope = (up_stream - down_stream)/ culv_length;
return slope;
}
// Catch Basin function
double cbasin ( )
{
double cb = 0;
cb = culv_length / 60;
cout << endl;
cout << "You need ";
cout << cb;
cout << " catch basins for this job.";
cout << endl;
}
1#QNAN is a string representation for a "quiet NAN". A "NAN" is "not-a-number" and applies only to floats and doubles.
NANs can be very useful actually for representing "null" values (rather than picking some genuine number and hoping for the best you don't need that number for its natural meaning).
Some mathematical operations can return a NAN if the operation is "not valid" (eg taking the log of a negative number).
You can generate a QNAN from C++ using
double d = std::numeric_limits<double>::quiet_NaN();
Any comparison operation (==, <= etc) on a NAN returns false, even comparing its equality to itself, except for != which always returns true (even when comparing to itself).
(The actual bug in your code appears to be a function that returns double but has no return statement).