Foros del Web » Programación para mayores de 30 ;) » C/C++ »

problema con getch

Estas en el tema de problema con getch en el foro de C/C++ en Foros del Web. ¿Se puede usar getch en Dev-C++? por lo menos a mi no me funciona(me baje conio.h) O tambien me serviria alguna funcion que al pulsar ...
  #1 (permalink)  
Antiguo 06/06/2008, 16:02
 
Fecha de Ingreso: mayo-2008
Ubicación: Chile
Mensajes: 189
Antigüedad: 16 años
Puntos: 3
problema con getch

¿Se puede usar getch en Dev-C++? por lo menos a mi no me funciona(me baje conio.h)
O tambien me serviria alguna funcion que al pulsar una tecla se introduzca sin nesecidad de enter.
  #2 (permalink)  
Antiguo 06/06/2008, 20:23
Avatar de hgp147  
Fecha de Ingreso: diciembre-2006
Ubicación: Buenos Aires, Argentina
Mensajes: 980
Antigüedad: 17 años, 4 meses
Puntos: 36
Respuesta: problema con getch

Sí, se puede usar.

Código:
#include<stdio.h>
#include<conio.h>

int main(){

getch();

}
Sino trata con system("pause"),

Código:
#include <iostream>

int main(){

system ("pause");

}
__________________
Spread Firefox | Download Day 2008
¡Únete en nuestra misión para alcanzar el Record Guinness al software más descargado en 24 horas! http://www.spreadfirefox.com/es-ES/worldrecord/

Última edición por hgp147; 07/06/2008 a las 17:22
  #3 (permalink)  
Antiguo 07/06/2008, 09:48
 
Fecha de Ingreso: mayo-2008
Ubicación: Chile
Mensajes: 189
Antigüedad: 16 años
Puntos: 3
Respuesta: problema con getch

No me funciona, mira el codigo:

Cita:
#include <iostream>
#include <stdlib.h>
#include <conio.h>

using namespace std;

void sumar();
void restar();
void multiplicar();
void dividir();

char x;
long a, b, total;
float c, d, totald;

void sumar() {
cout << "Introduce un numero para sumar.\n";
cin >> a;
cout << "Ahora introduce el otro numero para sumar.\n";
cin >> b;
cout << "La suma de "<< a <<" y "<< b <<" es "<< a + b <<"\n";
}

void restar() {
cout << "Introduce un numero para ser restado.\n";
cin >> a;
cout << "Ahora introduce el numero que restara a "<< a <<".\n";
cin >> b;
cout << ""<< a <<" disminuido en "<< b <<" es "<< a - b <<".\n";
}

void multiplicar() {
cout << "Introduce un numero para multiplicarlo.\n";
cin >> a;
cout << "Ahora introduce el otro numero para multiplicarlo con "<< a <<".\n";
cin >> b;
cout << ""<< a <<" por "<< b <<" es "<< a * b <<"\n";
}

void dividir() {
cout << "Introduce un numero para dividirlo.\n";
cin >> c;
cout << "Ahora introduce el numero que dividira a "<< c <<".\n";
cin >> d;
totald = (float) c / d;
printf ("%.1f dividido en %.1f es %.3f \n" ,c,d, totald);
}

void bucleMain() {

cout << "\n\nIntroduce '+' o 's' si quieres sumar\n";
cout << "Introduce '-' o 'r' si quieres restar\n";
cout << "Introduce '*' o 'm' si quieres multplicar\n";
cout << "Introduce '/' o 'd' si quieres dividir\n";
cout << "Si oprimes otra tecla me cerrare\n";

getch(x);
switch (x) {
case 'S': case 's': case '+':
sumar();
break;
case 'R' : case 'r': case '-':
restar();
break;
case 'M' : case 'm': case '*':
multiplicar();
break;
case 'D' : case 'd': case '/':
dividir();
break;
case '\n' : case ' ':
break;

default : exit(0);
break;
}

}
main()
{
cout << "Esta es la Calculadora 1.0 \n";
do { bucleMain(); }
while(1);
system("PAUSE");
return 0;
}
Y aqui esta mi conio:
Cita:
/*
* conio.h
* This file has no copyright assigned and is placed in the Public Domain.
* This file is a part of the mingw-runtime package.
* No warranty is given; refer to the file DISCLAIMER within the package.
*
* Low level console I/O functions. Pretty please try to use the ANSI
* standard ones if you are writing new code.
*
*/

#ifndef _CONIO_H_
#define _CONIO_H_

/* All the headers include this file. */
#include <_mingw.h>

#ifndef RC_INVOKED

#ifdef __cplusplus
extern "C" {
#endif

_CRTIMP char* __cdecl _cgets (char*);
_CRTIMP int __cdecl _cprintf (const char*, ...);
_CRTIMP int __cdecl _cputs (const char*);
_CRTIMP int __cdecl _cscanf (char*, ...);

_CRTIMP int __cdecl _getch (void);
_CRTIMP int __cdecl _getche (void);
_CRTIMP int __cdecl _kbhit (void);
_CRTIMP int __cdecl _putch (int);
_CRTIMP int __cdecl _ungetch (int);


#ifndef _NO_OLDNAMES

_CRTIMP int __cdecl getch (void);
_CRTIMP int __cdecl getche (void);
_CRTIMP int __cdecl kbhit (void);
_CRTIMP int __cdecl putch (int);
_CRTIMP int __cdecl ungetch (int);

#endif /* Not _NO_OLDNAMES */


#ifdef __cplusplus
}
#endif

#endif /* Not RC_INVOKED */

#endif /* Not _CONIO_H_ */
  #4 (permalink)  
Antiguo 07/06/2008, 19:03
Avatar de hgp147  
Fecha de Ingreso: diciembre-2006
Ubicación: Buenos Aires, Argentina
Mensajes: 980
Antigüedad: 17 años, 4 meses
Puntos: 36
Respuesta: problema con getch

A mi me anda:

Código:
#include <iostream>
#include <stdlib.h>

using namespace std;

void sumar();
void restar();
void multiplicar();
void dividir();

char x;
long a, b, total;

void sumar() {
cout << "Introduce un numero para sumar.\n";
cin >> a;
cout << "Ahora introduce el otro numero para sumar.\n";
cin >> b;
cout << "La suma de "<< a <<" y "<< b <<" es "<< a + b <<"\n";
}

void restar() {
cout << "Introduce un numero para ser restado.\n";
cin >> a;
cout << "Ahora introduce el numero que restara a "<< a <<".\n";
cin >> b;
cout << ""<< a <<" disminuido en "<< b <<" es "<< a - b <<".\n";
}

void multiplicar() {
cout << "Introduce un numero para multiplicarlo.\n";
cin >> a;
cout << "Ahora introduce el otro numero para multiplicarlo con "<< a <<".\n";
cin >> b;
cout << ""<< a <<" por "<< b <<" es "<< a * b <<"\n";
}

void dividir() {
cout << "Introduce un numero para dividirlo.\n";
cin >> a;
cout << "Ahora introduce el numero que dividira a "<< a <<".\n";
cin >> b;
cout << ""<< a <<" dividido en "<< b <<" es "<< a / b <<"\n";
cout << "Y sobra "<< a % b <<".\n";
}

void bucleMain() {
cout << "Esta es la Calculadora 1.0 beta\n";
cout << "Introduce '+' o 's' si quieres sumar\n";
cout << "Introduce '-' o 'r' si quieres restar\n";
cout << "Introduce '*' o 'm' si quieres multplicar\n";
cout << "Introduce '/' o 'd' si quieres dividir\n";
cout << "Si oprimes otra tecla me cerrare\n";

cin >> x;
switch (x) {
case 'S': case 's': case '+':
sumar();
break;
case 'R' : case 'r': case '-':
restar();
break;
case 'M' : case 'm': case '*':
multiplicar();
break;
case 'D' : case 'd': case '/':
dividir();
break;
case '\n' : case ' ':
break;

default : exit(0);
break;
}

}
main()
{
do { bucleMain(); }
while(1);
system("PAUSE");
return 0;
}

conio.h

Código:
/*
 * conio.h
 * This file has no copyright assigned and is placed in the Public Domain.
 * This file is a part of the mingw-runtime package.
 * No warranty is given; refer to the file DISCLAIMER within the package.
 *
 * Low level console I/O functions. Pretty please try to use the ANSI
 * standard ones if you are writing new code.
 *
 */

#ifndef	_CONIO_H_
#define	_CONIO_H_

/* All the headers include this file. */
#include <_mingw.h>

#ifndef RC_INVOKED

#ifdef	__cplusplus
extern "C" {
#endif

_CRTIMP char* __cdecl	_cgets (char*);
_CRTIMP int __cdecl	_cprintf (const char*, ...);
_CRTIMP int __cdecl	_cputs (const char*);
_CRTIMP int __cdecl	_cscanf (char*, ...);

_CRTIMP int __cdecl	_getch (void);
_CRTIMP int __cdecl	_getche (void);
_CRTIMP int __cdecl	_kbhit (void);
_CRTIMP int __cdecl	_putch (int);
_CRTIMP int __cdecl	_ungetch (int);


#ifndef	_NO_OLDNAMES

_CRTIMP int __cdecl	getch (void);
_CRTIMP int __cdecl	getche (void);
_CRTIMP int __cdecl	kbhit (void);
_CRTIMP int __cdecl	putch (int);
_CRTIMP int __cdecl	ungetch (int);

#endif	/* Not _NO_OLDNAMES */


#ifdef	__cplusplus
}
#endif

#endif	/* Not RC_INVOKED */

#endif	/* Not _CONIO_H_ */
__________________
Spread Firefox | Download Day 2008
¡Únete en nuestra misión para alcanzar el Record Guinness al software más descargado en 24 horas! http://www.spreadfirefox.com/es-ES/worldrecord/
  #5 (permalink)  
Antiguo 08/06/2008, 16:49
 
Fecha de Ingreso: mayo-2008
Ubicación: Chile
Mensajes: 189
Antigüedad: 16 años
Puntos: 3
Respuesta: problema con getch

Si, pero a ti te anda con cin y yo quiero usar el getch
  #6 (permalink)  
Antiguo 09/06/2008, 07:34
Avatar de _Lucifer_  
Fecha de Ingreso: junio-2006
Mensajes: 1.662
Antigüedad: 17 años, 11 meses
Puntos: 28
Respuesta: problema con getch

Esto:
Código:
...
getch(x);
switch (x) {
...
Debería estar así:
Código:
...
x=getch();
switch (x) {
...
Fíjate en el prototipo de la función:
Código:
...
_CRTIMP int __cdecl getch (void);
...
No recibe parámetros y retorna valor.

Saludos
__________________
Si crees que no tiene sentido, etonces probablemente lo tenga... :arriba:
  #7 (permalink)  
Antiguo 09/06/2008, 17:01
 
Fecha de Ingreso: mayo-2008
Ubicación: Chile
Mensajes: 189
Antigüedad: 16 años
Puntos: 3
Respuesta: problema con getch

Gracias _Lucifer_ ya resolvi mi duda y me funciono el programa.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:55.