Easyish C++ coding

Please help with this
5. Declare the following variables:

double input1 (first user input),
double input2 (second user input),
double minimumValue (lower inputted value),
double maximumValue (higher inputted value).

7. In order to use the minimum and maximum functions, include the math library by inserting these two lines of code at the beginning of your source file (just below the initial comments):

#define _USE_MATH_DEFINES
#include <cmath>

8. Using the cout function in a single command, display the text "Welcome to Minimum and Maximum!". Follow this with a line break.
Prompt the user to enter the two numbers separately and then store them in the appropriate variables. The prompts should state something like "Enter the first number".
9. Determine the minimum and maximum values (hint: use the fmin and fmax functions from the math library), store them in variables, and display those results. The final message should state something like "The number 23 is lower than 65".

10. Liberally comment the code. Each variable must have a descriptive comment on the same line. I have included the comments to use in the parenthesis after each variable above.

The output should look something like this -- user inputs are in bold blue type:
Welcome to Minimum and Maximum!
Enter the first number
4.2
Enter the second number
3
The number 3 is lower than 4.2

What I have so far is:
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
using namespace std;

int main ()
{
double input1;

cout << "Welcome to Minimum and Maximum!\n";
cout << "Enter the first number. \n";

cin >> input1;

double input2;
cin >> input2;

double minimumValue;
double maximumValue;


cout << "Enter the second number. \n";

return 0;
}

Can someone finish the code or help me to finish it?
Last edited on
PLEASE learn to use code tags, it makes reading and commenting on your code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

HINT: You can edit your post and add the tags.

If your compiler is C++11 (or later) compliant there is no need for the #define:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>

int main()
{
   std::cout << "Enter the first number: ";
   double input1 { };
   std::cin >> input1;

   std::cout << "Enter the second number: ";
   double input2 { };
   std::cin >> input2;
   std::cout << '\n';

   double min_num { fmin(input1, input2) };
   double max_num { fmax(input1, input2) };

   std::cout << "Minimum: " << min_num << ", Maximum: " << max_num << '\n';
}

Enter the first number: 155
Enter the second number: 23

Minimum: 23, Maximum: 155
There is also std::min & std::max in <algorithm>:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <algorithm>

int main()
{
   std::cout << "Enter the first number: ";
   double input1 { };
   std::cin >> input1;

   std::cout << "Enter the second number: ";
   double input2 { };
   std::cin >> input2;
   std::cout << '\n';

   double min_num { std::min(input1, input2) };
   double max_num { std::max(input1, input2) };

   std::cout << "Minimum: " << min_num << ", Maximum: " << max_num << '\n';
}

(same inputs, same output)
I looked around the interwebz and that required #define will not do anything for your program. It won't add any math constants that are useful to you.

https://codeforces.com/blog/entry/51835
Hello cgperez,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



First you should get a better understanding of your directions.
10. Liberally comment the code. Each variable must have a descriptive comment on the same line. I have included the comments to use in the parenthesis after each variable above.

I do not see any comments in your code. It is better to put something to at least give you a hint and fix up later.

It would be helpful if you would mention what IDE/compiler you are using.

Your program is a good start, but I did rearrange it a bin with examples.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

int main()
{
	double input1{};  // First user input.
	double input2{};  // Second user input.
	double minimumValue{};
	double maximumValue{};

	std::cout << "\n Welcome to Minimum and Maximum!\n";

	std::cout << "\n Enter the first number.: ";
	std::cin >> input1;
	
	std::cout << " Enter the second number.: ";
	std::cin >> input2;




	return 0;
}

For the comments I have found that two spaces before the "//" and one space after makes the comment easier to read.

For your prompts if you end the string with a ": " and leave out and "\n" or following "std::endl" this will put the "std::cin" on the same line as the prompt. Personally I think it looks better, but it is your choice.

Also you have the std::cin >> input2; before the prompt. Very confusing when the screen is blinking at you waiting for something and yu have no idea what to do.

The only steps left to do is 9 and 10. And Furry Guy's example should help even if you do not fully understand it the concept should help.

Hope that helps,

Andy
Topic archived. No new replies allowed.