I will assume sign
and yn
are of type char*
since you didn't specify it.
What would happen in that case is that the input stream would give one char
to *sign
and keep the remainder of the input for the next access to the stream. So what you need to do is flush it. You can do that by calling ignore
on the stream with std::numeric_limits<std::streamsize>::max()
and '\n'
as arguments, like that:
#include <iostream>
#include <limits> //For std::numeric_limits<std::streamsize>::max()
int main()
{
char* sign = new char;
char* yn = new char;
std::cout << "Please now enter the sign (+-*/): ";
std::cin >> *sign;
std::cout << std::endl; //This std::cout is useless, entering an input in std::cin already returns to a newline
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //Like this
std::cout << "Do you want to try again? (y - yes, anything else - no): ";
std::cin >> *yn;
std::cout << *sign << std::endl << *yn << std::endl;
delete sign;
delete yn;
}
The first argument tells ignore
how many char
to extract from the stream while the second tells it when to stop extracting.
Here is a link to the cppreference page about ignore