Hexadecimal display?

This program is supposed to return the lowest number of three inputted double, floating numbers. But it always returns -1.#IND.

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <iostream>
using std::cin;
using std::cout;


// TODO: reference additional headers your program requires here

// exercise6-28.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

double smallest (double, double, double);

int _tmain(int argc, _TCHAR* argv[])
{
double a;
double b;
double c;
cout<<"Enter three double-precision #s";
cin>> a>>b>>c;
cout<<"The smallest number is "<<smallest (a, b,c);
cin>>a;
return 0;
}

double smallest (double a, double b, double c){
if (a>b>c)
return c;
if (b>c>a)
return a;
if (c>a>b)
return b;
}
Relational operators are evaluated left-to-right. Therefore, a>b>c is equivalent to (a>b) > c, where you compare a boolean (result of a>b) to a double (c). You are unlucky, if that gives an expected answer.

Read about std::min.
Last edited on
This is valid in terms of algebra, but not as c++ code (a>b>c)

You can separate it into two parts, (a>b) (b>c) which must both be true, so combine them with a logical and operator &&.
Last edited on
1
2
3
4
5
6
7
8
double smallest (double a, double b, double c){
if (a>b>c)
return c;
if (b>c>a)
return a;
if (c>a>b)
return b;
}


This should be something like this
first make new variable called small;
1
2
3
int small;
//assign a to small;
small = a;

Then start the comparing process
1
2
3
4
5
6
7
8
9
10
11
12
{

if(b<small)
//assign b to small now
small = b;

if(c<small)
small = c;

return small;

}


You can only compare 2 variables in the registor at a time
Last edited on
Topic archived. No new replies allowed.