We've done Java, we've done HTML, now for some C

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Code:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

void UserConvert();
void BatchConvert();

void BatchConvert()
{
	int arabicnum[30];
	int arabicnumdoc[30];
	int arabicnumdocout[30];
    int counter;
	char roman;
	FILE *file_in;
    FILE *file_out;


	printf("Please enter the name of the file containing the list of Arabic Numbers to be converted: \n");
	scanf("%s", &arabicnumdoc);

	{
		file_in=fopen("arabicnumdoc.txt", "r");
		while (!feof(file_in))

		{
			fscanf(file_in, "%s", &arabicnum);
		}

		fclose(file_in);
	}

	roman =0; 

	for (counter =0; counter <29; counter++)

	{

		if(arabicnum[counter]==1)
			roman +='I';

		else if(arabicnum[counter -1]==5)
		{
			if(arabicnum[counter]==1)
				roman +='I'+'I'+'I';
			else
				roman +='V';
		}

		if(arabicnum[counter]==10)
		{
			if(arabicnum[counter -1]==1)
				roman +='V'+'I'+'I'+'I';
			else if(arabicnum[counter]==5)
				roman +=0;
			else
				roman +='X';
		}

		if(arabicnum[counter]==50)
		{
			if(arabicnum[counter -1]==1)
				roman +='X'+'L'+'V'+'I'+'I'+'I';
			else if(arabicnum[counter]==5)
				roman +='X'+'L';
			else if(arabicnum[counter]==10)
				roman +='X'+'X'+'X';
			else
				roman +='L';
		}

		if(arabicnum[counter]==100)
		{
			if(arabicnum[counter -1]==1)
				roman +='X'+'C'+'V'+'I'+'I'+'I';
			else if(arabicnum[counter]==5)
				roman +='X'+'C';
			else if(arabicnum[counter]==10)
				roman +='L'+'X'+'X'+'X';
			else if(arabicnum[counter]==50)
				roman +=0;
			else
				roman +='C';
		}

		if(arabicnum[counter]==500)
		{
			if(arabicnum[counter -1]==1)
				roman +='C'+'D'+'X'+'C'+'V'+'I'+'I'+'I';
			else if(arabicnum[counter]==5)
				roman +='C'+'D'+'X'+'C';
			else if(arabicnum[counter]==10)
				roman +='C'+'D'+'L'+'X'+'X'+'X';
			else if(arabicnum[counter]==50)
				roman +='C'+'D';
			else if(arabicnum[counter]==100)
				roman +='C'+'C'+'C';
			else
				roman +='D';
		}


		if(arabicnum[counter]==1000)
		{
			if(arabicnum[counter -1]==1)
				roman +='C'+'M'+'X'+'C'+'V'+'I'+'I'+'I';
			else if(arabicnum[counter]==5)
				roman +='C'+'M'+'X'+'C';
			else if(arabicnum[counter]==10)
				roman +='C'+'M'+'L'+'X'+'X'+'X';
			else if(arabicnum[counter]==50)
				roman +='C'+'M';
			else if(arabicnum[counter]==100)
				roman +='D'+'C'+'C'+'C';
			else if(arabicnum[counter]==500)
				roman +=0;
			else
				roman +='M';
		}
	}

	{
		printf("Please wait whilst the Arabic Numbers are converted \n\n");
		printf("Please enter the name of the file to which the Roman Numerals will be written to: \n");
		scanf("%s", &arabicnumdocout);

		file_out=fopen("arabicnumdocout.txt", "w");
		fprintf(file_out, "%d\n", roman);
		fclose(file_out);
	}
	printf("The conversion is now complete, thank you for using my program\n\n");
}

void UserConvert()
{
	int arabicnum[4];
	int counter;
	char roman;

	printf("You have chosen to input the Arabic Number you wish to convert yourself. Please enter an Arabic Number: \n");
	scanf("%d", &arabicnum);

	roman ='I'; 

	for (counter =0; counter <4; counter++)

	{
		if(arabicnum[counter]==1)
			roman +='I';

		else if(arabicnum[counter -1]==5)
		{
			if(arabicnum[counter]==1)
				roman +='I'+'I'+'I';
			else
				roman +='V';
		}

		if(arabicnum[counter]==10)
		{
			if(arabicnum[counter -1]==1)
				roman +='V'+'I'+'I'+'I';
			else if(arabicnum[counter]==5)
				roman +=0;
			else
				roman +='X';
		}

		if(arabicnum[counter]==50)
		{
			if(arabicnum[counter -1]==1)
				roman +='X'+'L'+'V'+'I'+'I'+'I';
			else if(arabicnum[counter]==5)
				roman +='X'+'L';
			else if(arabicnum[counter]==10)
				roman +='X'+'X'+'X';
			else
				roman +='L';
		}

		if(arabicnum[counter]==100)
		{
			if(arabicnum[counter -1]==1)
				roman +='X'+'C'+'V'+'I'+'I'+'I';
			else if(arabicnum[counter]==5)
				roman +='X'+'C';
			else if(arabicnum[counter]==10)
				roman +='L'+'X'+'X'+'X';
			else if(arabicnum[counter]==50)
				roman +=0;
			else
				roman +='C';
		}

		if(arabicnum[counter]==500)
		{
			if(arabicnum[counter -1]==1)
				roman +='C'+'D'+'X'+'C'+'V'+'I'+'I'+'I';
			else if(arabicnum[counter]==5)
				roman +='C'+'D'+'X'+'C';
			else if(arabicnum[counter]==10)
				roman +='C'+'D'+'L'+'X'+'X'+'X';
			else if(arabicnum[counter]==50)
				roman +='C'+'D';
			else if(arabicnum[counter]==100)
				roman +='C'+'C'+'C';
			else
				roman +='D';
		}


		if(arabicnum[counter]==1000)
		{
			if(arabicnum[counter -1]==1)
				roman +='C'+'M'+'X'+'C'+'V'+'I'+'I'+'I';
			else if(arabicnum[counter]==5)
				roman +='C'+'M'+'X'+'C';
			else if(arabicnum[counter]==10)
				roman +='C'+'M'+'L'+'X'+'X'+'X';
			else if(arabicnum[counter]==50)
				roman +='C'+'M';
			else if(arabicnum[counter]==100)
				roman +='D'+'C'+'C'+'C';
			else if(arabicnum[counter]==500)
				roman +=0;
			else
				roman +='M';
		}
	}

	printf("The Arabic Number you entered was %d. The Roman equivelant is %c. \n\n", arabicnum, roman);
}

//everything from here on is the part of the program the user will see. A prompt appears asking the user to choose
//between inputting numbers themselves, or running batch mode.

void main()

{
	int userchoice;

do
{
	printf("Welcome to the numeral conversion program. Please press 1 to enter the Arabic Number you wish to convert, or 2 to convert a list of numbers: \n");
	scanf("%d", &userchoice);
}
while ((userchoice != 1) && (userchoice != 2));

if (userchoice == 1)
UserConvert();
else if (userchoice == 2)
BatchConvert();
else;
}

Okay. I have to write a program to convert Arabic numbers (normal numbers) to Roman Numerals. Unfortunately, i'm a tard and wrote a program to convert Roman to Arabic. Basically i've tried to swap everything over. The first function is for a 'Batch' method, where it reads in a list of numbers from a txt doc, and writes the converted numbers into another. The second function is for the user to input a number into the cmd and it converts it there.

The program compiles, but gives me warnings about converting from int to char, which i'm not massively worried about, unless it's what has stopped the program working.

I've not tried the batch part yet, but when i enter a number into CMD is gives me the reply 'The Arabic Number you entered was 1244788. The Roman equivelant is random symbol'.

Have i got this completely wrong or is it something easy to fix?

Thanks in advance.
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Fixed the 1244788 issue, just the lack of conversion issue now.
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
I've been informed that's all crap, so i've restarted... but i'm struggling. I think it's probably the algorithm that i'm really struggling to get my head round.
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Code:
void UserConvert(void)

{
int num1;

printf("You have chosen to input the Arabic Number you wish to convert yourself. Please enter an Arabic Number: \n");
scanf("%i", &num1);

while (num1 >= 1000)
{
printf("M");
num1 -= 1000;
}
if (num1 >= 900)
{
printf("CM");
num1 -= 900;
}
if (num1 >= 500)
{
printf("D");
num1 -= 500;
}
if (num1 >= 400)
{
printf("CD");
num1 -= 400;
}
while (num1 >= 100)
{
printf("C");
num1 -= 100;
}
if (num1 >= 90)
{
printf("XC");
num1 -= 90;
}
if (num1 >= 50)
{
printf("L");
num1 -= 50;
}
if (num1 >= 40)
{
printf("XL");
num1 -= 40;
}
while (num1 >= 10)
{
printf("X");
num1 -= 10;
}
if (num1 >= 9)
{
printf("IX");
num1 -= 9;
}
if (num1 >= 5)
{
printf("V");
num1 -= 5;
}
if (num1 >= 4)
{
printf("IV");
num1 -= 4;
}
while (num1 >= 1)
{
printf("I");
num1 -= 1;
}
}

no? euff. compiles but gives me this string as a result MMDDCCLLXXVVI followed by a string of random characters
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Don't think that convert code will work for the batch function though :(
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
ahaha more problems. this is turning into a diary, rather than a help thread, but i'll put that down to it being 5:37 or possibly my code being so bad it's beyond help.

anyway. The Conversion function works fine, no issues there BUT the program is completely skipping past the Batch function, and i can't work out why.

Code:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

void UserConvert();
void BatchConvert();

void UserConvert(void)
{
  int num1;

  printf("You have chosen to input the Arabic Number you wish to convert yourself. Please enter an Arabic Number: \n");
  scanf("%i", &num1);

    while (num1 >= 1000)
    {
      printf("M");
      num1 -= 1000;
    }
      if (num1 >= 900)
    {
      printf("CM");
      num1 -= 900;
    }
      if (num1 >= 500)
    {
      printf("D");
      num1 -= 500;
    }
      if (num1 >= 400)
    {
      printf("CD");
      num1 -= 400;
    }
      while (num1 >= 100)
    {
      printf("C");
      num1 -= 100;
    }
      if (num1 >= 90)
    {
      printf("XC");
      num1 -= 90;
    }
      if (num1 >= 50)
    {
      printf("L");
      num1 -= 50;
    }
      if (num1 >= 40)
    {
      printf("XL");
      num1 -= 40;
    }
      while (num1 >= 10)
    {
      printf("X");
      num1 -= 10;
    }
      if (num1 >= 9)
    {
      printf("IX");
      num1 -= 9;
    }
      if (num1 >= 5)
    {
      printf("V");
      num1 -= 5;
    }
      if (num1 >= 4)
    {
      printf("IV");
      num1 -= 4;
    }
      while (num1 >= 1)
    {
      printf("I");
      num1 -= 1;
    }
}

void BatchConvert(void)
{
	int num1;
	FILE *file_in;
	FILE *file_out;

	*file_in=fopen("arabicnumdoc.txt", "r");

	while
		(!feof(file_in))
	{
		fscanf(file_in, "%d", &num1);
	}
		fclose(file_in);

	while (num1 >= 1000)
    {
      printf("M");
      num1 -= 1000;
    }
      if (num1 >= 900)
    {
      printf("CM");
      num1 -= 900;
    }
      if (num1 >= 500)
    {
      printf("D");
      num1 -= 500;
    }
      if (num1 >= 400)
    {
      printf("CD");
      num1 -= 400;
    }
      while (num1 >= 100)
    {
      printf("C");
      num1 -= 100;
    }
      if (num1 >= 90)
    {
      printf("XC");
      num1 -= 90;
    }
      if (num1 >= 50)
    {
      printf("L");
      num1 -= 50;
    }
      if (num1 >= 40)
    {
      printf("XL");
      num1 -= 40;
    }
      while (num1 >= 10)
    {
      printf("X");
      num1 -= 10;
    }
      if (num1 >= 9)
    {
      printf("IX");
      num1 -= 9;
    }
      if (num1 >= 5)
    {
      printf("V");
      num1 -= 5;
    }
      if (num1 >= 4)
    {
      printf("IV");
      num1 -= 4;
    }
      while (num1 >= 1)
    {
      printf("I");
      num1 -= 1;
    }

	  {
	  printf("Please wait whilst the Arabic Numbers are converted \n\n");
	  printf("Please enter the name of the file to which the Roman Numerals will be written to: \n");
	  file_out=fopen("arabicnumdocout.txt", "w");
	  fprintf(file_out, "%d\n", num1);
	  fclose(file_out);
	  }

	  printf("The conversion is now complete, thank you for using my program\n\n");

}
		
//everything from here on is the part of the program the user will see. A prompt appears asking the user to choose
//between inputting numbers themselves, or running batch mode.

void main()

{
	int userchoice;

do
{
	printf("Welcome to the numeral conversion program. Please press 1 to enter the Arabic Number you wish to convert, or 2 to convert a list of numbers: \n");
	scanf("%d", &userchoice);
}
while ((userchoice != 1) && (userchoice != 2));

if (userchoice == 1)
UserConvert();
else if (userchoice == 2)
BatchConvert();
else;
}
 

SheepCow

Bringer of Code
Joined
Dec 22, 2003
Messages
1,365
I've only glanced at your code so there may be other things, but;

Code:
*file_in=fopen("arabicnumdoc.txt", "r");

Is wrong. fopen() returns a pointer to a "FILE" structure, so you should dump the dereference *. I'm surprised this doesn't blat out a warning at the very least about indirection
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Thanks SheepCow. I took out the * and it seems to have picked up the function, but i get an error message now when it runs:

f_CErrorm_03a1bee.png

an error which i've never seen before. Will google it now.

edit - Googled, can't find anything on it. Surely Stream not equaling Null is a good thing?
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Code:
        int num1;
	int arabicnumdoc[20];
	FILE *file_in;
	FILE *file_out;

	printf("Please enter the name of the file you wish to be converted: \n");
	scanf("%s", &arabicnumdoc[]);

is now the beginning of the batch function. it now crashes after i've entered the name of the file that is being read from.
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Okay, i think i'm trying to print char's as int's into romnumout.txt. Can anyone tell me how i could convert num1 to char? pleaaaaaaaaaase!
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Okay, i'm getting somewhere
Code:
void BatchConvert(void)
{
	int num1;
	int arabicnumdoc[20];
	FILE *file_in;
	FILE *file_out;

	printf("Please enter the name of the file you wish to be converted: \n");
	scanf("%s", &arabicnumdoc);

	{
	file_in=fopen("arabicnumdoc.txt", "r");

	while
		(!feof(file_in))
	{
		fscanf(file_in, "%d", &num1);
	}
		fclose(file_in);
	}

	 printf("Please enter the name of the file to which the Roman Numerals will be written to: \n");//Line asking the user to enter the name of the batch file to be written to.
	 file_out=fopen("romannumout.txt", "w");//opens the file named 'romannumout.txt' to be written into

	while (num1 >= 1000)
    {
      fprintf(file_out, "M");
      num1 -= 1000;
    }
      if (num1 >= 900)
    {
      fprintf(file_out, "CM");
      num1 -= 900;
    }
      if (num1 >= 500)
    {
      fprintf(file_out, "D");
      num1 -= 500;
    }
      if (num1 >= 400)
    {
      fprintf(file_out, "CD");
      num1 -= 400;
    }
      while (num1 >= 100)
    {
      fprintf(file_out, "C");
      num1 -= 100;
    }
      if (num1 >= 90)
    {
      fprintf(file_out, "XC");
      num1 -= 90;
    }
      if (num1 >= 50)
    {
      fprintf(file_out, "L");
      num1 -= 50;
    }
      if (num1 >= 40)
    {
      fprintf(file_out, "XL");
      num1 -= 40;
    }
      while (num1 >= 10)
    {
      fprintf(file_out, "X");
      num1 -= 10;
    }
      if (num1 >= 9)
    {
      fprintf(file_out, "IX");
      num1 -= 9;
    }
      if (num1 >= 5)
    {
      fprintf(file_out, "V");
      num1 -= 5;
    }
      if (num1 >= 4)
    {
      fprintf(file_out, "IV");
      num1 -= 4;
    }
      while (num1 >= 1)
    {
      fprintf(file_out, "I");
      num1 -= 1;
    }

	
	  {
	  fclose(file_out);//closes romannumout.txt
	  }

	  printf("The conversion is now complete, thank you for using my program\n\n"); //Prints a line confirming the program has completed its task successfully.

}

It now prints to the file, but only the first digit from the list. IE - the first number in the list written in was 42, it wrote IV into the new file and nothing else. Any ideas?
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Code:
#define _CRT_SECURE_NO_WARNINGS //I have added this line because of an error I was getting regarding scanf and printf. I hadn't seen the error before so i looked it up, and apparently it is a compatibility issue.
#include<stdio.h>
#include<stdlib.h>

void UserConvert(); //Function prototypes.
void BatchConvert();

void UserConvert(void) //This function converts Arabic Numbers into Roman Numerals.
{
  int num1; //This variable holds the number that is inputted.

  printf("You have chosen to input the Arabic Number you wish to convert yourself. Please enter an Arabic Number: \n"); //A line of text asking the user to input a number.
  scanf("%i", &num1); //This line of code reads the inputted number and maps it to the variable 'num1'

    while (num1 >= 1000) //Compares the inputted number against 1000. If num1 is greater or equal to 1000, an M is added to the outputted Roman Numeral.
    {
      printf("M");
      num1 -= 1000;
    }
      if (num1 >= 900) //Compares the inputted number against 900. If num1 is greater or equal to 900, CM is added to the outputted Roman Numeral.
    {
      printf("CM");
      num1 -= 900;
    }
      if (num1 >= 500) //Compares the inputted number against 500. If num1 is greater or equal to 500, a D is added to the outputted Roman Numeral.
    {
      printf("D");
      num1 -= 500;
    }
      if (num1 >= 400) //Compares the inputted number against 400. If num1 is greater or equal to 400, CD is added to the outputted Roman Numeral.
    {
      printf("CD");
      num1 -= 400;
    }
      while (num1 >= 100) //Compares the inputted number against 100. If num1 is greater or equal to 100, a C is added to the outputted Roman Numeral.
    {
      printf("C"); //I have used a while loop rather than a for loop for 'C' as it is a letter than can appear more than once in the final numeral.
      num1 -= 100;
    }
      if (num1 >= 90) //Compares the inputted number against 90. If num1 is greater or equal to 90, XC is added to the outputted Roman Numeral.
    {
      printf("XC");
      num1 -= 90;
    }
      if (num1 >= 50) //Compares the inputted number against 50. If num1 is greater or equal to 50, an L is added to the outputted Roman Numeral.
    {
      printf("L");
      num1 -= 50;
    }
      if (num1 >= 40) //Compares the inputted number against 40. If num1 is greater or equal to 40, XL is added to the outputted Roman Numeral.
    {
      printf("XL");
      num1 -= 40;
    }
      while (num1 >= 10) //Compares the inputted number against 1. If num1 is greater or equal to 10, an X is added to the outputted Roman Numeral.
    {
      printf("X");
      num1 -= 10;
    }
      if (num1 >= 9) //Compares the inputted number against 9. If num1 is greater or equal to 9, IX is added to the outputted Roman Numeral.
    {
      printf("IX");
      num1 -= 9;
    }
      if (num1 >= 5) //Compares the inputted number against 5. If num1 is greater or equal to 5, a V is added to the outputted Roman Numeral.
    {
      printf("V");
      num1 -= 5;
    }
      if (num1 >= 4) //Compares the inputted number against 4. If num1 is greater or equal to 4, IV is added to the outputted Roman Numeral.
    {
      printf("IV");
      num1 -= 4;
    }
      while (num1 >= 1) //Compares the inputted number against 1. If num1 is greater or equal to 1, an I is added to the outputted Roman Numeral.
    {
      printf("I");
      num1 -= 1;
    }
} //The function works by running the inputted number through the loop once. If you were to enter 10 it would go through all the loops until it reached X.
  //If you were to enter 550 it would stop at 500, add a D then continue untill it found the next matching parameter at 50 and add L.

void BatchConvert(void)
{
	int num1;
	int arabic[20];
	char roman[100];
	FILE *file_in;
	FILE *file_out;

	printf("Please enter the name of the file you wish to be converted: \n");
	scanf("%s", &arabic);

	{
	file_in=fopen("arabic.txt", "r");

	while (!feof(file_in))
	{
		fscanf(file_in, "%d", &num1);//loads
	}
		fclose(file_in);//closes arabic.txt
	}

	 printf("Please enter the name of the file to which the Roman Numerals will be written to: \n");//Line asking the user to enter the name of the batch file to be written to.
	 scanf("%s", &roman);
	 file_out=fopen("roman.txt", "w");//opens the file named 'roman.txt' to be written into

	while (num1 >= 1000)
    {
      fprintf(file_out, "M");//Prints an M to file_out if num1 is greater or equal to 1000.
      num1 -= 1000;
    }
      if (num1 >= 900)
    {
      fprintf(file_out, "CM");//Prints CM to file_out if num1 is greater or equal to 900.
      num1 -= 900;
    }
      if (num1 >= 500)
    {
      fprintf(file_out, "D");//Prints a D to file_out if num1 is greater or equal to 500.
      num1 -= 500;
    }
      if (num1 >= 400)
    {
      fprintf(file_out, "CD");//Prints CD to file_out if num1 is greater or equal to 40.
      num1 -= 400;
    }
      while (num1 >= 100)
    {
      fprintf(file_out, "C");//Prints a C to file_out if num1 is greater or equal to 100.
      num1 -= 100;
    }
      if (num1 >= 90)
    {
      fprintf(file_out, "XC");//Prints XC to file_out if num1 is greater or equal to 90.
      num1 -= 90;
    }
      if (num1 >= 50)
    {
      fprintf(file_out, "L");//Prints an L to file_out if num1 is greater or equal to 50.
      num1 -= 50;
    }
      if (num1 >= 40)
    {
      fprintf(file_out, "XL");//Prints XL to file_out if num1 is greater or equal to 40.
      num1 -= 40;
    }
      while (num1 >= 10)
    {
      fprintf(file_out, "X");//Prints an X to file_out if num1 is greater or equal to 10.
      num1 -= 10;
    }
      if (num1 >= 9)
    {
      fprintf(file_out, "IX");//Prints IX to file_out if num1 is greater or equal to 9.
      num1 -= 9;
    }
      if (num1 >= 5)
    {
      fprintf(file_out, "V");//Prints a V to file_out if num1 is greater or equal to 5.
      num1 -= 5;
    }
      if (num1 >= 4)
    {
      fprintf(file_out, "IV");//Prints IV to file_out if num1 is greater or equal to 4.
      num1 -= 4;
    }
      while (num1 >= 1)
    {
      fprintf(file_out, "I");//Prints an I to file_out if num1 is greater or equal to 1.
      num1 -= 1;
    }

	  {
	  fclose(file_out);//closes roman.txt
	  }

	  printf("The conversion is now complete, thank you for using my program\n\n"); //Prints a line confirming the program has completed its task successfully.

}
		
//everything from here on is the part of the program the user will see. A prompt appears asking the user to choose
//between inputting numbers themselves, or running batch mode.

void main()

{
	int userchoice; //This integer corresponds to the decision made by the user to run the batchconvert function or the userconvert function.

do
{
	printf("Welcome to the numeral conversion program. Please press 1 to enter the Arabic Number you wish to convert, or 2 to convert a list of numbers: \n");
	scanf("%d", &userchoice);
}
while ((userchoice != 1) && (userchoice != 2)); //This line ensures that if anything other than 1 or 2 is entered the loop will go round again untill a correct number is entered.

if (userchoice == 1)
UserConvert(); //If the user enters a number 1, this line calls the function 'UserConvert' and runs it.
else if (userchoice == 2)
BatchConvert(); //If the user enters a number 1, this line calls the function 'BatchConvert' and runs it.
else;
}
 

phlash

Fledgling Freddie
Joined
Dec 24, 2003
Messages
195
OK: main() is mostly fine, UserConvert() also ok but should be refactored to re-use the algorithm, BatchConvert() has several problems, here's a version that works for me, I've commented the important changes with 'PAA':
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_ARABIC_NUM	10000	// Otherwise we could get a *lot* of M's :)
#define MAX_ROMAN_SIZE	80	// Given above limit this can contain roman text

void UserConvert(); //Function prototypes.
void BatchConvert();
char *DoConversion(int num1);

char *DoConversion(int num1) //This function converts an Arabic Number into Roman Numerals.
{
    static char buf[MAX_ROMAN_SIZE];

    buf[0] = 0;	// Empty output buffer

    if (num1 > MAX_ARABIC_NUM) // Limit input number, skip duff ones
    {
        fprintf(stderr, "Invalid arabic number (too large): %d\n", num1);
        return buf;
    }

    while (num1 >= 1000) //Compares the inputted number against 1000. If num1 is greater or equal to 1000, an M is added to the outputted Roman Numeral.
    {
      strcat(buf, "M");
      num1 -= 1000;
    }
      if (num1 >= 900) //Compares the inputted number against 900. If num1 is greater or equal to 900, CM is added to the outputted Roman Numeral.
    {
      strcat(buf, "CM");
      num1 -= 900;
    }
      if (num1 >= 500) //Compares the inputted number against 500. If num1 is greater or equal to 500, a D is added to the outputted Roman Numeral.
    {
      strcat(buf, "D");
      num1 -= 500;
    }
      if (num1 >= 400) //Compares the inputted number against 400. If num1 is greater or equal to 400, CD is added to the outputted Roman Numeral.
    {
      strcat(buf, "CD");
      num1 -= 400;
    }
      while (num1 >= 100) //Compares the inputted number against 100. If num1 is greater or equal to 100, a C is added to the outputted Roman Numeral.
    {
      strcat(buf, "C"); //I have used a while loop rather than a for loop for 'C' as it is a letter than can appear more than once in the final numeral.
      num1 -= 100;
    }
      if (num1 >= 90) //Compares the inputted number against 90. If num1 is greater or equal to 90, XC is added to the outputted Roman Numeral.
    {
      strcat(buf, "XC");
      num1 -= 90;
    }
      if (num1 >= 50) //Compares the inputted number against 50. If num1 is greater or equal to 50, an L is added to the outputted Roman Numeral.
    {
      strcat(buf, "L");
      num1 -= 50;
    }
      if (num1 >= 40) //Compares the inputted number against 40. If num1 is greater or equal to 40, XL is added to the outputted Roman Numeral.
    {
      strcat(buf, "XL");
      num1 -= 40;
    }
      while (num1 >= 10) //Compares the inputted number against 1. If num1 is greater or equal to 10, an X is added to the outputted Roman Numeral.
    {
      strcat(buf, "X");
      num1 -= 10;
    }
      if (num1 >= 9) //Compares the inputted number against 9. If num1 is greater or equal to 9, IX is added to the outputted Roman Numeral.
    {
      strcat(buf, "IX");
      num1 -= 9;
    }
      if (num1 >= 5) //Compares the inputted number against 5. If num1 is greater or equal to 5, a V is added to the outputted Roman Numeral.
    {
      strcat(buf, "V");
      num1 -= 5;
    }
      if (num1 >= 4) //Compares the inputted number against 4. If num1 is greater or equal to 4, IV is added to the outputted Roman Numeral.
    {
      strcat(buf, "IV");
      num1 -= 4;
    }
      while (num1 >= 1) //Compares the inputted number against 1. If num1 is greater or equal to 1, an I is added to the outputted Roman Numeral.
    {
      strcat(buf, "I");
      num1 -= 1;
    }
    return buf;
}

void UserConvert()
{
  int num1;

  printf("You have chosen to input the Arabic Number you wish to convert yourself. Please enter an Arabic Number: \n"); //A line of text asking the user to input a number.
  scanf("%i", &num1); //This line of code reads the inputted number and maps it to the variable 'num1'
  printf("Roman number: %s\n", DoConversion(num1));

}

void BatchConvert(void)
{
	int num1;
	char arabic[20], roman[20];
	FILE *file_in;
	FILE *file_out;

	fflush(stdin); // PAA: Make sure there is nothing in the input buffer.

	printf("Please enter the name of the file you wish to be converted: \n");
	fgets(arabic, sizeof(arabic), stdin); // PAA: Much safer than scanf()
	arabic[strlen(arabic)-1] = 0; // PAA: Trim newline char

	file_in=fopen(arabic, "r"); // PAA: actually use file name :)

	if (!file_in)
	{
		perror("opening arabic input file");
		return;
	}

	printf("Please enter the name of the file to which the Roman Numerals will be written to: \n");//Line asking the user to enter the name of the batch file to be written to.
	fgets(roman, sizeof(roman), stdin); // PAA: Much safer than scanf()
	roman[strlen(roman)-1] = 0; // PAA: trim newline

	file_out=fopen(roman, "w"); // PAA: Actually use file name :)

	if (!file_out)
	{
		perror("opening roman output file");
		return;
	}

	while (!feof(file_in))
	{
		// PAA: Do the conversion in a loop while reading input file..
		// PAA: note reuse of DoConversion() function - much neater!
		fscanf(file_in, "%d ", &num1); // PAA: space after number consumes whitespace
		fprintf(file_out, "%d=%s\n", num1, DoConversion(num1));
	}
	fclose(file_in);
	fclose(file_out);

	printf("The conversion is now complete, thank you for using my program\n\n"); //Prints a line confirming the program has completed its task successfully.

}
		
//everything from here on is the part of the program the user will see. A prompt appears asking the user to choose
//between inputting numbers themselves, or running batch mode.

int main() // PAA: main should return an exit code

{
	int userchoice; //This integer corresponds to the decision made by the user to run the batchconvert function or the userconvert function.

	do
	{
		char buf[20];
		userchoice = -1; // PAA: known initial value incase parse fails

		printf("Welcome to the numeral conversion program. Please press 1 to enter the Arabic Number you wish to convert, or 2 to convert a list of numbers: \n");
		fgets(buf, sizeof(buf), stdin); // Much safer than scanf()
		sscanf(buf, "%d", &userchoice);
	}
	while ((userchoice != 1) && (userchoice != 2)); //This line ensures that if anything other than 1 or 2 is entered the loop will go round again untill a correct number is entered.

	if (userchoice == 1)
		UserConvert(); //If the user enters a number 1, this line calls the function 'UserConvert' and runs it.
	else
		BatchConvert(); //If the user enters a number 1, this line calls the function 'BatchConvert' and runs it.
	return 0; // PAA: exit 0 on success
}
 

SheepCow

Bringer of Code
Joined
Dec 22, 2003
Messages
1,365
Code:
        int num1;
	int arabicnumdoc[20];
	FILE *file_in;
	FILE *file_out;

	printf("Please enter the name of the file you wish to be converted: \n");
	scanf("%s", &arabicnumdoc[]);

is now the beginning of the batch function. it now crashes after i've entered the name of the file that is being read from.


&arabicnumdoc is a pointer tp the start of the array, you don't need the []
 

Chilly

Balls of steel
Joined
Dec 22, 2003
Messages
9,047
I already fixed it :p What mark did you get aoami you 'tard?
 

Users who are viewing this thread

Top Bottom