Many thanks for all your help. I have decided to steer clear of functions - don't understand parsing/passing well enough yet...
What I have done seems to work, although it is more scrappy... The only thing that doesn't work is reading the file in but I don't not whether thats how its being executed or the code.
Many thanks for all your help. When I get a little better and can help others I surely will...
Code:
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
char tab[16];
int REP;
int i;
int z;
int total;
int c;
int unreal = 0;
FILE *file_in;
FILE *file_out;
int CountM, CountD, CountC, CountL, CountX, CountV, CountI, CountCM, CountCD, CountXC, CountXL, CountIX, CountIV;
CountM = CountD = CountC = CountL = CountX = CountV = CountI = CountCM = CountCD = CountXC = CountXL = CountIX = CountIV = 0;
for (z=0; z<=15; ++z)
{
tab[z] = 0;
}
printf("\nChoose 1 for FILE input");
printf("\nChoose 2 for KEYBOARD input: ");
do
{
scanf("%i", &c);
}
while ((c <=0) || (c >=3));
if (c == 1)
{
printf("You have chosen one - file input"); //STILL NEED TO FIGURE OUT CODE TO INPUT A STRING VIA A FILE
{
file_in = fopen("Roman.txt","r");
fscanf(file_in, "%s", tab);
fclose(file_in);
return 0;
}
}
else if (c == 2)
{
printf("You have chosen two - file input"); // (KEYBOARD MODE) ASK FOR NUMERALS AND COPY INTO "LETTERS"
printf("\nPlease input your ROMAN NUMERALS from the keyboard: ");
scanf("%s", tab);
}
for (i=0; tab[i] != '\0';i++)
switch (tab[i])
{
case 'M':
REP = 4;
CountM ++;
if (CountM > REP)
{
printf("ERROR: You have input too many M's");
unreal = 1;
}
break;
case 'D':
REP = 1;
CountD ++;
if (CountD > REP)
{
printf("ERROR: You have input too many D's");
unreal = 1;
}
break;
case 'C':
i++;
REP = 3;
CountC ++;
if (CountC > REP)
{
printf("ERROR: You have input too many C's");
unreal = 1;
}
REP = 1;
switch (tab[i])
{
case '\0':
break;
case 'M':
i++;
CountC --; CountCM ++;
if (CountCM > REP)
{
printf("ERROR: You have input too many CM's");
unreal = 1;
}
break;
case 'D':
i++;
CountC --; CountCD ++;
if (CountCD > REP)
{
printf("ERROR: You have input too many CD's");
unreal = 1;
}
break;
}i --;
break;
case 'L':
REP = 1;
CountL ++;
if (CountL > REP)
{
printf("ERROR: You have input too many L's");
unreal = 1;
}
break;
case 'X':
i++;
REP = 3;
CountX ++;
if (CountX > REP)
{
printf("ERROR: You have input too many X's");
unreal = 1;
}
REP = 1;
switch (tab[i])
{
case '\0':
break;
case 'C':
i++;
CountX -- ; CountXC ++;
if (CountXC > REP)
{
printf("ERROR: You have input too many XC's");
unreal = 1;
}
break;
case 'L':
i++;
CountX -- ; CountXL ++;
if (CountXL > REP)
{
printf("ERROR: You have input too many XL's");
unreal = 1;
}
break;
}i--;
break;
case 'V':
REP = 1;
++ CountV;
if (CountV > REP)
{
printf("ERROR: You have input too many V's");
unreal = 1;
}
break;
case 'I':
i++;
REP = 3;
++ CountI;
if (CountI > REP)
{
printf("ERROR: You have input too many I's");
unreal = 1;
}
REP = 1;
switch (tab[i])
{
case '\0':
break;
case 'X':
i++;
CountI -- ; CountIX ++ ;
if (CountIX > REP)
{
printf("ERROR: You have input too many IX's");
unreal = 1;
}
break;
case 'D':
i++;
CountI --; CountIV ++ ;
if (CountIV > REP)
{
printf("ERROR: You have input too many IV's");
unreal = 1;
}
break;
}i--;
break;
default:
printf("You have input an invalid character");
}
//CALCULATE THE TOTAL OF ALL THE COUNTS MULTIPLIED AGAINST THEIR EQUIVALENT ARABIC VALUE
total = ((CountM*1000) + (CountD*500) + (CountC*100) + (CountL*50) + (CountX*10)
+ (CountV*5) + (CountI) + (CountCM*900) + (CountCD*400) + (CountXC*90) + (CountXL*40)
+ (CountIX*9) + (CountIV*4));
if (c == 1)
{
printf("You have chosen one - file input"); //STILL NEED TO FIGURE OUT CODE TO INPUT A STRING VIA A FILE
{
file_out = fopen("new.txt","w");
fprintf(file_out, "%i", total);
fclose(file_out);
return 0;
}
}
//PRINT THE ORIGINAL ROMAN NUMERAL AND THE ARABIC CONVERTED VALUE
printf("\n The value you input in ROMAN NUMERALS was: %s", tab);
if (unreal == 0)
printf("\n The equivalent value in Arabic numbers is: %i", total);
else
printf("\n The possible equivalent value of your BADLY CONSTRUCTED ROMAN NUMERAL in Arabic numbers is: %i", total);
}