Hello soulworld05,
Sorry about that. I did not mean to leave those lines in the code. That is just something I use to pause the screen of the console window before it closes when the program ends.
The "std::cin.ignore()" function just clears the input buffer of whatever might be left from a previous input. If the input buffer is empty then it acts like a pause waiting for you to press enter to ignore it and continue.
That fancy part of the first parameter just comes up with a very large number to use. This should be the size of what the input buffer was defined as. The second parameter is optional and can be changed if needed. The default for the second parameter is "\n".
You could just as easily write "std::cin.ignore(1000)" or "std::cin.ignore(1000, '\n')" or even make the first parameter "10000". The idea here is a large number that is likely to be more than what would normally be entered.
As the comment says just include the header file <limits> and it should work fine. If you do not need it take it out.
A nice part of C and C++ is that you do not have to know everything at first just how to use it.
Do you mean to say, My coding is a bit hard to read? Should I separate them more.
|
Yes and yes.
When writing the source code the first goal is to make it easy to read and follow. The first benefit is to you when it comes time to go over what you have done and second to anyone else who has to read it.
The compiler does not care about white space, i.e.,
if(user_operator == 1)
could be written as
if(user_operator==1)
, and the compiler would not care. It would compile either version the same.
When you compare:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
int main()
{
std::cout << "Pick first number please: \n";
double first_input;
first_input = ask_user_input();
std::cout << "Pick second number please: \n";
double second_input;
second_input = ask_user_input();
std::cout << "Pick 1 for +: \n" << "Pick 2 for -: \n" << "Pick 3 for *: \n" << "Pick 4 for /: \n";
ask_user_operator(first_input, second_input);
return 0;
}
and
|
int main()
{
std::cout << "Pick first number please: "; // <--- leave out the "\n". Puts your input on the same line.
double first_input;
first_input = ask_user_input();
std::cout << "Pick second number please: "; // <--- leave out the "\n".
double second_input;
second_input = ask_user_input();
std::cout <<
"Pick 1 for +: \n"
"Pick 2 for -: \n"
"Pick 3 for *: \n"
"Pick 4 for /: \n";
ask_user_operator(first_input, second_input);
return 0; // <--- Not required, but makes a good break point.
} |
You decide. Which one is easier to read.
As it has been mentioned it is
ALWAYS a
GOOD idea to initialize your variables when defined. If for no other reason than to know that it does not contain a garbage value.
I see that you have used this in the function "ask_user_input()"
double input{0};
. This tells me that you are using at least the C++11 standards or later.
The "{0}" is fine, but if the {}s are empty the compiler will initialize the variable to the needed type of (0) zero based on how the variable is defined. In the case of initializing a double to be proper it should be { 0.0 } although { 0 } does work, but I would not count on it as compilers and header files are different and what works in 1 may not work in another.
In "main" you have the code:
1 2 3 4 5
|
std::cout <<
"Pick 1 for +: \n"
"Pick 2 for -: \n"
"Pick 3 for *: \n"
"Pick 4 for /: \n";
|
First this should be in the "ask_user_operator" function where it is the only place it is used. Just makes more sense. As a menu this way of writing the "cout" makes it easier to visualize what the output will be, but this should not be a menu just a statement of what symbols can be used.
Since each line is a string you do not need the insertion operator (<<) to chain everything together.
Just something small to make life easier.
In the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
void ask_user_operator(double first_input, double second_input)
{
char user_operator{};
//std::cout << // <--- Should not be a menu.
// "Pick 1 for +: \n"
// "Pick 2 for -: \n"
// "Pick 3 for *: \n"
// "Pick 4 for /: \n";
std::cout << "Enter operator (+, -, *, /): ";
std::cin >> user_operator;
if (user_operator == '+')
//std::cout << first_input + second_input;
std::cout << "\n" << first_input << " + " << second_input << " = " << first_input + second_input;
|
Just a thought to consider. Based on your instructions. Changing the "std::cout" statements in the if statements makes the output look nicer than printing just a number with nothing to say what it is for.
Pick first number please: 100
Pick second number please: 200
Enter operator (+, -, *, /): +
100 + 200 = 300
Press Enter to continue:
|
Andy