Newskooler Newskooler - 1 month ago 18
C++ Question

Debug Assertion Failed on cout function

In my code, I have a couple of places an identical

cout
function, which I decided to move outside of the
int main()
.
When I did this however and re-ran my code, when the function in question was triggered, I received a "Debug Assertion Failed" error.

the code which I have placed outside the main function.

string printresult(double smallest, double largest)
{
cout << "the smaller value is: " << smallest << "\n";
cout << "the larger value is: " << largest << "\n";
return 0;
}


I believe it has something to do with the
return 0
code, which I have added at the end of my
cout
function, without it however, my code does now even compile. Can anyone point me towards to right direction, so that I can better identify my error?

my full code

#include "../../std_lib_facilities.h"

// conversion of units to meters

double funconvert(double x, string unit)
{
const double cm_m = 1.0 / 100.0, in_cm = 2.54, ft_in = 12;
if (unit == "m")
return x = x;
else if (unit == "cm")
return x = x * cm_m;
else if (unit == "in")
return x = x * in_cm * cm_m;
else if (unit == "ft")
return x = x * ft_in * in_cm * cm_m;
else
cout << "Unknown unit value.\n";
}

// printing the large and the small variables
string printresult(double smallest, double largest)
{
cout << "the smaller value is: " << smallest << "\n";
cout << "the larger value is: " << largest << "\n";
return 0;
}

int main()
{
vector<double>numbers;
double a, b, smallest, largest;
string unit;
cout << "Please enter an intiger followed by a measurment unit [e.g 10cm]:";

// Take input
while (cin >> a >> unit)
{
// check if this is the first number entered
if (numbers.size() == 0)
{
a = funconvert(a, unit);
b = a;
numbers.push_back(a);
numbers.push_back(b);
}
else
{
//assign a and b to the last two numbers in a vector
a = funconvert(a, unit);
numbers.push_back(a);
a = numbers[(numbers.size() - 1)];
b = numbers[(numbers.size() - 2)];
// numbers.erase(numbers.begin()); // enable if desire to keep the vector empty
}

// find out which variable is larger and smaller
if (a > b && (a/b-1) >= 0.01)
{
smallest = b;
largest = a;
printresult(smallest, largest);
}
else if (a < b && (b / a - 1) >= 0.01)
{
smallest = a;
largest = b;
printresult(smallest, largest);
}
else if ((a / b - 1) < 0.01 && (a / b - 1) != 0.00 || (b / a - 1) < 0.01 && (a / b - 1) != 0.00)
{
cout << "the nubers are almost equal\n";
}
else
cout << a << " equals " << b << "\n";
}
return 0;
}

Answer

When you write return 0, the C++ standard library attempts to use the std::string constructor from a const char*. But that constructor requires you to supply a valid address to a nul-terminated character buffer else the program behaviour is undefined.

To fix, change the function to a void return type:

void printresult(double smallest, double largest)

and drop the return statement in the function body.

Also consider replacing the idiosyncratic return x = x; with return x;