I want to do something like below, but the console closes too fast for me to read the error message and I can't break;
completely out of a nested for loop. I just want the program to terminate and give me time to read the error message before it closes.
#include <iostream>
#include <string>
int main()
{
std::string x;
std::cin >> x;
char y;
for (int i = 0; i <= 5; ++i)
{
y = x[i];
for (int j = 0; j <= 5; ++j)
{
if (j < 5)
{
std::cout << "Random text.\n";
}
else if (j >= 5)
{
std::cerr << "ERROR, j cannot be more than 4.";
exit(EXIT_FAILURE);
}
}
}
return 0;
}
I've considered using cin.get();
but it's not guaranteed to work because cin
may still have input from its buffer, and I don't think I can invoke it anyways after exit();
is called. Is there any way for me to have the console wait for me to press a key before terminating so I can read this error message? Or is there a better way for me to output this error message and terminate the program safely?
Note: The above program is just example code to explain what I'm trying to do.