miércoles, 27 de febrero de 2008

¿Cuál es la diferencia en C++ entre usar "\n" y endl?

La diferencia está en que al usar endl se invoca automáticamente a flush y "\n" no.

Cómo implementar la funcion getch(), readkey, getchar() para C++

El siguiente post explica como implementar una función getch() al estilo getchar() de Pascal o getche() de conio.h.

Creamos un fichero al que yo llamé Stflib.h ( debido a que incluyo otras funciones como ToString, Gotoxy, que puedes ver en este blog en el menú).

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <cstdlib>
 
using namespace std;

static bool isGenerated = false;

const string readCharScript[7] = {"#!/bin/sh" ,
"tput smso",
"tput rmso",
"oldstty=`stty -g`",
"stty -icanon min 1 time 0",
"dd bs=1 count=1 of=output >/dev/null 2>&1",
"stty \"$oldstty\"" };

void GenereFile() {
ofstream outputFile( ".readone", fstream::out );
for ( int i = 0; i < 7; i++ ) outputFile << readCharScript[i] << endl; system("chmod +x .readone"); isGenerated = true; } char getch() { if (!isGenerated) GenereFile(); system("./.readone"); char ch; ifstream file("output", fstream::in); file >> ch;
file.close();
return ch;
}



En otro fichero, al que llamé getch.C, ponemos:


#include "Stflib.h"

using namespace std;

int main() {
char ch = getch();
cout << endl << "El caracter es: " << ch << endl; return 0; }

lunes, 25 de febrero de 2008

¿Por qué se usa m_ como prefijo en los atributos privados de la clase en C++ y otros lenguajes?

En los atributos privados muchos programadores anteponen el prefijo m_ al atributo. Esto se hace para saber distinguir las variables de la clase de las que no lo son. La m viene de member variable.
En los atributos protegidos se utiliza m_p.

viernes, 22 de febrero de 2008

How to convert float to int in c++

Let's use static_cast.


float foo = 3.4; // Here the float variable
int myint = static_cast<int>(foo); // Now we have the int part of foo in myint


I hope I had helped you. If you want, you can post your comments and I'll answer you as soon as possible. Thanks!

Cómo convertir un float a un int en c++

Para convertir un int a float utilizamos static_cast de la siguiente manera:
float foo = 3.4;
int myint = static_cast<int>(foo);
Y ahora ya tenemos en la variable myint el valor 3.
Espero que les haya servido de ayuda. Pueden postear comentarios si lo desean o proponer una alternativa.