Write a simple calculator program in C++ for beginners that repeatedly allows th
ID: 3541551 • Letter: W
Question
Write a simple calculator program in C++ for beginners that repeatedly allows the user to select +,-,*,/ from a menu of operations on real values and then enter the two operands which then computes the result of applying the selected operation to those operands.
However, the only named variables you may use are pointer variables; all others must be anonymous variables.
Pay close attention to the following aspects:
error checking
loop function
pointer variables
code efficiency(smart code), readability, documentation.
Explanation / Answer
Statement: A programming statement is the smallest independent unit in a program, just like a sentence in the English language. It performs a piece of programming action. A programming statement must be terminated by a semi-colon (;), just like an English sentence ends with a period. (Why not ends with a period like an english sentence? This is beacuse period crashes with decimal point - it is hard for the dumb computer to differentiate between period and decimal point!)
For examples,
// Each of the following lines is a programming statement, which ends with a semi-colon (;)
int number1 = 10;
int number2, number3 = 99;
int product;
product = number1 * number2 * number3;
cout << "Hello" << endl;
Block: A block (or a compound statement) is a group of statements surrounded by braces { }. All the statements inside the block is treated as one unit. Blocks are used as the body in constructs like function, if-else and loop, which may contain multiple statements but are treated as one unit. There is no need to put a semi-colon after the closing brace to end a complex statement. Empty block (without any statement) is permitted. For examples,
// Each of the followings is a "complex" statement comprising one or more blocks of statements.
// No terminating semi-colon needed after the closing brace to end the "complex" statement.
// Take note that a "complex" statement is usually written over a few lines for readability.
if (mark >= 50) {
cout << "PASS" << endl;
cout << "Well Done!" << endl;
cout << "Keep it Up!" << endl;
}
if (number == 88) {
cout << "Got it" << endl;
} else {
cout << "Try Again" << endl;
}
i = 1;
while (i < 8) {
cout << i << endl;
++i;
}
int main() {
...statements...
}
White Spaces and Formatting Source Codes
White Spaces: Blank, tab and new-line are collectively called white spaces. C++, like most of the computing languages, ignores extra white spaces. That is, multiple contiguous white spaces are treated as a single white space.
You need to use a white space to separate two keywords or tokens, e.g.,
int sum=0; // Need a white space between int and sum
double average; // Need a white space between double and average
average=sum/100.0;
Additional white spaces and extra lines are, however, ignored, e.g.,
// same as above
int sum
= 0 ;
double average ;
average = sum / 100.0;
Formatting Source Codes: As mentioned, extra white spaces are ignored and have no computational significance. However, proper indentation (with tabs and blanks) and extra empty lines greatly improves the readability of the program, which is extremely important for others (and yourself three days later) to understand your programs. For example, the following hello-world works, but can you understand the program?
#include <iostream>
using namespace std;int main(){cout<<"Hello, world!"<<endl;return 0;}
Braces: Place the beginning brace at the end of the line, and align the ending brace with the start of the statement.
Indentation: Indent the body of a block by an extra 3 (or 4 spaces), according to its level.
For example,
/*
* Recommended Programming style.
*/
#include <iostream>
using namespace std;
// blank line to separate sections of codes
int main() { // Place the beginning brace at the end of the current line
// Indent the body by an extra 3 or 4 spaces for each level
int mark = 70;
if (mark >= 50) { // in level-1 block, indent once
cout << "You Pass!" << endl; // in level-2 block, indent twice
} else {
cout << "You Fail!" << endl;
}
return 0;
} // ending brace aligned with the start of the statement
Most IDEs (such as CodeBlocks, Eclipse and Netbeans) have a command to re-format your source code automatically.
Note: Traditional C-style formatting places the beginning and ending braces on the same column. For example,
/*
* Traditional C-style.
*/
#include <iostream>
using namespace std;
int main()
{
if (mark >= 50) // in level-1 block, indent once
{
cout << "You Pass!" << endl; // in level-2 block, indent twice
}
else
{
cout << "You Fail!" << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.