Quisiera que alguien diga como tengo que hacer para que cada vez que cometo un error o acierto una letra se vaya limpiando la pantalla con system("cls") de manera correcta y se vaya como actualizando el juego del ahorcado y no mostrando todo secuencialmente como lo hace mi programa ;-)
#include <iostream>
#include <string.h>
#include <time.h>
using namespace std;
void inicio();
void primer_error();
void segundo_error();
void tercer_error();
void cuarto_error();
void quinto_error();
void jugar();
void rellenar(string n);
void intentos(int intento);
int main(){
int opcion;
srand(time(NULL));
do{
cout << "1. JUGAR" << endl;
cout << "2. SALIR" << endl;
cin >> opcion;
switch(opcion){
case 1: jugar(); break;
case 2: cout << "HASTA LUEGO" << endl; break;
default: cout << "OPCION INCORRECTA, Intente de nuevo" << endl;
}
}while(opcion!=2);
system("pause");
return 0;
}
void inicio()
{
cout << "**********" << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
}
void primer_error()
{
cout << "**********" << endl;
cout << "* | " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
}
void segundo_error()
{
cout << "**********" << endl;
cout << "* | " << endl;
cout << "* O " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
}
void tercer_error()
{
cout << "**********" << endl;
cout << "* | " << endl;
cout << "* O " << endl;
cout << "* /|\\" << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
}
void cuarto_error()
{
cout << "**********" << endl;
cout << "* | " << endl;
cout << "* O " << endl;
cout << "* /|\\" << endl;
cout << "* A " << endl;
cout << "* / \\" << endl;
cout << "* " << endl;
}
void quinto_error()
{
cout << "********** " << endl;
cout << "* | " << endl;
cout << "* O " << endl;
cout << "* /|\\ " << endl;
cout << "* A " << endl;
cout << "* / \\ " << endl;
cout << "* " << endl;
cout << " PERDISTE" << endl;
}
void jugar()
{
int aleatorio;
string a="OSO", b="PAYASO", c="CUADERNO";
aleatorio=rand()%3+1;
switch(aleatorio){
case 1: rellenar(a); break;
case 2: rellenar(b); break;
case 3: rellenar(c); break;
}
}
void rellenar(string n)
{
int contador=0, aux=0, aux2, MAX=n.length();
char letra, contenedor[MAX];
for(int i=0;i<MAX;i++){
contenedor[i]='_';
}
inicio();
while(contador<5){
aux2=0;
for(int j=0;j<MAX;j++){
cout << contenedor[j] << " ";
}
cout << endl;
cout << "Ingrese letra: ";
cin >> letra;
for(int k=0;k<MAX;k++){
if(letra==n[k] && contenedor[k]=='_'){
contenedor[k]=letra;
aux++;
break;
}
else{
aux2++;
}
}
if(aux==MAX){
cout << "GANASTE! La palabra oculta es: " << n << endl;
break;
}
if(aux2==MAX){
contador++;
intentos(contador);
}
}
}
void intentos(int intento)
{
switch(intento){
case 1: primer_error(); break;
case 2: segundo_error(); break;
case 3: tercer_error(); break;
case 4: cuarto_error(); break;
case 5: quinto_error(); break;
}
}
El problema es que te faltaban librerías <windows.h> y <stdlib.h> la primera para poder usar el "cls"y la otra para los números aleatorio y use void gotoxy(int x, int y) para otorgar coordenadas a la parte donde ingresas las letras y asi poder sobre escribir en la misma posición y se viera como si se borrara y use los system("cls") para borrar el monito de palo mostrar el siguiente.
No se si me explique bien pero igual te dejo el código "arreglado"
#include <windows.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <time.h>
using namespace std;
void inicio();
void primer_error();
void segundo_error();
void tercer_error();
void cuarto_error();
void quinto_error();
void jugar();
void rellenar(string n);
void intentos(int intento);
void gotoxy(int x,int y){
HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hcon,pos);
}
int main(){
int opcion;
srand(time(NULL));
do{
cout << "1. JUGAR" << endl;
cout << "2. SALIR" << endl;
cin >> opcion;
system("cls");
switch(opcion){
case 1: jugar(); break;
case 2: cout << "HASTA LUEGO" << endl; break;
default: cout << "OPCION INCORRECTA, Intente de nuevo" << endl;
}
}while(opcion!=2);
system("pause");
return 0;
}
void inicio()
{
cout << "**********" << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
}
void primer_error()
{
system("cls");
cout << "**********" << endl;
cout << "* | " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
}
void segundo_error()
{
system("cls");
cout << "**********" << endl;
cout << "* | " << endl;
cout << "* O " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
}
void tercer_error()
{
system("cls");
cout << "**********" << endl;
cout << "* | " << endl;
cout << "* O " << endl;
cout << "* /|\\" << endl;
cout << "* " << endl;
cout << "* " << endl;
cout << "* " << endl;
}
void cuarto_error()
{
system("cls");
cout << "**********" << endl;
cout << "* | " << endl;
cout << "* O " << endl;
cout << "* /|\\" << endl;
cout << "* A " << endl;
cout << "* / \\" << endl;
cout << "* " << endl;
}
void quinto_error()
{
system("cls");
cout << "********** " << endl;
cout << "* | " << endl;
cout << "* O " << endl;
cout << "* /|\\ " << endl;
cout << "* A " << endl;
cout << "* / \\ " << endl;
cout << "* " << endl;
cout << " PERDISTE" << endl;
}
void jugar()
{
int aleatorio;
string a="OSO", b="PAYASO", c="CUADERNO";
aleatorio=rand()%3+1;
switch(aleatorio){
case 1: rellenar(a); break;
case 2: rellenar(b); break;
case 3: rellenar(c); break;
}
}
void rellenar(string n)
{
int contador=0, aux=0, aux2, MAX=n.length();
char letra, contenedor[MAX];
for(int i=0;i<MAX;i++){
contenedor[i]='_';
}
inicio();
while(contador<5){
aux2=0;
for(int j=0;j<MAX;j++){
cout << contenedor[j] << " ";
}
gotoxy(0,8);
cout << endl;
cout << "Ingrese letra: ";
cin >> letra;
for(int k=0;k<MAX;k++){
if(letra==n[k] && contenedor[k]=='_'){
contenedor[k]=letra;
aux++;
break;
}
else{
aux2++;
}
}
if(aux==MAX){
cout << "GANASTE! La palabra oculta es: " << n << endl;
break;
}
if(aux2==MAX){
contador++;
intentos(contador);
}
}
}
void intentos(int intento)
{
switch(intento){
case 1: primer_error(); break;
case 2: segundo_error(); break;
case 3: tercer_error(); break;
case 4: cuarto_error(); break;
case 5: quinto_error(); break;
}
}
Muchas gracias @PiernaDeFelipeCamiroaga, era lo que me faltaba ;-)
#include<stdio.h>
main(){
printf("Hola?");}
// probando publican código xd