Tsallaka zuwa abun ciki
Justice Tech School

Reading Lines

getline reads a whole line. Here an istringstream stands in for the keyboard.

Example
#include <iostream>
#include <sstream>
#include <string>

int main() {
  std::istringstream in("Ada Lovelace\n42");
  std::string name; int age;
  std::getline(in, name);
  in >> age;
  std::cout << name << " is " << age << "\n";
  return 0;
}

Result

Ada Lovelace is 42

Exercise

Read a line.

std::(in, s)

Quiz Test

Why getline?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.