Answered You can hire a professional tutor to get the answer.

QUESTION

Hello.txt file through my code using stdin.

Hello. I need help trying to read a .txt file through my code using stdin. Right now I have the user enter their own input so they can add 2 Roman numerals together to give an output of a Roman Numeral. For example, if a user entered III for the first line and VI for the second line, it will equal the sum of IX. My code is able to add Roman Numerals correctly. However I need it where a user can enter a .txt file with the same Roman numerals; III and VI and have it display the sum using stdin and fgets. Here is the code below:

#include<stdio.h>

#include<string.h>

// List all of the Roman Numerals with their values

int val(char r){

if (r == 'I')

   return 1;

if (r == 'V')

   return 5;

if (r == 'X')

   return 10;

if (r == 'L')

   return 50;

if (r == 'C')

   return 100;

if (r == 'D')

   return 500;

if (r == 'M')

   return 1000;

return -1;

}

// This function will change the Roman Numerals into Numbers

int romanToDecimal(char arr[]){

int res = 0;

int i,len = strlen(arr);

for (i=0; i<len; i++){

   int s1 = val(arr[i]);

   if (i+1 < len){

       int s2 = val(arr[i+1]);

   if (s1 >= s2)

       res = res + s1;

   else{

       res = res + s2 - s1;

       i++;

   }

}

   else{

       res = res + s1;

       i++;

   }

}

return res;

}

int sub_digit(char num1, char num2, int i, char *c){

c[i++] = num1;

c[i++] = num2;

return i;

}

int digit(char ch, int n, int i, char *c){

int j;

for (j = 0; j < n; j++)

   c[i++] = ch;

return i;

}

// Function will turn Numbers into Roman Numerals

void printInRoman(int num){

char c[10001];

int i = 0;

   while (num != 0){

       if (num >= 1000){

           i = digit('M', num/1000, i, c);

           num = num%1000;

   }

       else if (num >= 500){

           if (num < 900){

               i = digit('D', num/500, i, c);

               num = num%500;

           }

           else{

               i = sub_digit('C', 'M', i, c);

               num = num%100 ;

           }

       }

       else if (num >= 100){

           if (num < 400){

               i = digit('C', num/100, i, c);

               num = num%100;

           }

           else{

               i = sub_digit('C','D',i,c);

               num = num%100;

           }

       }

       else if (num >= 50 ){

           if (num < 90){

               i = digit('L', num/50,i,c);

               num = num%50;

           }

           else{

               i = sub_digit('X','C',i,c);

               num = num%10;

           }

       }

       else if (num >= 10){

           if (num < 40){

               i = digit('X', num/10,i,c);

               num = num%10;

           }

           else{

               i = sub_digit('X','L',i,c);

               num = num%10;

           }

       }

       else if (num >= 5){

           if (num < 9){

               i = digit('V', num/5,i,c);

               num = num%5;

           }

           else{

               i = sub_digit('I','X',i,c);

               num = 0;

           }

       }

       else if (num >= 1){

           if (num < 4){

               i = digit('I', num,i,c);

               num = 0;

           }

           else{

               i = sub_digit('I', 'V', i, c);

               num = 0;

           }

       }

   }

int j;

for (j = 0; j < i; j++)

   printf("%c", c[j]);

   printf("n");

}

int main(int argc, char *argv[]){

   char arr1[100];

   char arr2[100];

   char fileName[100];

   printf("Enter file namen");

   scanf("%s",fileName);

   FILE* file = fopen(fileName, "r");

   fgets(arr1, 100, file);

   fgets(arr2, 100, file);

   int val1 = romanToDecimal(arr1);

   int val2 = romanToDecimal(arr2);

   printInRoman(val1+val2);

return 0;

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question