Reputation: 43
I want to do something like this
// Example program
#include <iostream>
/*
//IF
//ASKED FOR INPUT :
//12
*/
int main()
{
int myInt;
std::cin >> myInt;
std::cout << myInt;
}
I want this snippet to print 12
I want the code to do something like what the commented part states.
I know I can use standard output and just type it in there. But, my IDE dosen't allow that and I don't want to read it from a file either. Any suggestions?
Before, I thought I can use #define
to redefine the purpose of cin
and read from the top of the file instead. But, I'm not sure if it would work or how to implement it either.
Upvotes: 0
Views: 252
Reputation: 104549
You could have two different builds based on a #define
value. Use stringstream as an input:
#include <iostream>
#include <sstream>
#define USING_FAKE_STREAM
#ifdef USING_FAKE_STREAM
std::stringstream g_ss;
void initStream()
{
g_ss << "12\n";
}
std::istream& getInputStream()
{
return g_ss;
}
#else
void initStream()
{
}
std::istream& getInputStream()
{
return std::cin;
}
#endif
int main()
{
initStream();
auto& inputStream = getInputStream();
inputStream >> myInt;
std::cout << myInt;
return 0;
}
Upvotes: 0