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.
 *****************************************************************************/

#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 std;

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

	wstring name;
	name = Utils::prompt(L"What is your name? ");
	wstring favoriteColor;
	favoriteColor = Utils::prompt(L"What is your favorite color? ");
	wcout << fmt::format(L"Hello, {0:s}!  I like {1:s} too!", name, favoriteColor) << 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.
 *****************************************************************************/

#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 std;

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

	try {
		int favoriteInt;
		long favoriteLong;
		double favoriteDouble;
		wstring favoriteIntInput;
		favoriteIntInput = Utils::prompt(L"What is your favorite small integer? ");
		favoriteInt = stoi(favoriteIntInput);
		wstring favoriteLongInput;
		favoriteLongInput = Utils::prompt(L"What is your favorite large integer? ");
		favoriteLong = stol(favoriteLongInput);
		wstring favoriteDoubleInput;
		favoriteDoubleInput = Utils::prompt(L"What is your favorite floating point? ");
		favoriteDouble = stod(favoriteDoubleInput);
		double const sum = favoriteInt + favoriteLong + favoriteDouble;
		wcout << fmt::format(L"All together they add up to {0:f}!", sum) << endl;
	} catch (invalid_argument ex) {
		wcout << L"Bad input! " + Utils::exceptionMessage<wchar_t>(ex) << endl;
	} catch (exception ex) {
		wcout << L"Don't know what went wrong!" << 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