From C to C++
Input/Output, Strings & References
Read and write with cin and cout, use the std::string class, format output with iomanip, and meet references — C++'s safer alias for a variable.
cin: reading input
cin (character input) reads values with the extraction operator >>. It works out the type from the variable — no format specifiers and no &:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
int age;
double cgpa;
cout << "Name, age and CGPA: ";
cin >> name >> age >> cgpa;
cout << name << " is " << age << " with a CGPA of " << cgpa << endl;
return 0;
}Name, age and CGPA: Ravi 19 8.6
Ravi is 19 with a CGPA of 8.6Like scanf("%s"), cin >> name stops at whitespace. To read a whole line, use getline:
#include <iostream>
#include <string>
using namespace std;
int main() {
int roll;
string fullName;
cout << "Roll number: ";
cin >> roll;
cin.ignore(); // skip the newline left after the number
cout << "Full name: ";
getline(cin, fullName);
cout << "#" << roll << ": " << fullName << " (" << fullName.length() << " characters)" << endl;
return 0;
}Roll number: 21 Full name: Ravi Kumar Sharma #21: Ravi Kumar Sharma (17 characters)
The string class
std::string grows automatically, knows its own length, and supports +, == and < directly:
#include <iostream>
#include <string>
using namespace std;
int main() {
string a = "Data", b = "Structures";
string full = a + " " + b; // concatenation
cout << full << " has " << full.size() << " characters" << endl;
cout << "First letter: " << full[0] << ", last: " << full.back() << endl;
cout << "Substring: " << full.substr(5, 6) << endl; // from index 5, length 6
cout << "\"Struct\" found at " << full.find("Struct") << endl;
full.replace(0, 4, "Algo");
cout << "After replace: " << full << endl;
cout << boolalpha << ("apple" < string("banana")) << endl; // dictionary order
for (char c : string("abc")) cout << char(c - 32); // range-based for
cout << endl;
return 0;
}Data Structures has 15 characters First letter: D, last: s Substring: Struct "Struct" found at 5 After replace: Algo Structures true ABC
Formatting with iomanip
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double pi = 3.14159265;
cout << fixed << setprecision(2) << pi << endl; // 2 decimal places
cout << setw(8) << 42 << "|" << endl; // width 8, right-aligned
cout << left << setw(8) << 42 << "|" << endl; // left-aligned
cout << setfill('0') << right << setw(5) << 7 << endl; // pad with zeros
cout << hex << 255 << " " << oct << 8 << dec << endl; // number bases
return 0;
}3.14
42|
42 |
00007
ff 10References
A reference is another name (an alias) for an existing variable. It must be initialised when declared and can never be re-seated to refer to something else.
#include <iostream>
using namespace std;
int main() {
int marks = 70;
int &r = marks; // r is another name for marks
r += 15;
cout << "marks = " << marks << ", r = " << r << endl;
cout << "same address? " << (&marks == &r ? "yes" : "no") << endl;
return 0;
}marks = 85, r = 85 same address? yes
The real power of references is in function parameters — the subject of the next lesson.
| Pointer | Reference |
|---|---|
can be nullptr |
always refers to a valid object |
| can be re-pointed later | bound once, forever |
needs * to read, & to take an address |
used like the variable itself |
| can do arithmetic | no arithmetic |
| has its own memory | usually just an alias |
bool and auto
C++ has a built-in bool type with true and false. And auto lets the compiler deduce a variable's type from its initialiser:
#include <iostream>
#include <vector>
using namespace std;
int main() {
bool passed = true;
auto count = 42; // int
auto ratio = 0.75; // double
auto name = string("Asha"); // std::string
cout << boolalpha << passed << " " << count << " " << ratio << " " << name << endl;
vector<int> v = {3, 1, 4};
for (auto x : v) cout << x * 10 << " ";
cout << endl;
return 0;
}true 42 0.75 Asha 30 10 40
Test yourself
4 questions · pick an answer to check it instantly
- 1
Which reads a full line, including spaces, into
string s?>>stops at whitespace;getlinereads up to the newline. - 2
What is printed by
string s = "hello"; cout << s.size();?size()(orlength()) counts characters; there is no separate'\0'to count. - 3
Which statement about references is true?
A reference is a permanent alias bound at initialisation. - 4
What does
cout << fixed << setprecision(1) << 2.46;print?fixedwith precision 1 rounds to one decimal place.