So I got this program to run, but now any numbers that I input come out as really large numerals. Do I need to add a header for math computation? Or is there something like with C, a printf function for C++?
#include <iostream> // Necessary
using namespace std;
#define mMaxOf2(max, min) ((max) > (min) ? (max) : (min))
#define mMaxOf3(Min, Mid, Max)\
{\
mMaxOf2(mMaxOf2((Min), (Mid)),(Max))\
}
int main()
{
double primary;
double secondary;
double tertiary;
long double maximum = mMaxOf3(primary, secondary, tertiary);
cout << "Please enter three numbers: ";
cin >> primary >> secondary >> tertiary;
cout << "The maximum of " << primary << " " << secondary << " " << tertiary;
cout << " using mMaxOf3 is " << maximum;
return 0;
}
The problem is that you are computing
maximumbefore you get the user’s input. Rearrange your code like this:Always remember that code is executed sequentially, or you may say line-by-line. For C++, any changes to variables affect the behavior of later instructions using those modified variables. In your case, you computed
maximumfirst before you have properly set the values ofprimary,secondaryandtertiarywhich are needed inmMaxOf3.As a note, it is completely OK to have
primary,secondary,tertiaryandmaximumhave uninitialized values in the case of your program.