Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int teclado(char NRomano[]){ /*function that converts the numbers*/
int tam = strlen(NRomano);
int arabe = 0;
for (int i=0; i<tam; i++) {
switch (NRomano[i]) {
case 'V': arabe = arabe + 5;
break;
case 'L': arabe = arabe + 50;
break;
case 'D': arabe = arabe + 500;
break;
case 'M': arabe = arabe + 1000;
break;
case 'I':
if (i == tam-1)
arabe = arabe + 1;
else if (NRomano[i+1] != 'I')
arabe = arabe - 1;
else
arabe = arabe + 1;
break;
case 'X':
if (i == tam-1 )
arabe = arabe + 10;
else if (( NRomano[i+1] != 'I') && (NRomano[i+1] != 'V') && (NRomano[i+1] != 'X'))
arabe = arabe - 10;
else
arabe = arabe + 10;
break;
case 'C':
if (i == tam-1)
arabe = arabe + 100;
else if (( NRomano[i+1] != 'M') && ( NRomano[i+1] != 'D'))
arabe = arabe + 100;
else
arabe = arabe - 100;
break;
}
}
return arabe;
}
void main() {
char escolha, op;
do{
system("cls");
printf("Conversao de numeros romanos para arabe !!\t");
printf("\n\n");
printf("Como quer fornecer os dados?\n\n");
printf("---------------------------------------\n");
printf("| |\n");
printf("| Por Ficheiro [1] |\n");
printf("| Por Teclado [2] |\n");
printf("| Deseja Sair do Programa ? [3] |\n");
printf("| |\n");
printf("---------------------------------------\n\n");
printf("Qual e a sua escolha?: ");
fflush(stdin);
scanf("%c", &escolha);
switch(escolha){
case '1':
FILE *f;
int ne1, *Arabe1, i;
char **NRomano1;
char drive[10], ficheiro[10];
system("cls");
printf("Qual a drive?\n");
fflush(stdin);
gets(drive);
strcat(drive,":\\");
printf("Qual o nome do ficheiro?\n");
fflush(stdin);
gets(ficheiro);
strcat(drive, ficheiro);
f= fopen(drive, "r");
printf("\n\n");
printf("Quantos numeros romanos quer converter?: ");
scanf("%d", &ne1);
if (f != NULL) {
NRomano1 = (char**) malloc (ne1 * sizeof(char*));
Arabe1 = (int*) malloc (ne1 * sizeof(int));
while (!feof(f)) {
for (i=0; i<ne1; i++)
NRomano1[i] = (char*) malloc(ne1 * sizeof(char));
for (i=0; i<ne1; i++) {
fgets(NRomano1[i], ne1, f);
Arabe1[i] = teclado(NRomano1[i]);
}
}
for (i=0; i<ne1; i++) {
printf("O numero romano %s = %d em arabe\n", NRomano1[i], Arabe1[i]);
}
}
else {
puts("Nao e possivel abrir o ficheiro");
}
fclose(f);
break;
case '2':
FILE *fp;
int k, *Arabe, ne;
char **NRomano;
system("cls");
fp = fopen("C:\\exe\\romano.txt","w");
printf("Quantos numeros romanos quer converter?: ");
scanf("%d", &ne);
NRomano = (char**) malloc (ne * sizeof(char*));
Arabe = (int*) malloc (ne * sizeof(int));
printf("\n\n");
for (k=0; k<ne; k++)
NRomano[k] = (char*) malloc(ne * sizeof(char));
for (k=0; k<ne; k++) {
printf("Digite o numero romano: ");
fflush(stdin);
gets(NRomano[k]);
Arabe[k] = teclado(NRomano[k]);
}
printf("\n\n");
for (k=0; k<ne; k++){
printf("O numero romano %s = %d em arabe\n", NRomano[k], Arabe[k]);
fprintf(fp,"O numero romano %s = %d em arabe\n", NRomano[k], Arabe[k]);
}
fclose(fp);
break;
case '3': exit(1);
break;
}
printf("\n");
printf("Deseja Sair?\n");
fflush(stdin);
scanf("%c", &op);
}while( op == 'n');
}
here it is, the part of the problem is in the switch case "1", when i run the program it asks me wich drive i want and the name of the file to read it(the file has 2 roman number) and then i ask the program how many numbers i want to convert i choose "1" and the program stops there and doesnt show anything. and i dont know if its useful but i use visual studio.