Hi all

I've only been learning C for 6 weeks and have a project to convert arabic numbers to roman numerals.

Part of the prgram must do this in batch mode. i.e. read integers from .txt file, convert and write numerals back to .txt file.

It seems to be working ok, however the ouput is mixed up.

Reading integers from .txt file as follows:
-------------------------------------------------
1
4
10
-------------------------------------------------

When converted and appended back to the file I get:

-------------------------------------------------

1
4
10

The value: 10 = X
The value: 1 = I
The value: 4 = IV

-------------------------------------------------

So it is appending the last integer first, then the rest.

It is obviously something I am doing wrong in the loop at the end.

Can anyone advise me please?

Code follows:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void convert(int, char); //declaration of function to convert numerals

void convert(int value, char roman[50])

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



int main ()
{
    int arabic, count, value; 
	char roman[50];
	char filename[20];
	FILE *file_in;
	FILE *file_out;
    
	value = 0;
	count = 0;
		
	printf ("Please enter the filename including extension.\n(example: filename.txt) : "); //asks user for filename to open
	scanf ("%s",filename);

	file_in = fopen (filename,"r"); //open the input stream

	if (file_in == NULL) 
	{
    printf ("Error: cannot open file.\n");
    return 1;
	}

	else 
	{
    printf ("File opened.\n");
	}
	
	while (fscanf(file_in,"%d",&value)!=EOF)
	{

	for (count=0; count<50; count++) //sets array to zero
	{
	roman[count] = 0;
	}
			
	arabic = value;
	
	convert(value,roman); //function to convert numerals
	
	file_out = fopen (filename,"a"); //open the output stream
	fprintf (file_out, "\nThe value: %d = %s", arabic, roman); //append conversion to file
	
	}

	fclose (file_in);
	fclose (file_out);
}

1. Your indentation sucks.
A consistently applied indentation style will save you from no end of disasters (and confusion) in the long run.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void convert(int, char); //declaration of function to convert numerals

void convert(int value, char roman[50])

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



int main ()
{
    int arabic, count, value; 
    char roman[50];
    char filename[20];
    FILE *file_in;
    FILE *file_out;
    
    value = 0;
    count = 0;
    
    printf ("Please enter the filename including extension.\n(example: filename.txt) : "); //asks user for filename to open
    scanf ("%s",filename);

    file_in = fopen (filename,"r"); //open the input stream

    if (file_in == NULL) 
    {
        printf ("Error: cannot open file.\n");
        return 1;
    }

    else 
    {
        printf ("File opened.\n");
    }
    
    while (fscanf(file_in,"%d",&value)!=EOF)
    {

        for (count=0; count<50; count++) //sets array to zero
        {
            roman[count] = 0;
        }
        
        arabic = value;
        
        convert(value,roman); //function to convert numerals
        
        file_out = fopen (filename,"a"); //open the output stream
        fprintf (file_out, "\nThe value: %d = %s", arabic, roman); //append conversion to file
        
    }

    fclose (file_in);
    fclose (file_out);
}

2. You're using the same file for input and output
file_out = fopen (filename,"a");

3. You open the output file repeatedly INSIDE the loop.

1. Yes that's true. I'll try to indent better in the future.

2. I wanted to use the same file for input and output, just append the output to it.

3. Yes that was the trouble, openeing the stream constantly. It's fixed now. Many thanks :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.