Hello irene 127,
When I started to work on the code I first noticed:
cout << num(n, a, b) << endl;
. Here you are calling the function with 3 "int"s, but the function is expecting to receive "int, int, char**".
The problem is with a prototype, function definition and function call everything has to match. It is rare when a function sends 1 variable type and the function definition changes it to a different type, i.e., sending an "int" to a function and changing it to a "double" in the function is not a problem. The reverse though would cause data loss trying to put a "double" into an "int".
In the following code I want you to notice:
The blank lines that make the code easier to read.
The function and variable names and how it makes the code easier to understand and follow.
And how the function definition now matches the function call.
Also feel free at any time to change variable names if you want. Just be sure that the name reflect what the variable is or does.
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 28 29 30 31 32 33 34 35
|
#include <iostream>
//#include <iomanip> // <--- Not used here.
#include <string>
#include <limits>
int CheckNum(int num, int min, int max)
{
while ((num < min) || (num > max))
{
std::cerr << "\n Your number is out of range! Try again.\n\n insert a new number: ";
std::cin >> num;
}
return num;
}
int main(int argc, char** argv)
{
int num;
int min = atoi(argv[1]);
int max = atoi(argv[2]);
std::cout << "\n Enter a number: ";
std::cin >> num;
std::cout << "\nYour number " << CheckNum(num, min, max) << " is within the correct range.\n";
// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
}
|
Andy