Efectivamente, es en la consola,
os comento así por lo alto, se trata de un sistema de ayuda por consola que muestra varios bucles.
En el enunciado especifica que la variable privada de la clase sea de tipo entero.
He probado a pasar un char en vez de un int, pero sigue tragándose los caracteres no numéricos.
Voy a poner el código que llevo hasta ahora, porque ya tengo un cacao que no veas, a ver si vosotros veis el fallo.
helpclass.h
#ifndef __HELPCLASS_H__
#define __HELPCLASS_H__
class Help {
private:
int _option;
public:
Help();
int getoption();
void setoption(int option);
void showmenu();
bool helpon(char option);
bool isvalid(char option);
};
#endif
helpclass.cpp
#include "helpclass.h"
#include <iostream>
#include <cstdlib> //Para system()
#include <cstdio>
using namespace std;
Help::Help() {
setoption(1);
}
int Help::getoption() {
return _option;
}
void Help::setoption(int option) {
_option = option;
}
void Help::showmenu() {
cout << endl << "Sistema de ayuda en modo texto en la consola" << endl << endl;
cout << "0.- Se mostrara la ultima ayuda" << endl;
cout << "1.- if" << endl;
cout << "2.- switch" << endl;
cout << "3.- for" << endl;
cout << "4.- while" << endl;
cout << "5.- do-while" << endl;
cout << "6.- Salir del programa" << endl << endl;
}
bool Help::helpon(char option) {
setoption(option);
switch(option) {
case '0': option = getoption();
break;
case '1': cout << endl << "Bucle if" << endl << endl;
cout << " if(condition)" << endl;
cout << " {" << endl;
cout << " statements;" << endl;
cout << " }" << endl << endl << endl;
break;
case '2': cout << endl << "Bucle switch" << endl << endl;
cout << " switch(expression)" << endl;
cout << " {" << endl;
cout << " case constant-expression:" << endl;
cout << " statements;" << endl;
cout << " break; //optional" << endl;
cout << " case constant-expression:" << endl;
cout << " statements;" << endl;
cout << " break; //optional" << endl << endl;
cout << " // you can have any number of case statements." << endl;
cout << " default : //Optional" << endl;
cout << " statements;" << endl;
cout << " }" << endl << endl;
break;
case '3': cout << endl << "Bucle for" << endl << endl;
cout << " for(init; condition; increment)" << endl;
cout << " {" << endl;
cout << " statements;" << endl;
cout << " }" << endl << endl;
break;
case '4': cout << endl << "Bucle while" << endl << endl;
cout << " while(condition)" << endl;
cout << " {" << endl;
cout << " statements;" << endl;
cout << " }" << endl << endl;
break;
case '5': cout << endl << "Bucle do-while" << endl << endl;
cout << " do" << endl;
cout << " {" << endl;
cout << " statements;" << endl;
cout << " } while(contidion);" << endl << endl;
break;
default: return false;
}
return true;
}
/*
bool Help::isvalid(int option) {
if(option < 0 || option > 6) {
return false;
}
//No haría falta el else, porque si se cumple bucle if, sale de la funcion
//devolviendo false, si no se cumple, no entra y al final devuelve true;
return true;
}*/
bool Help::isvalid(char option) {
if(option >= '0' || option <= '6') {
cout << "TRUE" << endl;
getchar();
getchar();
return true;
}
//No haría falta el else, porque si se cumple bucle if, sale de la funcion
//devolviendo false, si no se cumple, no entra y al final devuelve true;
else {
cout << "FALSE" << endl;
getchar();
getchar();
return false;
}
}
y el helpmain.cpp
#include "helpclass.h"
#include <iostream>
#include <cstdlib> //Para system()
#include <cstdio> //Para getchar();
using namespace std;
int main() {
Help h;
char option;
bool continuar = true;
do {
system("clear");
option = h.getoption();
cout << option << endl; //borrar
h.showmenu();
cout << "Introduce opcion: ";
cin >> option;
if(h.isvalid(option)) {
system("clear");
continuar = h.helpon(option);
cout << "Pulsa ENTER para continuar ...";
getchar();
getchar();
}
else {
cout << "Por favor, introduzca valor valido" << endl;
}
} while(continuar);
return 0;
}
Gracias y un saludo.