Ver Mensaje Individual
  #21 (permalink)  
Antiguo 18/06/2014, 12:40
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años, 1 mes
Puntos: 292
Respuesta: Atascado con ejercicio.

Gracias @vangodp .. en este ejercicio creo complica asi que me va a tocar dejarlo como antes

La verdad queria aprender algo con este ejercicio pero me volvi loco con castings de char* a entero, de const char a char* ... etc! es de locos!

Sera que ya que el compa viene demorado con el ejercicio.....de paso me ayudan a mi tambien a cerrar lagunas ?

Código C:
Ver original
  1. #include <string>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9.  
  10. // esta me funcionó en g++ pero no me sirve creo
  11. string rellenar_str(string str,char chr='0',int long_esperada=10,bool izq=true)
  12. {
  13.     int l;
  14.     l = strlen(str.c_str());
  15.  
  16.     if (l < long_esperada){
  17.         for (int i=0;i<(long_esperada-l);i++){
  18.             if (izq)
  19.                 str = chr+ str;
  20.             else
  21.                 str +=chr;
  22.         }
  23.     }
  24.  
  25.     return str;
  26. }
  27.  
  28. void rellenar_array_chars(const char str[],char chr='0',int long_esperada=10,bool izq=true)
  29. {
  30.     int l;
  31.     l = strlen(str);
  32.  
  33.     if (l < long_esperada){
  34.         for (int i=0;i<(long_esperada-l);i++){
  35.             if (izq)
  36.                 str = chr+ str;
  37.             else
  38.                 str +=chr;
  39.         }
  40.     }
  41. }
  42.  
  43.  
  44. bool Incrementa(char numero[10])
  45. {
  46.     for (int i=0;i<strlen(numero);i++)
  47.         if (numero[i]<48 || numero[i]>57){        
  48.             cout << "Error : no son solo numeros!" << endl;
  49.             return false;
  50.         }    
  51.  
  52.     int integer;
  53.     string str;
  54.  
  55.     integer=atoi(numero);
  56.     integer++;
  57.  
  58.    
  59.     stringstream ss;
  60.     ss << integer;
  61.     str = ss.str();
  62.  
  63.     const char* char_arr = str.c_str();
  64.  
  65.     rellenar_array_chars(char_arr);
  66.     //cout << integer;
  67.  
  68.     numero =  char_arr; // error: invalid conversion from 'const char*' to 'char*' ???????
  69.  
  70.     if ( strcmp( numero, "999999999" ) == 0 )
  71.       numero[0] = '0', numero[1] = '\0';
  72.  
  73.     return true; // arreglar
  74. }
  75.  
  76.  
  77. int main(void)
  78. {
  79.     char s1[]="0000000552";
  80.     char s2[]="900000173";
  81.     Incrementa(s1);
  82.     Incrementa(s2);
  83.     cout << s1 << endl;
  84.     cout << s2 << endl;
  85.  
  86.  
  87.  
  88. }


No me funciona........ obviamente chilla al hacer:

Código C:
Ver original
  1. numero =  char_arr;

Como se arregla el programa que esta hasta ahora escrito ? (he tomado fragmentos de otros contribuidores )
__________________
Salu2!