elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Estamos en la red social de Mastodon


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Problema al copiar string a un arreglo string dinamico VS2013 ultimate
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Problema al copiar string a un arreglo string dinamico VS2013 ultimate  (Leído 2,182 veces)
raver1983

Desconectado Desconectado

Mensajes: 3



Ver Perfil
Problema al copiar string a un arreglo string dinamico VS2013 ultimate
« en: 6 Febrero 2017, 07:10 am »

El problema ocurre en la linea 259 donde esta esto: inst_past = str_ir; El programa no esta completo aun pero necesito corregir ese error que al parecer es que no puede leer los datos, al tiempo de ejecucion str_ir si tiene un valor pero no lo puede signar a inst_past. Otro problema es al borrar el la el arreglo dinamico en la funcion void deleteMem() de la linea 139, pero me es mas urgente arreglar lo de la linea 259. El archivo de entrada es un .txt que contiene lo siguiente:
6
2
50
100
1100
4101
8100
3100
7200
2100
0008
0005

Donde:
Línea 01: No. de instrucciones
Línea 02: No. de datos
Línea 03: Dirección inicial de instrucciones
Línea 04: Dirección inicial de datos
A partir de la línea 5 vienen las instrucciones y después los datos

Espero puedan ayudarme!

Código
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void generarCicloFetch();
  8. void imprimirMem();
  9. void deleteMem();
  10. void ejecutarMem();
  11. void escribirMem();
  12. string conc(int ac_2, unsigned int numb_digit);
  13. void buscarInstSig();
  14. void buscarInstPast();
  15. void guardarInstPast();
  16. void saltoDireccion();
  17. void cargaAC();
  18. void imprimir_PC_IR_AC();
  19.  
  20. fstream archivoEntrada;
  21. ofstream archivoSalidaFetch;
  22. ofstream archivoSalidaMem;
  23. ofstream archivoSalidaAC;
  24. string nombre;
  25. string linea;
  26. string stroper;
  27. string strdirdat;
  28. string str_ir, str_ac, str_pc;
  29. const int cols = 2;
  30. int filas;
  31. int countlines = 0, i = 1, j = 0, i2, count_inst = 0, ciclos = 0, numb_inst = 0, numb_dat = 0, dirini_inst = 0, dirini_dat = 0, dir, pc, ac, ir, oper, numb_inst_past=0;
  32. bool inst_captured = false, mem_made = false, ejecutarPrograma = true, imprimirCabecera_PC_IR_AC = false;
  33. string **mem = new string*[filas];
  34. string *inst_past = new string[numb_inst];
  35.  
  36. int main() {
  37.  
  38. generarCicloFetch();
  39. //cout << "\n\nmemoria\n" << mem[0][0] << " " << mem[0][1] << endl;
  40. //imprimirMem();
  41. //deleteMem();
  42. /*escribirMem();
  43. escribirMem();*/
  44. ejecutarMem();
  45. cout << endl;
  46. system("pause");
  47.  
  48. return 0;
  49. }
  50. void generarCicloFetch(){
  51. cout << "Escribe el nombre del archivo de texto.\nEscribe tambien la extension (.txt): ";
  52. fflush(stdin);
  53. getline(cin, nombre);
  54.  
  55. archivoEntrada.open(nombre.c_str(), ios::in);
  56. if (archivoEntrada.is_open()) {
  57. archivoSalidaFetch.open("ciclofetch.txt", ios::out);
  58. archivoSalidaFetch << "Archivo: " << nombre << endl << endl;
  59. while (!archivoEntrada.eof()) {
  60. getline(archivoEntrada, linea);
  61. countlines++;
  62. switch (countlines){
  63. case 1: numb_inst = stoi(linea);
  64. archivoSalidaFetch << "No.de instrucciones: " << numb_inst << endl;
  65. break;
  66. case 2: numb_dat = stoi(linea);
  67. archivoSalidaFetch << "No. de datos: " << numb_dat << endl;
  68. break;
  69. case 3: dirini_inst = stoi(linea);
  70. archivoSalidaFetch << "Dirección inicial de instrucciones: " << dirini_inst << endl;
  71. break;
  72. case 4: dirini_dat = stoi(linea);
  73. archivoSalidaFetch << "Dirección inicial de datos: " << dirini_dat << endl << endl;
  74. break;
  75. }
  76.  
  77. if (countlines >= 5){
  78. if (!mem_made){
  79. filas = numb_inst + numb_dat;
  80.  
  81. for (int i = 0; i < filas; i++)
  82. mem[i] = new string[cols];
  83.  
  84. mem_made = true;
  85. }
  86.  
  87. if (linea.length() == 4){
  88. if (countlines == 5){
  89. archivoSalidaFetch << "Instrucciones" << endl;
  90. dir = dirini_inst;
  91. }
  92. /*stroper = linea[0];
  93. strdirdat = linea.substr(1, 3);*/
  94. if (i <= numb_inst && !inst_captured){
  95. stroper = linea[0];
  96. strdirdat = linea.substr(1, 3);
  97. archivoSalidaFetch << dir << " Cod: " << stroper << "     Dir: " << strdirdat << endl;
  98. mem[i - 1][j] = to_string(dir);
  99. mem[i - 1][j + 1] = linea;
  100. dir++;
  101. i++;
  102. if (i > numb_inst){
  103. inst_captured = true;
  104. i2 = i;
  105. i = 1;
  106. dir = dirini_dat;
  107. archivoSalidaFetch << "\nDatos" << endl;
  108. }
  109. }
  110. else if (inst_captured && i <= numb_dat){
  111. archivoSalidaFetch << dir << " Valor:  " << linea << endl;
  112. mem[i2 - 1][j] = to_string(dir);
  113. mem[i2 - 1][j + 1] = linea;
  114. dir++;
  115. i++;
  116. i2++;
  117. }
  118. }
  119.  
  120. }
  121. }
  122.  
  123. archivoEntrada.close();
  124. archivoSalidaFetch.close();
  125. cout << "\nArchivo de salida (ciclofetch.txt) creado con exito..." << endl;
  126. }
  127. else cout << "\nArchivo inexistente o faltan permisos para abrirlo" << endl;
  128.  
  129. }
  130. void imprimirMem(){
  131. cout << "\n\nMemoria" << endl << endl;
  132. for (int i = 0; i < filas; i++){
  133. for (int j = 0; j < cols; j++){
  134. cout << mem[i][j] << " ";
  135. }
  136. cout << endl;
  137. }
  138. }
  139. void deleteMem(){
  140. // Clean Up
  141.  
  142. /*for (int i = 0; i < filas; i++)
  143. delete [] mem[i];*/
  144.  
  145. //delete [] mem;
  146. }
  147. void ejecutarMem(){
  148. if (ciclos == 0){
  149. pc = dirini_inst;
  150. buscarInstSig();
  151. }
  152. else if (ciclos > 0){
  153. buscarInstSig();
  154. buscarInstPast();
  155. }
  156.  
  157. if (ejecutarPrograma){
  158. if (!imprimirCabecera_PC_IR_AC){
  159. cout << "\n\n\tPC\t|\tIR\t|\tAC" << endl;
  160. cout << "    -------------------------------------------" << endl;
  161. imprimirCabecera_PC_IR_AC = true;
  162. }
  163. switch (oper)
  164. {
  165. case 0:// salto direccion
  166. imprimir_PC_IR_AC();
  167. guardarInstPast();
  168. ciclos++;
  169. saltoDireccion();
  170. break;
  171. case 1:// carga AC con mem[dir]
  172. cargaAC();
  173. imprimir_PC_IR_AC();
  174. guardarInstPast();
  175. pc++;
  176. ciclos++;
  177. break;
  178. case 2:
  179. break;
  180. case 3:
  181. break;
  182. case 4:
  183. break;
  184. case 5:
  185. break;
  186. case 6:
  187. break;
  188. case 7:
  189. break;
  190. case 8:
  191. break;
  192. case 9:
  193. break;
  194. default:
  195. break;
  196. }
  197. }
  198.  
  199.  
  200. }
  201. string conc(int ac_2, unsigned int numb_digit){
  202. string str;
  203. str = to_string(ac_2);
  204.  
  205. while (str.length() < numb_digit){
  206. str = "0" + str;
  207. }
  208.  
  209. return str;
  210. }
  211. void escribirMem(){
  212. archivoSalidaMem.open("memoria.txt", ios::app);
  213. for (int i = 0; i < filas; i++){
  214. for (int j = 0; j < cols; j++){
  215. if (j == 1){
  216. archivoSalidaMem << " ";
  217. }
  218. archivoSalidaMem << mem[i][j];
  219. }
  220. archivoSalidaMem << endl;
  221. }
  222. archivoSalidaMem << endl;
  223. archivoSalidaMem.close();
  224. }
  225. void buscarInstSig(){
  226. bool inst_found;
  227.  
  228. inst_found = false;
  229. str_pc = to_string(pc);
  230. for (int i = 0; i < numb_inst && !inst_found; i++){
  231. //str_pc = to_string(pc);
  232. if (str_pc == mem[i][0]){
  233. str_ir = mem[i][1];
  234. stroper = str_ir[0];
  235. strdirdat = str_ir.substr(1, 3);
  236. oper = stoi(stroper);
  237. dir = stoi(strdirdat);
  238. inst_found = true;
  239. }
  240. }
  241. }
  242. void buscarInstPast(){
  243. bool instpast_found;
  244. instpast_found = false;
  245. if (numb_inst_past > 0){
  246. for (int i = 0; i < numb_inst_past && !instpast_found; i++){
  247. //buscarInstSig();
  248. if (str_ir == inst_past[i]){
  249. instpast_found = true;
  250. ejecutarPrograma = false;
  251. }
  252. }
  253. }
  254. }
  255. void guardarInstPast(){
  256. if (numb_inst_past == 0){
  257. i = 0;
  258. }
  259. inst_past[i] = str_ir;
  260. cout << "\ninst_past[i]: " << inst_past[i] << endl;
  261. i++;
  262. numb_inst_past++;
  263.  
  264. }
  265. void saltoDireccion(){
  266. pc = dir;
  267. }
  268. void imprimir_PC_IR_AC(){
  269. str_ac = conc(ac, 4);
  270. cout << "\t" << pc << "\t|\t" << str_ir << "\t|\t" << str_ac << endl;
  271. }
  272. void cargaAC(){
  273. bool dat_found;
  274. dat_found = false;
  275. /*string str_pc_aux;
  276. str_pc_aux = to_string(dir);*/
  277. for (int i = numb_inst; i < filas && !dat_found; i++){
  278. if (strdirdat == mem[i][0]/*str_pc_aux == mem[i][0]*/){
  279. str_ac = mem[i][1];
  280. ac = stoi(str_ac);
  281. dat_found = true;
  282. }
  283. }
  284. }



« Última modificación: 6 Febrero 2017, 23:12 pm por raver1983 » En línea

raver1983

Desconectado Desconectado

Mensajes: 3



Ver Perfil
Re: Problema al copiar string a un arreglo string dinamico VS2013 ultimate
« Respuesta #1 en: 7 Febrero 2017, 01:11 am »

Ya pude arreglar el error de la linea 259. Para lograrlo modifique la variable global de la siguiente forma:

Código
  1. string *inst_past = NULL;

Agregue además una variable global de tipo bool: instpast_made = false

Y después de la linea 63 agregue para que quedara así:

Código
  1. case 1: numb_inst = stoi(linea);
  2. if (!instpast_made){
  3. inst_past = new string[numb_inst];
  4. instpast_made = true;
  5. }
  6. archivoSalidaFetch << "No.de instrucciones: " << numb_inst << endl;
  7. break;

Al final el código quedo así:

Código
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void generarCicloFetch();
  8. void imprimirMem();
  9. void deleteMem();
  10. void ejecutarMem();
  11. void escribirMem();
  12. string conc(int ac_2, unsigned int numb_digit);
  13. void buscarInstSig();
  14. void buscarInstPast();
  15. void guardarInstPast();
  16. void saltoDireccion();
  17. void cargaAC();
  18. void imprimir_PC_IR_AC();
  19.  
  20. fstream archivoEntrada;
  21. ofstream archivoSalidaFetch;
  22. ofstream archivoSalidaMem;
  23. ofstream archivoSalidaAC;
  24. string nombre;
  25. string linea;
  26. string stroper;
  27. string strdirdat;
  28. string str_ir, str_ac, str_pc;
  29. const int cols = 2;
  30. int filas;
  31. int countlines = 0, i = 1, j = 0, i2, count_inst = 0, ciclos = 0, numb_inst = 0, numb_dat = 0, dirini_inst = 0, dirini_dat = 0, dir, pc, ac, ir, oper, numb_inst_past=0;
  32. bool inst_captured = false, mem_made = false, instpast_made = false, ejecutarPrograma = true, imprimirCabecera_PC_IR_AC = false;
  33. string **mem = new string*[filas];
  34. string *inst_past = NULL;//string *inst_past = new string[numb_inst];
  35.  
  36. int main() {
  37.  
  38. generarCicloFetch();
  39. //cout << "\n\nmemoria\n" << mem[0][0] << " " << mem[0][1] << endl;
  40. //imprimirMem();
  41. //deleteMem();
  42. /*escribirMem();
  43. escribirMem();*/
  44. ejecutarMem();
  45. cout << endl;
  46. system("pause");
  47.  
  48. return 0;
  49. }
  50. void generarCicloFetch(){
  51. cout << "Escribe el nombre del archivo de texto.\nEscribe tambien la extension (.txt): ";
  52. fflush(stdin);
  53. getline(cin, nombre);
  54.  
  55. archivoEntrada.open(nombre.c_str(), ios::in);
  56. if (archivoEntrada.is_open()) {
  57. archivoSalidaFetch.open("ciclofetch.txt", ios::out);
  58. archivoSalidaFetch << "Archivo: " << nombre << endl << endl;
  59. while (!archivoEntrada.eof()) {
  60. getline(archivoEntrada, linea);
  61. countlines++;
  62. switch (countlines){
  63. case 1: numb_inst = stoi(linea);
  64. if (!instpast_made){
  65. inst_past = new string[numb_inst];
  66. instpast_made = true;
  67. }
  68. archivoSalidaFetch << "No.de instrucciones: " << numb_inst << endl;
  69. break;
  70. case 2: numb_dat = stoi(linea);
  71. archivoSalidaFetch << "No. de datos: " << numb_dat << endl;
  72. break;
  73. case 3: dirini_inst = stoi(linea);
  74. archivoSalidaFetch << "Dirección inicial de instrucciones: " << dirini_inst << endl;
  75. break;
  76. case 4: dirini_dat = stoi(linea);
  77. archivoSalidaFetch << "Dirección inicial de datos: " << dirini_dat << endl << endl;
  78. break;
  79. }
  80.  
  81. if (countlines >= 5){
  82. if (!mem_made){
  83. filas = numb_inst + numb_dat;
  84.  
  85. for (int i = 0; i < filas; i++)
  86. mem[i] = new string[cols];
  87.  
  88. mem_made = true;
  89. }
  90.  
  91. if (linea.length() == 4){
  92. if (countlines == 5){
  93. archivoSalidaFetch << "Instrucciones" << endl;
  94. dir = dirini_inst;
  95. }
  96. /*stroper = linea[0];
  97. strdirdat = linea.substr(1, 3);*/
  98. if (i <= numb_inst && !inst_captured){
  99. stroper = linea[0];
  100. strdirdat = linea.substr(1, 3);
  101. archivoSalidaFetch << dir << " Cod: " << stroper << "     Dir: " << strdirdat << endl;
  102. mem[i - 1][j] = to_string(dir);
  103. mem[i - 1][j + 1] = linea;
  104. dir++;
  105. i++;
  106. if (i > numb_inst){
  107. inst_captured = true;
  108. i2 = i;
  109. i = 1;
  110. dir = dirini_dat;
  111. archivoSalidaFetch << "\nDatos" << endl;
  112. }
  113. }
  114. else if (inst_captured && i <= numb_dat){
  115. archivoSalidaFetch << dir << " Valor:  " << linea << endl;
  116. mem[i2 - 1][j] = to_string(dir);
  117. mem[i2 - 1][j + 1] = linea;
  118. dir++;
  119. i++;
  120. i2++;
  121. }
  122. }
  123.  
  124. }
  125. }
  126.  
  127. archivoEntrada.close();
  128. archivoSalidaFetch.close();
  129. cout << "\nArchivo de salida (ciclofetch.txt) creado con exito..." << endl;
  130. }
  131. else cout << "\nArchivo inexistente o faltan permisos para abrirlo" << endl;
  132.  
  133. }
  134. void imprimirMem(){
  135. cout << "\n\nMemoria" << endl << endl;
  136. for (int i = 0; i < filas; i++){
  137. for (int j = 0; j < cols; j++){
  138. cout << mem[i][j] << " ";
  139. }
  140. cout << endl;
  141. }
  142. }
  143. void deleteMem(){
  144. // Clean Up
  145.  
  146. /*for (int i = 0; i < filas; i++)
  147. delete [] mem[i];*/
  148.  
  149. //delete [] mem;
  150. }
  151. void ejecutarMem(){
  152. if (ciclos == 0){
  153. pc = dirini_inst;
  154. buscarInstSig();
  155. }
  156. else if (ciclos > 0){
  157. buscarInstSig();
  158. buscarInstPast();
  159. }
  160.  
  161. if (ejecutarPrograma){
  162. if (!imprimirCabecera_PC_IR_AC){
  163. cout << "\n\n\tPC\t|\tIR\t|\tAC" << endl;
  164. cout << "    -------------------------------------------" << endl;
  165. imprimirCabecera_PC_IR_AC = true;
  166. }
  167. switch (oper)
  168. {
  169. case 0:// salto direccion
  170. imprimir_PC_IR_AC();
  171. guardarInstPast();
  172. ciclos++;
  173. saltoDireccion();
  174. break;
  175. case 1:// carga AC con mem[dir]
  176. cargaAC();
  177. imprimir_PC_IR_AC();
  178. guardarInstPast();
  179. pc++;
  180. ciclos++;
  181. break;
  182. case 2:
  183. break;
  184. case 3:
  185. break;
  186. case 4:
  187. break;
  188. case 5:
  189. break;
  190. case 6:
  191. break;
  192. case 7:
  193. break;
  194. case 8:
  195. break;
  196. case 9:
  197. break;
  198. default:
  199. break;
  200. }
  201. }
  202.  
  203.  
  204. }
  205. string conc(int ac_2, unsigned int numb_digit){
  206. string str;
  207. str = to_string(ac_2);
  208.  
  209. while (str.length() < numb_digit){
  210. str = "0" + str;
  211. }
  212.  
  213. return str;
  214. }
  215. void escribirMem(){
  216. archivoSalidaMem.open("memoria.txt", ios::app);
  217. for (int i = 0; i < filas; i++){
  218. for (int j = 0; j < cols; j++){
  219. if (j == 1){
  220. archivoSalidaMem << " ";
  221. }
  222. archivoSalidaMem << mem[i][j];
  223. }
  224. archivoSalidaMem << endl;
  225. }
  226. archivoSalidaMem << endl;
  227. archivoSalidaMem.close();
  228. }
  229. void buscarInstSig(){
  230. bool inst_found;
  231.  
  232. inst_found = false;
  233. str_pc = to_string(pc);
  234. for (int i = 0; i < numb_inst && !inst_found; i++){
  235. //str_pc = to_string(pc);
  236. if (str_pc == mem[i][0]){
  237. str_ir = mem[i][1];
  238. stroper = str_ir[0];
  239. strdirdat = str_ir.substr(1, 3);
  240. oper = stoi(stroper);
  241. dir = stoi(strdirdat);
  242. inst_found = true;
  243. }
  244. }
  245. }
  246. void buscarInstPast(){
  247. bool instpast_found;
  248. instpast_found = false;
  249. if (numb_inst_past > 0){
  250. for (int i = 0; i < numb_inst_past && !instpast_found; i++){
  251. //buscarInstSig();
  252. if (str_ir == inst_past[i]){
  253. instpast_found = true;
  254. ejecutarPrograma = false;
  255. }
  256. }
  257. }
  258. }
  259. void guardarInstPast(){
  260. if (numb_inst_past == 0){
  261. i = 0;
  262. }
  263. inst_past[i] = str_ir;
  264. //cout << "\ninst_past[i]: " << inst_past[i] << endl;
  265. i++;
  266. numb_inst_past++;
  267.  
  268. }
  269. void saltoDireccion(){
  270. pc = dir;
  271. }
  272. void imprimir_PC_IR_AC(){
  273. str_ac = conc(ac, 4);
  274. cout << "\t" << pc << "\t|\t" << str_ir << "\t|\t" << str_ac << endl;
  275. }
  276. void cargaAC(){
  277. bool dat_found;
  278. dat_found = false;
  279. /*string str_pc_aux;
  280. str_pc_aux = to_string(dir);*/
  281. for (int i = numb_inst; i < filas && !dat_found; i++){
  282. if (strdirdat == mem[i][0]/*str_pc_aux == mem[i][0]*/){
  283. str_ac = mem[i][1];
  284. ac = stoi(str_ac);
  285. dat_found = true;
  286. }
  287. }
  288. }

Solo falta arreglar lo de la funcion void deleteMem() para borrar la memoria alocada.


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
como puedo hacer q mi JTextArea me imprima mas de un dato string datos string
Dudas Generales
alda19 1 5,064 Último mensaje 15 Junio 2011, 03:18 am
por Novlucker
Recorrer un Arreglo en Java (Buscar String)
Java
NatzMorel 1 4,184 Último mensaje 14 Junio 2012, 11:31 am
por ollo
Problema con busqueda en arreglo de string
Programación C/C++
MikeMonostone 4 2,799 Último mensaje 13 Noviembre 2012, 00:28 am
por leosansan
Como creo un arreglo MultiClase? a = {String, int, miclase}
Programación C/C++
Synth3tik0 2 2,656 Último mensaje 1 Enero 2013, 21:11 pm
por 0xDani
Arreglo dinamico string
Programación C/C++
leopaez 3 4,719 Último mensaje 10 Diciembre 2016, 21:43 pm
por engel lex
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines