C++: Iostream

This entry is part 6 of 61 in the series C++

C++ includes an extensive standard library that provides IO, input and output (and many other facilities).

A stream is a sequence of characters read from or written to an IO device.  For example, inputting form the keyboard and outputting to the console.

The term stream is intended to suggest that the characters are generated, or consumed, sequentially over time.

Using the IO library, we can prompt the user to give us two numbers and then print their sum.

#include <iostream>
using namespace std;

void main()
{

	int v1;
	int v2;

	cout << "Enter two numbers: ";

	cin >> v1 >> v2;
	
	cout << "The sum of " << v1 << " and " << v2
		<< " is " << v1 + v2;

	cout << endl;

}

You can use CTRL-F5 to run the above program.

iostream

 

C++: Printing the ASCII value of a char

This entry is part 7 of 61 in the series C++

Using iostream cout, we can print out the integer value of an ASCII char.

If you check the ascii value of b, it’s decimal value is 98.  This can be shown in the program below where we define an integer b with the char ‘b’.

#include <iostream>
using namespace std;

void main()
{

	int b = 'b';   // note: char implicitly converted to int
	char c = 'c';

	cout << 'a' << endl;
	cout << b << endl;
	cout << c << endl;

}

ascii

C++: The string class

This entry is part 8 of 61 in the series C++

To make it easier to handle string, the standard C++ library provides a string class.

To include the string class, we need to have this line #include <string> at the header of the file.

#include <iostream>
#include <string>
using namespace std;

void main()
{
	string str;

	cout << "Please enter your name\n";
	
	cin >> str;

	cout << "Hello, " << str << "!\n";

}

 

The operator << is an output operator. cout is the standard output stream.  It is used to output a string of characters.

The operator >> (“get from”) is used as an input operator; cin is the standard input stream.

.string

Posted in

C++: A string class and its method

This entry is part 9 of 61 in the series C++

In our previous post, we have mentioned about string as a class.  In our later posts, we will talk more about Object Oriented programming and its core concepts.

The idea about object oriented programming is that you can create objects and define their relevant characteristics.  This could sound a bit abstract when you first started learning OOP.

Since we say string is a class, we should be able to create an object out of it.  Let’s look at this example.

#include <iostream>
#include <string>
using namespace std;

void main()
{
	string str = "Stay calm and keep learning";

	int length = str.size();

	cout << "The length of the string is: " << length;

	cout << endl;  //Output end of line character

}

In this example, we have defined an instance of a class string and this instance hold a string of character.

Making use of the string class and its relevant characteristics, we can find the length of the string str easily uisng str.size().

In later lessons, we will learn that size() is a method of the string class.  And we are using a dot(.) operator to assess its method called size().

 

C++: Flow control with if statement

This entry is part 10 of 61 in the series C++

Like most languages, C++ provides an if statement that supports conditional execution.

We can use an if to check for each condition.  You can actually stack up the many if statements to check for the different conditions.

The example below checks for 2 conditions.

#include <iostream>
#include <string>
using namespace std;

void main()
{
	string singer = "Taylor Swift";
	int count = 6;

	if (singer == "Taylor Swift")
	{
		cout << "I am a fan.";
	}

	if (count == 5)
	{
		cout << "This is her fifth album.";
	}

	cout << endl;

}

For an if statement, if an expression evaluates to true, it will proceed to execute other statements inside the curly braces.

The first expression singer==”Taylor Swift” will evaluate to true and so the line inside the curly braces is printed out.

For the second expression count==5, as we have set count to be 6 earlier, this expression will not evaluate to true.  So it will not execute the cout statement inside the loop.

ifstatement

 

Posted in