/
Inicio :: Foros

 F.A.Q.F.A.Q.                  Conéctese para revisar sus mensajesConéctese para revisar sus mensajes   

que valide mi programa

 
      Índice del Foro elrincondelc.com -> C/C++ en General
Ver tema anterior :: Ver siguiente tema  
AutorMensaje
Omar Ali



Registrado: 07 Feb 2011
Mensajes: 16
Ubicación: Zacatecas

MensajePublicado: 03/11/2011 4:59 pm
Título: que valide mi programa

quiero que el programa que acontinuacion escribo valide los caracteres y ke si no es un numero no se cicle ayuda, urgente!!!
Código:


#include<cstdlib>
#include <iostream>

using namespace std;
struct factura{
       int fecha;
       struct fecha{
                 int dia;
                 int mes;
                 int anio;
              }d;
       struct cliente{
              string nombre;
              string direccion;
              string telefono;
              string producto[3][4];
           }c;
     
       }f;
int cont=0;
void cargar(factura &F){
    int i=0, j=0, sel=0;
    string descrip[6]={"CODIGO:      ", "NOMBRE:      ", "DESCRIPCION: ", "PRECIO:    $ " };
    cout<<"REGISTRE LOS SIGUENTES DATOS PARA GENERAR LA FACTURA"<<endl;
    cout<<"FECHA "<<endl;
    cout<<"ELIJA UNA OPCION "<<endl;
    cout<<"   *1: FECHA ACTUAL"<<endl;
    cout<<"   *2: OTRA FECHA"<<endl;
    cout<<"LA OPCION ES: ";
    cin>>sel;
    switch(sel){
        case 1:
            time_t t;
            struct tm *ahora;
            time(&t);
            ahora = localtime(&t);
            printf("LA FECHA ACTUAL ES: %02d/%02d/%02d ",ahora->tm_mday, ahora->tm_mon + 1,  ahora->tm_year + 1900);
            cout<<endl;
            f.d.dia=ahora->tm_mday;
            f.d.mes=ahora->tm_mon + 1;
            f.d.anio=ahora->tm_year + 1900;
            cout<<"DIA : "<<f.d.dia<<endl;
            cout<<"MES : "<<f.d.mes<<endl;
            cout<<"ANIO: "<<f.d.anio<<endl<<endl;
            cout<<"DATOS DEL CLIENTE"<<endl;
            fflush(stdin);   
            cout<<"NOMBRE:    ";
            getline(cin,f.c.nombre);
            fflush(stdin);
            cout<<"DIRECCION: ";
            getline(cin,f.c.direccion);
            fflush(stdin);
            cout<<"TELEFONO:  ";
            getline(cin,f.c.telefono);
            fflush(stdin);
            cout<<endl;
            cout<<"DATOS DEL PRODUCTO"<<endl;
            fflush(stdin);
            for(i=0;i<3;i++){         
                for(j=0;j<4;j++){
                    cout<<descrip[j];
                    cin>>f.c.producto[i][j];
                 }
                cout<<endl;
           }
            system("cls");
        break;
        case 2:
            cout<<"DIA: ";
            cin>>f.d.dia;
            while(f.d.dia>31){
                 cout<<"DIA NO VALIDO"<<endl;
                 cout<<"DIA: ";
                 cin>>f.d.dia;
                 }           
            cout<<"MES: ";
            cin>>f.d.mes;
            while(f.d.mes>12){
                 cout<<"MES NO VALIDO"<<endl;
                 cout<<"MES: ";
                 cin>>f.d.mes;
                 }         
            cout<<"ANIO: ";
            cin>>f.d.anio;
            while(f.d.anio<1000){
                 cout<<"ANIO NO VALIDO"<<endl;
                 cout<<"ANIO: ";
                 cin>>f.d.anio;
                 }
            system("cls");   
            cout<<"DATOS DEL CLIENTE"<<endl;
            fflush(stdin);   
            cout<<"NOMBRE ";
            getline(cin,f.c.nombre);
            fflush(stdin);
            cout<<"DIRECCION ";
            getline(cin,f.c.direccion);
            fflush(stdin);
            cout<<"TELEFONO ";
            getline(cin,f.c.telefono);
            fflush(stdin);
            system("cls");
            cout<<"DATOS DEL PRODUCTO"<<endl;
            fflush(stdin);
            for(i=0;i<3;i++){         
                for(j=0;j<4;j++){
                    cout<<descrip[j];
                    getline(cin,f.c.producto[i][j]);
                   }
                cout<<endl;
           }
            system("cls");
        break;
         default: cout<<"OPCION NO DISPONIBLE"<<endl;
        } 
}
           
   
   
   
void imprimir(factura &F){
    int i, j;
    string descrip[6]={"CODIGO   ", "       NOMBRE    ", "    DESCRIPCION  ", "         PRECIO  " };
    if(cont==1){
        cout<<"DIA : "<<f.d.dia<<"    MES : "<<f.d.mes<<"     ANIO: "<<f.d.anio<<endl<<endl;
       
        cout<<"NOMBRE:    "<<f.c.nombre<<endl;;
        cout<<"DIRECCION: "<<f.c.direccion<<endl;
        cout<<"TELEFONO:  "<<f.c.telefono<<endl;
        cout<<endl;
        for(i=0;i<4;i++){
            cout<<descrip[i];
        }
        cout<<endl;
        for(i=0;i<3;i++){
            for(j=0;j<4;j++){
                cout<<f.c.producto[i][j]<<"             ";
               }
               cout<<endl;
              }
         cout<<endl;
    }
    else{
         cout<<"LA FACTURA NO SE HA CARGADO"<<endl;
         }
}

int main(){
    factura F;
    int sel;
    system("color 1f");
     do{
        system("color 3f");
        cout<<"/////////////////////////////////////////////"<<endl;
        cout<<"*                                           /"<<endl;
        cout<<"*     BIENVENIDO AL MENU DE FACTURACION     /"<<endl;
        cout<<"*                                           /"<<endl;
        cout<<"*            1: INGRESAR DATOS              /"<<endl;
        cout<<"*            2: IMPRIMIR                    /"<<endl;
        cout<<"*            3: SALIR                       /"<<endl;
        cout<<"*                                           /"<<endl;
        cout<<"* * * * * * * * * * * * * * * * * * * * * * *"<<endl;
        cout<<endl;
        cout<<"LA OPCION ES: ";
        cin>>sel;
        system("cls");
       
    switch(sel){
        case 1:
             cont=1;
            cargar(F);
        break;
        case 2:
            imprimir(F);
        break;
         default: cout<<"OPCION NO DISPONIBLE"<<endl;
        } 
        }while (sel!=3);
}

_________________
ZoRrX 51 ¡EXITO!
Volver arriba
rir3760



Registrado: 01 Oct 2004
Mensajes: 6586
Ubicación: Mexico

MensajePublicado: 03/11/2011 7:17 pm
Título:

Para validar la entrada una opción es leer una linea de texto y a continuación utilizar la clase "stringstream" para verificar si su contenido es un numero.

Un programa de ejemplo (C++ estándar):
Código:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <string>
using std::string;

#include <sstream>
using std::stringstream;

int main()
{
   bool ok;
   
   do {
      cout << "Introduce un numero:" << endl;
      string linea;
      getline(cin, linea);
      
      stringstream aux;
      aux << linea;
      
      int numero;
      if (aux >> numero){
         cout << "El numero es " << numero << endl;
         ok = true;
      }else
         ok = false;
   }while (!ok);
   
   return 0;
}

Lo puedes tomar como base para realizar las modificaciones a tu programa.

Por ultimo utilizar "fflush(stdin)" es un error ya que no es valido (esa función espera un stream de salida como "stdout" mientras que "stdin" es uno de entrada).

Un saludo
_________________
Memory never recaptures reality. Memory reconstructs. All reconstructions change the original, becoming external frames of reference that inevitably fall short.
--
Mentat Handbook
Volver arriba
      Índice del Foro elrincondelc.com -> C/C++ en General
Página 1 de 1Todas las horas están en GMT - 8 Horas

 
No puede crear mensajes
No puede responder temas
No puede editar sus mensajes
No puede borrar sus mensajes
No puede votar en encuestas

(c) ElRincondelC.com

Un proyecto de UrlanHeat.com