Hello RobertCalvin,
As I started working with the program I observes this:
In the section "variable declaration" you define all your variables nicely, but they are all uninitialized. This was not a problem until I started changing the program around. In the "processing system" section I put a comment on all the "cin" statements because "answer"s 1 - 4 need to be calculated not input. If you want user input for something these need to be four new variables that will eventually be compared to the "answer?" variables.
So when my compiler gave me an error that the "answer?" variables were not initialized I went back and initialized all the variables. Some will say that not all variables need to be initialized. I can see this with "num1" and "num2" as the first thing you do after defining the variable is to input something into these variables. With the variable "answer1" - 4 with the way you originally used them they do need to be initialized because when you use them in the 'output" section they have no value and cause a compiler error.
I believe that from C++11 on, could be C++98, the uniform initializer of {}s is the easiest way to initialize a variable. An empty set of {}s will initialize to zero. In the case of a "double" "0.0". The added advantage is that you can put a number between the {}s if needed. Since it is so easy to type I feel that it is a good idea to initialize numeric variables when defined. I also feel that it is a good idea to know the starting value of a variable and know that it is not garbage.
Later as I added to the program initialization was not necessary, but since it was there already I left it that way.
The "input system" section is OK, although I did change the prompt slightly.
Eventually I added the section " Calculating answers" to give "answe?"s 1 - 4 a value for later use.
Between the "input" and "calculating" sections I added a line to manipulate the output for a better look with decimal point and the amount of numbers to the right of the decimal point.
In this line
std::cout << std::fixed << std::showpoint << std::setprecision(4);
the "fixed" means to show a decimal number not scientific notation. The "showpoint" means to show ".0" if there is nothing to the right of the decimal point. The "setprecision(4)" tells the output how many numbers to the right of the decimal point to print, that is the number in (). This only needs to be done once before any output involving a "double" or "float".
In the "processing system" you can see what I did.
In the "output" section there are minor changes I did. You should be able to see what they are in the following code. Mostly it has to do with spaces around things like "<<" and in the string constants like " + ". This makes the code easier to read and also the output easier to read when things are not run together.
An example of my output:
please enter a num1: 2.5
please enter a num2: 11.3
13.8000
-8.8000
28.2500
0.2212
2.5000 + 11.3000 = 13.8000
2.5000 - 11.3000 = -8.8000
2.5000 * 11.3000 = 28.2500
2.5000 / 11.3000 = 0.2212
Press Enter to continue:
|
As you can see the output is easier to read.
After the output I added the two lines before the "cin.get()" to make it work. Since you used "cin >> aVariable" previous to the "cin.get()" there is a new line character left in the input buffer that "cin.get()" is extracting and then moving on to end the program. The "cin.ignore" line will clear the input buffer allowing the "cin.get()" to wait for enter.
I usually offer this as a C++ replacement to people who use "system("pause")" in their program. I offer it here in place of the quick fix I made;
1 2 3 4 5 6
|
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needid. 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();
|
The "return 1;" for the last line is the wrong usage. A zero means that the return is normal and there was no problem with the program. Any number greater then zero meas that there was a problem and that number can be used to tell what the problem is. Also if there is more than one return statement in a program the number can help you find where the problem is.
With that I offer this revision to your code to give you an idea of what could be done:
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
#include <iomanip> // <--- Added for line 22.
//using namespace std; // <--- Best not to use.
int main()
{
// variable declaration
double num1{};
double num2{};
double answer1{};
double answer2{};
double answer3{};
double answer4{};
// input system
std::cout << "please enter a num1: "; // <--- Changed.
std::cin >> num1;
std::cout << "please enter a num2: ";
std::cin >> num2;
std::cout << std::fixed << std::showpoint << std::setprecision(4);
// Calculating answers. Added this section.
answer1 = num1 + num2;
answer2 = num1 - num2;
answer3 = num1 * num2;
answer4 = num1 / num2;
//processing system
std::cout << answer1 << std::endl;
//std::cin >> answer1;
std::cout << answer2 << std::endl;
//std::cin >> answer2;
std::cout << answer3 << std::endl;
//std::cin >> answer3;
std::cout << answer4 << std::endl;
//std::cin >> answer4;
//output
std::cout << "\n" << num1 << " + " << num2 << " = " << answer1;
std::cout << "\n" << num1 << " - " << num2 << " = " << answer2;
std::cout << "\n" << num1 << " * " << num2 << " = " << answer3;
std::cout << "\n" << num1 << " / " << num2 << " = " << answer4 << std::endl;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: "; //<--- Added.
std::cin.get();
return 0; //<--- Changed. Anything above zero means that there was a promlem. Zero is a normal return.
}
|
Hope that helps,
Andy