Have you ever been in a situation where you write lines of cout<< and
cin>> statements to get input from the user and display the output on the
console? If yes, then this library is for you. It provides a simple and powerful
interface to get input from the user and display the output on the console.
#include"iostream"
#include<string>usingnamespacestd;intmain(){
cout<<"Enter a number: ";
int num;
cin>>num;
cout << "You entered: " << num << endl;
cout<<"Enter a string: ";
string str;
cin>>str;
cout << "You entered: " << str << endl;
return0;
}There should be a better way to do this...
Download the source code from this repository and include the cppio.hpp file
in your environment. Then include it in your .cpp file as follows:
main.cpp
#include"cppio.hpp"main.cpp
Finally, use the namespace cppIO to access the library functions.
#include"cppio.hpp"usingnamespacecppIO;intmain(){
...
}Takes a string as a parameter which is displayed on the console to ask the user for input. Also, you have to specify the type that the function will return, before the function call parenthesis. It returns the input entered by the user. An example of this function is:
#include"cppio.hpp"
#include<string>usingnamespacestd;usingnamespacecppIO;intmain(){
int num = input<int>("Enter an integer: ");
cout << "You entered: " << num << endl;
//You don't even need to declare the type of the variableauto str = input<string>("Enter a string: ");
cout << "You entered: " << str << endl;
return0;
} //You don't need the cout statements as well, but we are not there yet :) Takes a string as a parameter as well, plus it takes a second parameter which is the variable to which the input will be stored. This function does not return anything. An example of this function is:
#include"cppio.hpp"
#include<string>usingnamespacestd;usingnamespacecppIO;//The input that the user enters will be stored in the variable that is passed to the function.intmain(){
int age;
input("Enter your age: ",age);
cout<<age;
} 👉 You can use whichever variation you want. They both do the same thing however the second variation is more efficient because of its implementation details.
This function displays the output on the console. It takes a variable to be displayed on the console. It displays the output on the console and returns nothing. An example of this function is:
#include"cppio.hpp"
#include<string>usingnamespacestd;usingnamespacecppIO;intmain(){
int num = input<int>("Enter an integer: ");
log(num);
auto str = input<string>("Enter a string: ");
log(str)
return0;
} 👉 log() function supports output multiple variables
#include"cppio.hpp"
#include<string>usingnamespacestd;usingnamespacecppIO;intmain(){
auto name = input<string>("Enter your name: ");
auto surname = input<string>("Enter your surname: ");
log("Hello, ",name, surname); //Hello, John Foo/*Without log function you would have to write: cout << "Hello, " << name << " " << surname << endl;*/return0;
} 👉 log() function supports arrays, vectors, lists, queues, stacks and maps.
For arrays, you have to pass the array's length as second argument You can pass the length manually or use the len() function provided by the library
#include"cppio.hpp"usingnamespacestd;usingnamespacecppIO;intmain(){
int array[] = {1,2,3,4,5};
log(array,5); //Output: [1,2,3,4,5]log(array,len(array)); //Output: [1,2,3,4,5]log(array,sizeof(array)/sizeof(array[0])); //Output: [1,2,3,4,5]return0;
} #include"cppio.hpp"
#include<vector>usingnamespacestd;usingnamespacecppIO;intmain(){
//define a vector
vector<int> vec;
//push 1,2,3,4,5 into the vectorfor(int i = 1; i <= 5; i++)
vec.push_back(i);
//log the vectorlog(vec); //Output: {1,2,3,4,5}return0; } #include"cppio.hpp"
#include<list>usingnamespacestd;usingnamespacecppIO;intmain(){
//define a list
list<int> list;
//push 1,2,3,4,5 into the listfor(int i = 1; i <= 5; i++)
list.push_back(i);
//log the listlog(list); //Output: {1,2,3,4,5}return0;
} #include"cppio.hpp"
#include<queue>usingnamespacestd;usingnamespacecppIO;intmain(){
//define a queue
queue<int> queue;
//push 1,2,3,4,5 into the queuefor(int i = 1; i <= 5; i++)
queue.push(i);
//log the queuelog(queue); //Output: {1,2,3,4,5}return0; } #include"cppio.hpp"
#include<stack>usingnamespacestd;usingnamespacecppIO;intmain(){
//define a stack
stack<string> stack;
//push "A","B","C" into the stack
stack.push("library!");
stack.push("amazing");
stack.push("What an");
//log the stacklog(stack); //Output: {What,an,amazing,library!}return0; } #include"cppio.hpp"
#include<map>usingnamespacestd;usingnamespacecppIO;intmain(){
//define a map
map<string, int> map;
//insert some key-value pairs into the map
map.insert({"one",1});
map.insert({"two",2});
map.insert({"three",3});
//log the maplog(map); //Output: {one:1,two:2,three:3}int my_value = map["one"]; //my_value = 1//define a hash_maplog(my_value); //Output: 1 return0;
} It stands for c++ input/output.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.
1.1.9
I'm not a C++ guru. This library only aims to provide a simple and powerful interface to get
input from the user and display the output on the console. For these reasons I built this library
on top of the iostream library. I'm aware of there are some more performant ways of what I did
via dealing with more low-level details. If you have any suggestions or improvements, please feel
free to open an issue or a pull request. I'll be happy to learn from you.
First release on 28/12/2022 by Utku Onur Şahin