Hola a todos. Hace mucho que no hago alguna aportación por aquí, y por cutre que sea, voy a poner un selector de opciones, en el que no hay que elegir el número de la opción, sinó hay que mover el marcador con W y S. Bueno, aquí va:
#include <iostream>
#include <vector>
#include <conio.h>
using namespace std;
class options{
vector<string> _options;
string _title;
void coutTitle()const{
if(_title.size()==0) return;
vector<string> v;
string t = _title;
while(t.size()>80){
v.push_back(t.substr(0,80));
t.erase(0,80);
}
v.push_back(t);
while(v.size()>1){
cout << v[0];
v.erase(v.begin());
}
cout << string((80-v[0].size())/2,' ') << v[0] << endl << endl;
}
public:
options(){}
options(string title):_title(title){}
options(const options& o):_options(o.getOptions()),_title(o.getTitle()){}
void setTitle(string title){
_title = title;
}
string getTitle()const{
return _title;
}
vector<string> getOptions()const{
return _options;
}
string getOption(size_t i)const{
if(i<_options.size()) return _options[i];
return "";
}
void addOption(string option){
_options.push_back(option);
}
void addOptions(const vector<string>& v){
for(int i=0; i<v.size(); i++)
_options.push_back(v[i]);
}
void setOptions(const vector<string>& v){
_options = v;
}
int show()const{
int index = 0;
char c = 0;
while(c!='\r' && c!='\n'){
system("cls");
coutTitle();
for(int i=0; i<_options.size(); i++){
cout << '[' << (i==index?'X':' ') << "] " << _options[i] << endl;
}
c = getch();
if((c=='w' || c=='W') && index>0)
--index;
if((c=='s' || c=='S') && index<_options.size()-1)
++index;
}
return index;
}
};
int main(){
options op;
op.setTitle("Que querrias ser en otra vida?");
op.addOption("Un Dios.");
op.addOption("Un gobernador.");
op.addOption("Un pensador.");
op.addOption("Un trabajador.");
op.addOption("Un esclavo.");
cout << endl << endl << op.show();
}
Se le pone un titulo, se le agregan las opciones, y se invoca
.show(). Show retorna el índice de la opcion elegida.
Utilizo la función system("cls") y getch(). Quién las quiera cambiar por otras, es libre de hacerlo, este código no tiene copyright xD
Usé getch() de conio.h porque es la más rápida y sencilla.
Jeje esto lo hice hace unos 4 años, pero lo hice a lo cutre con 2 solas opciones (y me costó). Ahora, años después, he visto lo terriblemente sencilo que es jaja.
"Una vez más, la experiencia marca la aptitud."
Saludos, espero que a alguien le sea útil :p