Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

can anyone help me out????

kkhankkhan Member Posts: 65
please have a look at my code and suggest reasons why it doesnt work..ill be really greatful.



#include
#include
#include

// declaration of functions for the program
void instructUser(void);
int getRomanAndConvertToInt(void);
void printInt(int num);
char getOp(void);
int doOp (int num1,int num2,char op);
void printtRoman(int result);
char askAboutAnotherCalculation(void);

main()
{
int num1=0, num2=0,result=0;
char response=0,op=0;

instructUser();

do
{
num1= getRomanAndConvertToInt();
printInt(num1);

num2= getRomanAndConvertToInt();
printInt(num1);
op=getOp();
}while( response=='Y');
}

void instructUser()
{
printf("This program does arithmetic calculations using additive Roman numeral
");
printf("notation. The program will repeatedly prompt you to enter 2 Roman
");
printf("numeral numbers and an operator and then calculate and print the result.
");
printf("In entering a Roman numeral do not leave any space in front of
");
printf("the number, use only the letters M(=1000),D(=500),C(=100),L(=
");
printf("50),X(=10),V(=5),I(=1) in uppercase and in that order, and follow
");
printf("the number immediately by a . In entering the operator do not
");
printf("leave any space before the operator. Enter either +,-,* as the operator,
");
printf("and immediately follow the operator by a . Similarly when
");
printf("responding to the question about whether you want to do another
");
printf("calculation enter a Y or N in uppercase with no space in front and
");
printf("immediately followed by a .

");
}



int getRomanAndConvertToInt(void)
{
char ch;
int num=0, numError=0;

do{

printf("Enter a Roman numeral number >");
ch=getchar();
ch=toupper(ch);
while(ch != '
')
{
switch(ch)
{
case 'M': num+=1000;
break;
case 'D': num+=500;
break;
case 'C': num+=100;
break;
case 'L': num+=50;
break;
case 'X': num+=10;
break;
case 'V': num+=5;
break;
case 'I': num+=1;
break;
case ' ': break;
case '
': break;
default: numError+=1;
break;
}
}
if(numError>0)
{
printf(" Error - input contains %d incorrect
",numError);
printf("character(s)
");
}

}while(numError>0);
return num;
}

void printInt( int num)
{
printf("
=%d",num);
}

char getOp(void)
{
char t;
do{
printf("Enter the desired operator >");
t=getchar();
getchar();
if(t!='+'|| t!='-'|| t!='*')
printf(" Error %c is not an operator.

");
}while(t!='+'|| t!='-'|| t!='*');
return t;
}

int doOp( int num1, int num2, char op)
{
int result = 0;
switch(op){
case '+': result=num1+num2;
break;
case '*': result=num1*num2;
break;
case '-': result=num1-num2;
break;
default: break;
}
return result;
}

void printRoman(int result)
{
int i,j,k,p,r,s,n,q,numMs,numDs,numCs,numLs,numXs,numIs,numVs;

n=result;

numMs=n/1000;

n=n%1000;

printf("The Roman result = ");

for(i=1;i<=numMs;i++)

printf(" M");

numDs=n/500;

n=n%500;

for(j=1;j<=numDs;j++)

printf("D");

numCs=n/100;

n=n%100;

for(k=1;k<=numCs;k++)

printf("C");

numLs=n/50;

n=n%50;

for(p=1;p<=numLs;p++)

printf("L");

numXs=n/10;

n=n%10;

for(r=1;r<=numXs;r++)

printf("X");

numVs=n/5;

n=n%5;

for(s=1;s<=numVs;s++)

printf("V");

numIs=n/1;

n=n%1;

for(q=1;q<=numIs;q++)

printf("I");

printf("
=%d",result);
}

char askAboutAnotherCalculation(void)
{
char send;
printf("Do another calculation >");
send=getchar();
return send;
}


Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    Use the debugger and you will see why it does not work. Here is a solution for the first part:
    [code]
    int getRomanAndConvertToInt(void)
    {
    char ch;
    int num=0, numError=0;

    printf("Enter a Roman numeral number >");
    while(ch != '
    ')
    {
    ch=getchar();
    ch=toupper(ch);
    switch(ch)
    {
    case 'M': num+=1000;
    break;
    case 'D': num+=500;
    break;
    case 'C': num+=100;
    break;
    case 'L': num+=50;
    break;
    case 'X': num+=10;
    break;
    case 'V': num+=5;
    break;
    case 'I': num+=1;
    break;
    case ' ': break;
    case '
    ': break;
    default: numError+=1;
    break;
    }
    if(numError>0)
    {
    printf(" Error - input contains %d incorrect
    ",numError);
    printf("character(s)
    ");
    }

    }
    return num;
    }

    [/code]

  • kkhankkhan Member Posts: 65
    thanks alot dude.
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : thanks alot dude.
    :
    [blue]According to code I see both "XI" and "IX" is 11, but "IX" is actually 9.[/blue]
  • kkhankkhan Member Posts: 65
    [b][red]This message was edited by kkhan at 2003-2-21 8:43:42[/red][/b][hr]
    ill look into what u are saying
    actually the original program was to make roman numeral calculator, that would would add subtract, multiply and convert the roman numerals into regular numbers. Then we had to an extension of it which asked us to do the following. I tried the modifications but for some reason the program would compile perfectly but would not run after the first input and just freeze. thats why i needed a diff perspective on the matter.also if u look at my original code getchar wouldnt even take the characters, always returning a 0...dont know why???


    Extend program 4 to be more user friendly and to do error checking on
    the input. The program should be made more user friendly by modifying
    it to accept input in either lower or upper case. To accomplish this
    use the function toupper(c) in the character handling library (use
    #include ). This function has a character as argument and
    returns the character in uppercase if it is a lowercase letter and
    unchanged otherwise. Each time a character is inputted in one of the
    functions, getRomanConvertToInt and doAnother, call on this function
    to make sure the resulting character is in uppercase. Thus if the
    user enters a Roman numeral number as mDcxXvi the function should
    accept it and return the integer value 1626. Similarly if the user
    enters 'n' instead of 'N', the function doAnother should accept this
    and return 0 (FALSE). Please modify the user instructions outputted
    to delete any mention of uppercase, since the program is now prepared
    to accept either upper or lower case.

    The program should do error checking on the input as follows.

    1)For function getRomanConvertToInt, if the input contains any
    characters other than M, D, C, L, X, V, I, ' ' (or their lowercase
    equivalents) then output an error message and prompt for and input
    another Roman numeral number. Hint - you may have to do the prompting
    for and inputting of the Roman numeral number an indefinite number of
    times so this should be done using a loop. This could be done by
    enclosing the entire process inside a do while loop. Use a variable
    numErrors which is set to the number of incorrect characters during
    the input. Also just for experience while you are making this
    modification to the function change the entire if-else statement into
    a switch statement. Besides the regular characters allow the space
    character ' ' as not being an error, i.e. if the user types the space
    character while entering the Roman numeral number this will not be
    considered an error, but will simply be ignored. An example of the
    interactive I/O is
    Enter a Roman number: mccxzvi
    Error - input contains 1 incorrect
    character(s)
    Enter a Roman number: mcfxbi
    Error - input contains 2 incorrect
    character(s)
    Enter a Roman number: mD cx Xvi
    = 1626
    2)For function getOp if any character other than +, -, * is entered
    then output an error message and prompt for and get another character
    for the operator. An example of the interactive I/O is
    Enter operator(+,-,*): 8
    Error - 8 is not an operator

    Enter operator(+,-,*): x
    Error - x is not an operator

    Enter operator(+,-,*): *

    3)For function printRoman, if the value of the parameter result is
    less than or equal to 0 (this can only happen if the operator is -
    and the second number is greater than the first), then do not try to
    convert it to a roman numeral. Instead just output that it is
    undefined. For example if reult had the value -17 then the output
    should be
    The Roman result = undefined
    = -17



Sign In or Register to comment.