C++ Lec8-Strings+Math
C++ Lec8-Strings+Math
C++ Lec8-Strings+Math
It is possible to use the extraction operator >> on cin to display a string entered by a user:
Example
string firstName;
cout << "Type your first name: ";
cin >> firstName; // get user input from the keyboard
cout << "Your name is: " << firstName;
// Type your first name: John
// Your name is: John
However, cin considers a space (whitespace, tabs, etc) as a terminating character, which means
that it can only display a single word (even if you type many words):
Example
string fullName;
cout << "Type your full name: ";
cin >> fullName;
cout << "Your name is: " << fullName; // Type your full name: John Doe
From the example above, you would expect the program to print "John Doe", but it only prints
"John".
That's why, when working with strings, we often use the getline() function to read a line of text.
It takes cin as the first parameter, and the string variable as second:
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName; // Type your full name: John Doe
// Your name is: John Doe
return 0;
}
Omitting Namespace
You might see some C++ programs that runs without the standard namespace library. The using
namespace std line can be omitted and replaced with the std keyword, followed by the ::
operator for string (and cout) objects:
Example
#include <iostream>
#include <string>
int main() {
std::string greeting = "Hello";
std::cout << greeting;
return 0;
}
C++ Math
C++ has many functions that allow you to perform mathematical tasks on numbers.
The max(x,y) function can be used to find the highest value of x and y:
Example
#include <iostream>
int main() {
return 0;
And the min(x,y) function can be used to find the lowest value of x and y:
Example
#include <iostream>
int main() {
return 0;
}
Programming C++ Samir Bilal Practical & Theoretical
5
Other functions, such as sqrt (square root), round (rounds a number) and log (natural logarithm),
can be found in the <cmath> header file:
Example
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << sqrt(64) << "\n";
cout << round(2.6) << "\n";
cout << log(2) << "\n";
return 0;
}
A list of other popular Math functions (from the <cmath> library) can be found in the table
below:
Function Description
C++ Booleans
Very often, in programming, you will need a data type that can only have one of two values, like:
YES / NO
ON / OFF
TRUE / FALSE
For this, C++ has a bool data type, which can take the values true (1) or false (0).
Boolean Values
A boolean variable is declared with the bool keyword and can only take the values true or false:
Example
bool x = true;
bool y = false;
cout << x; // Outputs 1 (true)
cout << y; // Outputs 0 (false)
From the example above, you can read that a true value returns 1, and false returns 0.
Boolean Expression
A Boolean expression is a C++ expression that returns a boolean value: 1 (true) or 0 (false).
You can use a comparison operator, such as the greater than (>) operator to find out if an
expression (or a variable) is true:
Example
int x = 10;
int y = 9;
cout << (x > y); // returns 1 (true), because 10 is higher than 9
Example
#include <iostream>
int main() {
int x = 10;
int y = 9;
return 0;
Or even easier:
Example
#include <iostream>
int main() {
return 0;
In the examples below, we use the equal to (==) operator to evaluate an expression:
Example
int x = 10;
cout << (x == 10); // returns 1 (true), because the value of x is equal to 10
Example
Some Exercises:
1- If the value of x=3, Write a C++ program to find and print the following:
1- x
2- x2 (the square of x)
3- x3 (the cube of x)
4- x25
5- x
6- Log(x)