Video Coming Soon...

Created by Zed A. Shaw Updated 2024-10-28 08:02:26

19: Input From a User

WARNING: This is hot garbage. I'm debating just teaching it to people then pointing them at something but also I can't really think of when I would use this. File input maybe? I may have to write a little file input parser and see if you need any of the cin format systems given that regex exists.

View Source file ex19.cpp Only

#include <iostream>
#include <fmt/core.h>
#include <limits>

using std::cin, std::cout, std::getline, std::string;
using namespace fmt;

int main() {
  std::streamsize eatme = std::numeric_limits<std::streamsize>::max();
  string name;
  println("What's your name?");
  cin >> name;

  int age;

  println("What's your age?");
  cin >> age;

  while(!cin) {
    cin.clear();
    cin.ignore(eatme, '\n');
    println("Wrong, enter a number:");
    cin >> age;
  }

  // eat the newline
  cin.ignore(eatme, '\n');

  string quote;
  println("Tell me something about you?");
  getline(cin, quote);

  println("---\nHello {}, you are {} years old.",
      name, age);

  println("Something you about you:");
  cout << quote;
}
Previous Lesson Next Lesson

Register for Learn C++ the Hard Way

Register to gain access to additional videos which demonstrate each exercise. Videos are priced to cover the cost of hosting.