UPDATE
Placing here a robust version of the program that has been posted.
The root cause of the problem (infinite loop) is a conjunction of 2 factors: the unverification of the "Cin >> R0" result, and the use of recursion.
In this situation, when something non-numerical is typed, the program enters an infinite loop.
Keyboard reading is a notoriously difficult task for beginners in both C and C++.
#include <iostream>
#include <string>
using namespace std;
static void pergunta2()
{
cout << "* pergunta2\n";
}
static void pergunta1()
{
int r0;
string resto_da_linha;
// loop de execucao de pergunta1
for (;;) // #1
{
// leitura e consistencia da digitacao
for (;;) // #2
{
cout << "* digite uma opcao: 1 ou 2: " << flush;
if (cin >> r0)
{
// sucesso na leitura de um numero
break;
}
cout << "* erro, digitacao invalida (digitar apenas numeros)\n";
cin.clear(); // como houve erro na leitura, precisa limpar o estado de cin
// cin.ignore(1000, '\n'); // opcao 1 para limpar o buffer de entrada
getline(cin, resto_da_linha); // opcao 2 para limpar o buffer de entrada
} // for #2
// por via das duvidas limpa o buffer de leitura,
// caso o usuario tenha digitado coisas demais
// cin.ignore(1000, '\n');
getline(cin, resto_da_linha);
switch (r0)
{
case 1:
pergunta2();
break;
case 2:
pergunta2();
break;
default:
cout << "\n* erro, opcao invalida\n\n";
break;
} // switch
} // for #1
} // pergunta1
int main()
{
pergunta1();
}
You probably defined the R0 variable as an integer. Set the R0 variable to a character (char) and make sure that what was typed is a valid character for your program, so I understand from your problem either '1' or '2', all others are invalid characters.
– anonimo