Quick Review
Integers are used for holding whole numbers. When using integers, keep an eye out for overflow and integer division problems.
Floating point numbers are used for holding real numbers (which can have fractional components). When using floating point numbers, keep an eye out for precision issues, rounding errors, and comparison issues.
Boolean values hold only true and false. They do not have any major issues.
Char values are integers that can be interpreted as an ASCII value. When using chars, be careful not to mix up ASCII code values and numbers, and watch for overflow and integer division problems.
Use the const keyword to declare symbolic constants instead of #define. It’s safer.
Comprehensive quiz
1) Why are symbolic constants usually a better choice than literal constants? Why are const symbolic constants usually a better choice than #defined symbolic constants?
2) Pick the appropriate data type for a variable in each of the following situations. Be as specific as possible. If the answer is an integer, pick a specific integer type (eg. short) based on range. If the variable should be unsigned or const, say so.
a) The age of the user (in years)
b) Whether the user wants color or not
c) pi (3.14159265)
d) The number of pages in a textbook
e) The price of a stock in dollars (to 2 decimal places)
f) How many times you’ve blinked since you were born (note: answer is in the millions)
g) A user selecting an option from a menu by letter
h) The year someone was born
3) Declare each of the above using Caste Hungarian Notation. Pick a good variable name. Don’t forget to assign values to any const variables.
4) Write the following program: The user is asked to enter 2 floating point numbers (use doubles). The user is then asked to enter one of the following mathematical symbols: +, -, *, or /. The program computes the answer on the two numbers the user entered and prints the results. If the user enters an invalid symbol, the program should print nothing.
Example of program:
Enter a value: 7 Enter a second value: 5 Enter one of the following: +, -, *, or /: * 7 * 5 is 35
Hint: You can check if the user has entered a plus symbol using an if statement (briefly covered in section 2.6) and the equality operator (used to compare two values for equality): if (chUserInput == '+') // fill in rest of code here
Solutions
3.1 — Precedence and associativity
|
Index
|
2.9 — Hungarian Notation
|
3.1 — Precedence and associativity
Index
2.9 — Hungarian Notation
#include#include
using namespace std;
long double GetNumber()
{
long double nvalue;
cout <> nvalue;
cout << "\n";
return nvalue;
}
long double DoCalculation(long double x, long double y)
{
char chOperation;
cout <> chOperation;
cout << "\n";
if (chOperation == '+')
cout << setprecision(16) << x << " + " << y << " = " << x + y << "\n";
if (chOperation == '-')
cout << setprecision(16) << x << " - " << y << " = " << x - y << "\n";
if (chOperation == '*')
cout << setprecision(16) << x << " x " << y << " = " << x * y << "\n";
if (chOperation == '/')
cout << setprecision(16) << x << " / " << y << " = " << x / y << "\n";
}
int main()
{
long double x = GetNumber();
long double y = GetNumber();
DoCalculation(x,y);
return 0;
}
This is my code what i writted for the Comprehensive quiz. After i was done with it and tested that it works ( and it worked as it needed ) i checked out the solution but sawed that i do did it in an other way. I want you if you have some time that to check down the code and what do you think about it.
Im totally new in this C++ programming, started with your tutorial just a few days ago but i learned java in scool so probably its recogniseable in my codeing :D .
#include
#include
using namespace std;
int main()
{
double nNumberOne = 0;
double nNumberTwo = 0;
char nSymbol;
double nEredmeny = 0;
cout <> nNumberOne ;
cout <> nNumberTwo ;
cout <> nSymbol;
if (nSymbol == ‘+’ ){
nEredmeny = nNumberOne + nNumberTwo;
cout << nNumberOne <<" + " << nNumberTwo << " = " << nEredmeny;
}
if (nSymbol == '-' ){
nEredmeny = nNumberOne – nNumberTwo;
cout << nNumberOne <<" – " << nNumberTwo << " = " << nEredmeny;
}
if (nSymbol == '*' ){
nEredmeny = nNumberOne * nNumberTwo;
cout << nNumberOne <<" * " << nNumberTwo << " = " << nEredmeny;
}
if (nSymbol == '/'){
nEredmeny = nNumberOne / nNumberTwo;
cout << nNumberOne <<" / " << nNumberTwo << " = " << nEredmeny;
}
}
Posting my code here in case anyone are stuck/interested:
main.cpp
#include <iostream>
#include "functions.h"
using namespace std;
int main()
{
cout << "Input two values and an operation" << endl << "Input first value: " << endl;
double x = getNumber();
cout << "Input second value: ";
double y = getNumber();
cout << "Input operator + - * or /: ";
switch(getChar())
{
case '+':
{
cout << "result: ";
printText(addition(x, y));
}
break;
case '-':
{
cout << "result: ";
printText(subtraction(x, y));
}
break;
case '*':
{
cout << "result: ";
printText(multiplication(x, y));
}
break;
case '/':
{
cout << "result: ";
printText(division(x, y));
}
break;
default:
{
cout << "Invalid choice";
}
}
cin.get(); //for halting the program
cin.ignore(); //for halting the program
return 0;
}
functions.cpp
#include <iostream>
using namespace std;
double addition(double x, double y)
{
return x+y;
}
double subtraction(double x, double y)
{
return x-y;
}
double multiplication(double x, double y)
{
return x*y;
}
double division(double x, double y)
{
return x/y;
}
void printText(double x)
{
cout << x << endl;
}
double getNumber()
{
double x;
cin >> x;
return x;
}
char getChar()
{
char a;
cin >> a;
return a;
}
functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
double addition(double x, double y);
double subtraction(double x, double y);
double multiplication(double x, double y);
double division(double x, double y);
void printText(double x);
double getNumber();
char getChar();
#endif
~KanedaSyndrome
This is my code, but whenever I multiply an decimal say 1.1 * 2.2 it comes back zero. Although if I add them, it comes out 3.3 … I’m so confused >_> lol
#include
#include
#include
using namespace std;
double dnUserInput()
{
cout <> dnValue;
return dnValue;
}
char chGetOperator()
{
cout <> chTempOP;
return chTempOP;
}
double dnCalculation(double dnX, char chTempOP, double dnY)
{
double dAnswer;
if(chTempOP == ‘+’)
dAnswer = dnX + dnY;
return dAnswer;
if(chTempOP == ‘-’)
dAnswer = dnX – dnY;
return dAnswer;
if(chTempOP == ‘/’)
dAnswer = dnX / dnY;
return dAnswer;
if(chTempOP == ‘*’)
dAnswer = dnX * dnY;
return dAnswer;
}
int main()
{
double dnInput1 = dnUserInput();
char chTempOP1 = chGetOperator();
double dnInput2 = dnUserInput();
double dAnswer = dnCalculation(dnInput1, chTempOP1, dnInput2);
cout << dAnswer << "\n";
double dTest = 5.34332;
cout << dTest;
}
Okay, that was wrong >_> those functions have cin >> dnValue in them and so forth. Needless to say the program works and it runs.
I feel obligated to put mines up cause I find it interesting how every body wrote it differently.
#include
using namespace std;
int userinput(){
double x;
cout <> x;
return x;
} //gathers user input
int cOperate(){ //gather operation input
cout << "Type in your operation please. / * + – :" <> cOperation;
return cOperation;
}
int ifstates (double x, char a, double y){
if (a == ‘/’){ //checks the operator
cout << x / y << endl; // does the math
}
if (a == '+'){
cout << x + y << endl;
}
if (a == '*'){
cout << x * y << endl;
}
if (a == '-'){
cout << x – y << endl;
}
if (a != '-','+','/','*'){
cout << "you did not input one of 4 operators" << endl;
}
return 0;
}
int main(){
double num1 = userinput(); // user input number
char OP = cOperate(); // user input character
double num2 = userinput(); // user input number
ifstates (num1, OP, num2); // checks to see what operator was inputed and does the math
system("pause");
return 0;
system("pause");
}
I feel obligated ass well to put my code here seeing as how everyone has a different way of achieving the same goal. What do you think of my code?
#include “stdafx.h”
#include
#include
using namespace std;
double getUserNumer()
{
cout <> nY;
return nY;
}
double getUserNumer2()
{
cout <> nX;
return nX;
}
char enterSymbol()
{
cout <> chOperation;
return chOperation;
}
int calResult(double nY, double nX, char enterSymbol)
{
if (enterSymbol==’+')
return nY + nX;
if (enterSymbol==’-')
return nY – nX;
if (enterSymbol==’*')
return nY * nX;
if (enterSymbol==’/')
return nY / nX;
else
cout << "You did not input one of the four symbols provided, try again" << endl;
return 0;
}
void printResult(double nResult)
{
cout << "The answer is: " << nResult << endl;
}
int main()
{
double input1 = getUserNumer();
double input2 = getUserNumer2();
char userSymbol = enterSymbol();
double nResult = calResult(input1, input2, userSymbol);
printResult(nResult);
}
This is my code that I used, and I only looked at the solution ’cause I forgot that in if statements you need ” around +, -, /, *.
#include<iostream> #include<string> int main() { using namespace std; cout<<"Enter a number:"; double long x; cin>> x; cout<<"Enter one of these: (*, /, +, -)"; char chOperator; cin>>chOperator; cout<<"Enter another number:"; double long y; cin>> y; if (chOperator== '*' ) cout<<x*y; if(chOperator=='+') cout<<x+y; if (chOperator=='-') cout<<x-y; if(chOperator=='/') cout<<x/y; return 0; }Is this site still alive? I emailed “Alex” with a question but I haven’t received an answer…