Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views9 pages

C++ Lec8-Strings+Math

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

1

C++ User Input Strings

User Input Strings

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

// Your name is: John

Programming C++ Samir Bilal Practical & Theoretical


2

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;
}

Programming C++ Samir Bilal Practical & Theoretical


3

C++ String Namespace

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;
}

It is up to you if you want to include the standard namespace library or not.

Programming C++ Samir Bilal Practical & Theoretical


4

C++ Math

C++ has many functions that allow you to perform mathematical tasks on numbers.

Max and Min

The max(x,y) function can be used to find the highest value of x and y:

Example

#include <iostream>

using namespace std;

int main() {

cout << max(5, 10);

return 0;

And the min(x,y) function can be used to find the lowest value of x and y:

Example

#include <iostream>

using namespace std;

int main() {

cout << min(5, 10);

return 0;

}
Programming C++ Samir Bilal Practical & Theoretical
5

C++ <cmath> Header

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;
}

Other Math Functions

A list of other popular Math functions (from the <cmath> library) can be found in the table
below:

Function Description

abs(x) Returns the absolute value of x

cos(x) Returns the cosine of x, in radians

pow(x, y) Returns the value of x to the power of y

sin(x) Returns the sine of x (x is in radians)

tan(x) Returns the tangent of an angle

Programming C++ Samir Bilal Practical & Theoretical


6

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.

Programming C++ Samir Bilal Practical & Theoretical


7

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>

using namespace std;

int main() {

int x = 10;

int y = 9;

cout << (x > y);

return 0;

Or even easier:

Programming C++ Samir Bilal Practical & Theoretical


8

Example

#include <iostream>

using namespace std;

int main() {

cout << (10 > 9);

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

cout << (10 == 15); // returns 0 (false), because 10 is not equal to 15

Programming C++ Samir Bilal Practical & Theoretical


9

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)

2) Convert the Mathematical relationship to Mathematical expression in C++


Language:

Programming C++ Samir Bilal Practical & Theoretical

You might also like