Pure Programmer
Blue Matrix


Cluster Map

Console Input

L1

This page is under construction. Please come back later.

ConsoleInput1.cpp
/******************************************************************************
 * This program demonstrates how to prompt the user for input.
 * 
 * Copyright © 2020 Richard Lesh.  All rights reserved.
 *****************************************************************************/

#undef NDEBUG

#include "Utils.hpp"
#include <clocale>
#include <codecvt>
#include <fmt/format.h>
#include <fmt/xchar.h>
#include <iostream>
#include <string>

std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>);
using namespace Utils;
using namespace std;

int main(int argc, char **argv) {
	setlocale(LC_ALL, "en_US.UTF-8");
	wcout.imbue(utf8loc);
	wcin.imbue(utf8loc);

	wstring const NAME = Utils::prompt(L"What is your name? ");
	wstring const FAVORITE_COLOR = Utils::prompt(L"What is your favorite color? ");
	wcout << fmt::format(L"Hello, {0:s}!  I like {1:s} too!", NAME, FAVORITE_COLOR) << endl;
	return 0;
}

Output
$ g++ -std=c++11 ConsoleInput1.cpp -o ConsoleInput1 -lfmt $ ./ConsoleInput1 < ../../examples/ConsoleInput1.in What is your name? Rich What is your favorite color? blue Hello, Rich! I like blue too!
ConsoleInput2.cpp
/******************************************************************************
 * This program demonstrates how to prompt the user for input.
 * 
 * Copyright © 2020 Richard Lesh.  All rights reserved.
 *****************************************************************************/

#undef NDEBUG

#include "Utils.hpp"
#include <clocale>
#include <codecvt>
#include <fmt/format.h>
#include <fmt/xchar.h>
#include <iostream>
#include <string>

std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>);
using namespace Utils;
using namespace std;

int main(int argc, char **argv) {
	setlocale(LC_ALL, "en_US.UTF-8");
	wcout.imbue(utf8loc);
	wcin.imbue(utf8loc);

	int32_t favorite_int = 0;
	int64_t favorite_long = 0;
	double favorite_double = 0.0;
	try {
		wstring const FAVORITE_INT_INPUT = Utils::prompt(L"What is your favorite small integer? ");
		favorite_int = stoi(FAVORITE_INT_INPUT);
		wstring const FAVORITE_LONG_INPUT = Utils::prompt(L"What is your favorite large integer? ");
		favorite_long = stol(FAVORITE_LONG_INPUT);
		wstring const FAVORITE_DOUBLE_INPUT = Utils::prompt(L"What is your favorite floating point? ");
		favorite_double = stod(FAVORITE_DOUBLE_INPUT);
	} catch (invalid_argument ex) {
		wcout << wstring(L"Bad input! ") + Utils::exceptionMessage<wchar_t>(ex) << endl;
	} catch (...) {
		wcout << L"Don't know what went wrong!" << endl;
	}
	double const SUM = favorite_int + favorite_long + favorite_double;
	wcout << fmt::format(L"All together they add up to {0:f}!", SUM) << endl;
	return 0;
}

Output
$ g++ -std=c++11 ConsoleInput2.cpp -o ConsoleInput2 -lfmt $ ./ConsoleInput2 < ../../examples/ConsoleInput2.in1 What is your favorite small integer? 326 What is your favorite large integer? 1000000 What is your favorite floating point? 3.141926 All together they add up to 1000329.141593! $ g++ -std=c++11 ConsoleInput2.cpp -o ConsoleInput2 -lfmt $ ./ConsoleInput2 < ../../examples/ConsoleInput2.in2 What is your favorite small integer? 326 What is your favorite large integer? abc What is your favorite floating point? 3.141926 Bad input! $ g++ -std=c++11 ConsoleInput2.cpp -o ConsoleInput2 -lfmt $ ./ConsoleInput2 < ../../examples/ConsoleInput2.in3 What is your favorite small integer? 326 What is your favorite large integer? 1000000 What is your favorite floating point? abc Bad input!

Questions

Projects

More ★'s indicate higher difficulty level.

References