1.#QNAN error C++ - 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).

Related

Issue with functions not returning values

I am trying to create a program where you enter two values in individual functions and then print them out in the main function. But I am having an error stating that my function is not returning values.
#include <iostream>
#include <iomanip>
using namespace std;
void welcome();
double mass(double m);
double freqnat(double nf);
int main()
{
double attachedmass = 0;
double naturalfrequency = 0;
welcome();
mass(attachedmass);
freqnat(naturalfrequency);
cout << attachedmass << setw(20) << naturalfrequency << endl;
}
void welcome()
{
cout << "Welcome to the spring stiffness program." << endl << endl << "This program calculates spring stiffness using mass and natural frequency to calculate your spring stiffness." << endl << endl;
system("pause");
cout << endl;
}
double mass(double m)
{
cout << "Please enter your desired mass." << endl << endl;
cin >> m;
}
double freqnat(double nf)
{
cout << "Please enter your desired natural frequency." << endl << endl;
cin >> nf;
}
I tried using return m; and return nf; at the end of the functions, hoping this would tell the function to return the values inputted by the user. Instead, the program does run but the values print out as zeroes.
You need to return values AND store them somewhere:
// storing results in variables
attachedmass = mass(attachedmass);
naturalfrequency = freqnat(naturalfrequency);
Your functions should be:
double mass(double m)
{
cout << "Please enter your desired mass." << endl << endl;
cin >> m;
return m;
}
double freqnat(double nf)
{
cout << "Please enter your desired natural frequency." << endl << endl;
cin >> nf;
return nf;
}
Having said this, you don't need to pass any parameters at all to the functions for this. They can be something like this:
#include <iostream>
#include <iomanip>
using namespace std;
void welcome()
{
cout << "Welcome to the spring stiffness program." << endl << endl << "This program calculates spring stiffness using mass and natural frequency to calculate your spring stiffness." << endl << endl;
cout << endl;
}
double mass()
{
double user_in;
cout << "Please enter your desired mass." << endl << endl;
cin >> user_in;
return user_in;
}
double freqnat()
{
double user_in;
cout << "Please enter your desired natural frequency." << endl << endl;
cin >> user_in;
return user_in;
}
int main()
{
double attachedmass = 0;
double naturalfrequency = 0;
welcome();
attachedmass = mass();
naturalfrequency = freqnat();
cout << attachedmass << setw(20) << naturalfrequency << endl;
}
So with youre program you are using the functions but you are not assigning the return value to anything so the two double values will not change, you will probably want to pass the doubles in by reference like this:
double mass(double& m);
and
double freqnat(double& nf);
Before you werent changing the two double values atall so they would not change but now you are passing them by reference which means you can change them.
Also you will not need to return anything so you can just make mass and freqnat's return type void as you just passed a reference to it. So the working program would be:
#include <iostream>
#include <iomanip>
using namespace std;
void welcome();
double mass(double m);
double freqnat(double nf);
int main()
{
double attachedmass = 0;
double naturalfrequency = 0;
welcome();
mass(attachedmass);
freqnat(naturalfrequency);
cout << attachedmass << setw(20) << naturalfrequency << endl;
}
void welcome()
{
cout << "Welcome to the spring stiffness program." << endl << endl << "This program calculates spring stiffness using mass and natural frequency to calculate your spring stiffness." << endl << endl;
system("pause");
cout << endl;
}
void mass(double& m)
{
cout << "Please enter your desired mass." << endl << endl;
cin >> m;
}
void freqnat(double& nf)
{
cout << "Please enter your desired natural frequency." << endl << endl;
cin >> nf;
}

Comparison function returning incorrect results with overloaded operator

I am not quite sure where my error is, also I am very new to programming and I know I am breaking a lot of standardization rules. I am learning. The user inputs two integers and it compares them, it always reports that the first integer entered is greater, even when it shouldn't. I want to say it is something wrong with my getlength method, but at this point I am not too sure.
#include "stdafx.h"
#include <iostream>
using namespace std;
template <class T>
//Function to find larger value a = first number, b = second number
T findLargerValue(T a, T b) {
return (a>b ? a:b);
}
//Class trip
template <class T>
class Trip {
public:
Trip<T>::Trip();
Trip(T a){
length = a;
}
T operator>(Trip yourTrip);
T getLength() {
return length;
}
private:
T length;
};
// Template function to carry into
/** This is where I seem to be losing my logic, I am trying to push larger of the two objects to the screen but it is only displaying the first user input This is supposed to take in the two bojects created and measure the lengths.**/
template<class T>
T Trip<T>::operator>(Trip yourTrip) {
if (length > yourTrip.getLength()) {
return length;
}
else{
return yourTrip.getLength();
}
}
template<class T>
Trip<T>::Trip()
{
}
int main()
{
// input for integer values
int num1 , num2 ;
cout << "Enter integer value 1: ";
cin >> num1;
cout << endl << endl;
cout << "Enter integer value 2: ";
cin >> num2;
//display message of first two numbers
cout <<"Frist Number was: " << num1 << " Second Number was: " << num2 << "\nThe Larger number of the two is "
<< findLargerValue(num1, num2) << endl << endl;
//input for double values
double num3, num4;
cout << "Enter double value 1: ";
cin >> num3;
cout << endl << endl;
cout << "Enter double value 2: ";
cin >> num4;
//display mesasge of second two numbers
cout << "Frist Number was: " << num3 << " Second Number was: " << num4 << "\nThe Larger number of the two is "
<< findLargerValue(num3, num4) << endl << endl;
// Display message for Trip function/objects
int fTlength, sTlength;
cout << "How many miles was your first trip: ";
cin >> fTlength;
Trip<int> fTrip(fTlength);
cout << endl;
cout << "How many miles was your second trip: ";
cin >> sTlength;
Trip<int> sTrip(sTlength);
Trip<int> tripLonger;
tripLonger = findLargerValue(fTrip, sTrip);
cout << "First trip in miles: " << fTlength << endl << "Second Trip in miles: " << sTlength << endl;
cout << "The longest trip was " << tripLonger.getLength() << " miles." << endl;
system("pause");
return 0;
}
You overloaded the operator > incorrectly. It should return true of false, but it returns (in this case) an integral value. It should look like this:
template<class T>
bool Trip<T>::operator>(const Trip& yourTrip)
{
return length > yourTrip.length;
}
Comparison operators should indicate whether or not the comparison is true. In your case, it was returning either length or yourTrip.length. This is a problem when findLargerValue calls operator > for the two objects. It checks if the result was true, and because any integer that isn't 0 is true, it was almost always returning the first object. If you had entered a negative number and a 0, it would have returned the second object.

Simple calculator issues (Not outputting answers)

just started reading a C++ book and one of the practice problems was to write a small calculator that takes as input one of the four arithmetic operations, the two arguments to those operations, and then prints out the results.
Sadly, the program works up until the user inputs the arithmetic option.
So if I chose to do multiplication, id write "Multiplication" and it was just stay there and not do anything after.
Image of the problem im having
#include <iostream>
#include <string>
using namespace std;
int main(){
// Simple calculator program
// Declaring three variables
float numberOne;
float numberTwo;
string operationOption;
// Asking the user which two numbers he/she will use
cout << "Enter the first number you would like to apply a arithmetic operation to: ";
cin >> numberOne;
cin.ignore();
cout << "Now enter the second number: ";
cin >> numberTwo;
cin.ignore();
// Using cin to input users selection
cout << "Enter the operation you want to perform." << endl;
cout << "The options you have are: " << endl;
cout << "Multiplication, Subraction, Division and Addition: " << endl;
cin >> operationOption;
cin.ignore();
cin.get();
// Where it all happens
if ( operationOption == "Multiplication" ) {
cout << "The first number multiplied by the second number is: " << numberOne * numberTwo << endl;
} else if ( operationOption == "Division" ) {
cout << "The first number divided by the second number is: " << numberOne / numberTwo << endl;
} else if ( operationOption == "Subtraction" ) {
cout << "The first number subtracted by the second number is: " << numberOne - numberTwo << endl;
} else if ( operationOption == "Addition ") {
cout << "The first number added to the second number is: " << numberOne + numberTwo << endl;
} else {
cout << "You entered an invalid option.";
};
}
Remove line :
cin.get();
will solve your problem

How can I get user input to exit a loop?

I have a problem with my code, every time I loop it with the answer 'y'(Yes) it loops to infinity?
I'm trying to make a loan calculator and every time the user is done calculating with a transaction and wants to reset, and do another calculation if he enters in a value 'y', and if he enters 'n' the program will end.
Here's my code so far:
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
char ans = 'y';
do {
string name;
int Months;
int n;
double LoanAmount, Rate, MonthlyInterest, TotalLoanAmount, MonthlyAmortization, OutstandingBalance;
cout << fixed << showpoint;
cout << "Enter Name of Borrower: ";
getline(cin, name);
cout << "Enter Loan Amount: ";
cin >> LoanAmount;
cout << "Enter Number of Months to Pay: ";
cin >> Months;
cout << "Enter Interest Rate in Percent(%): ";
cin >> Rate;
cout << setprecision(2);
MonthlyInterest = LoanAmount * Rate;
TotalLoanAmount = LoanAmount + (MonthlyInterest * Months);
cout << "Monthly Interest: " << MonthlyInterest << endl
<< "Total Loan Amount with interest: " << TotalLoanAmount << endl;
cout << setw(100)
<< "\n\tSUMMARY OF OUTSTANDING INSTALLMENT" << endl
<< "\tName of Borrower: " << name
<< "\n\nMonth\t\tMonthly Amortization\t\tOutstanding Balance"
<< "\n";
for(n = 1; n <= Months; n++) {
MonthlyAmortization = TotalLoanAmount / Months;
OutstandingBalance = TotalLoanAmount - MonthlyAmortization;
cout << n << "\t\t" << MonthlyAmortization << "\t\t\t" << n - 1 << OutstandingBalance << endl;
}
cout << "\nEnd of Transaction";
cout << "Do you want to compute another transaction?[y/n]?" << endl;
cin >> ans;
}
while(ans == 'y');
}
After your cin>>ans, add these two lines :
cin.clear();
cin.sync();
That usually fixes a lot of the infinite looping problems I get with cin.
Also, I would recommend against initializing ans as 'y' when you declare it. I don't think this is causing you problems but it's an uncessesary thing.
You seem to expect pressing y and enter to register as only 'y'. If you want to get the input of just one character have a look at std::cin.get(char)

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.