I'm trying to build a roman numeral calculator....I'm not quite sure what happened with my code, it was working just fine and now it wont do what it's supposed to do. I've gone over line by line with a fine toothed comb and cannot seem to find where it's going wonky. Here is what I have
/* Amanda Strote
Roman Calculator
CISP 360
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h> //rand and srand is in here
#include <time.h> //contains time structure
#pragma warning (disable:4996) //turns off stdio function warnings
char num1, num2; //users 1st and 2nd entered roman numerals
int opchar; //operator characters
int yesno; //users answer to running again
int ch1, ch2;
int ans;
int opOK; //validation for the entered operator character
int again;
time_t t; //not needed for this program
void main()
{
printf("Welcome to the Roman Calculator.\n"); //welcome message
//main processing loop
do{
ch1 = ch2 = num1 = num2 = ans = 0; //initialized to zero
again = 2;
printf("Please enter your first number: ");
do{
//get user input
num1 = getche();
switch(num1)
{
case 'M':
case 'm':
ch1 += 1000;
break;
case 'D':
case 'd':
ch1 += 500;
break;
case 'C':
case 'c':
ch1 += 100;
break;
case 'L':
case 'l':
ch1 += 50;
break;
case 'X':
case 'x':
ch1 += 10;
break;
case 'V':
case 'v':
ch1 += 5;
break;
case 'I':
case 'i':
ch1 =+ 1;
break;
default:
//invalid entries will be ignored
break;
}
}while(num1 != 'Q' && num1 !='q'); //ends loop for 1st #
printf("\nFirst number = %d\n\n", ch1);
//users 2nd number
printf("Please enter your second number: ");
do{
//gets user input
num2 = getche();
switch(num2)
{
case 'M':
case 'm':
ch1 += 1000;
break;
case 'D':
case 'd':
ch1 += 500;
break;
case 'C':
case 'c':
ch1 += 100;
break;
case 'L':
case 'l':
ch1 += 50;
break;
case 'X':
case 'x':
ch1 += 10;
break;
case 'V':
case 'v':
ch1 += 5;
break;
case 'I':
case 'i':
ch1 =+ 1;
break;
default:
//invalid entries will be ignored
break;
}
}while(num2 != 'Q' && num2 !='q'); //ends loop for 2nd #
//display 2nd number in roman numerals
printf("\n Second number = %d\n", ch2);
do{ //loop that looks for valid operators
printf("\nPlease select and operation (*, /, +, -, %): ");
opchar = getche(); //get user's input
//checks for valid operation and perform's operations
switch(opchar)
{
case '+':
ans = ch1 + ch2;
opOK = true;
break;
case '-':
ans = ch1 - ch2;
opOK = true;
break;
case '*':
ans = ch1 * ch2;
opOK = true;
break;
case '/':
if(num2 ==0) //catches undefined answers
{
printf("\nCannot divide by zero. Select another operation.\n");
opOK = false;
break;
}
else
{
ans = ch1 / ch2;
opOK = true;
break;
}
defalut: //catches improper input
printf("Unidentified operation. Select another operation.\n");
opOK = false;
}
}while(opOK != true); //exits the loop when opOK becomes true
//convert and display roman numerals
printf("\n\nThe answer is %d\nRoman Numeral answer is ", ans);
if(ans < 0)
{
printf("-");
ans *= -1;
}
while(ans >= 1000)
{
printf("M");
ans -= 1000;
}
while(ans >= 500)
{
printf("D");
ans -= 500;
}
while(ans >= 100)
{
printf("C");
ans -= 100;
}
while(ans >= 50)
{
printf("L");
ans -= 50;
}
while(ans >= 10)
{
printf("X");
ans -= 10;
}
while(ans>= 5)
{
printf("V");
ans -= 5;
}
while(ans >= 1)
{
printf("I");
ans -= 1;
}
//prompts the user if they would like to run again
printf("\nDo you want to play again? (y)es or (n)o?");
do{
yesno=getche();
switch(yesno)
{
case 'Y':
case 'y':
again = 1;
break;
case 'N':
case 'n':
again = 0;
break;
default:
printf("\nNot a valid entry, please select (y)es or (n)o. ");
}
}while(again ==2); //exits loop when the user enters y or n
printf("\n\n"); //line break
}while(again==1); //ends main loop
printf("\nBye-bye!\n\n");
}
Here is what it's doing:
Welcome to the Roman Calculator. Please enter your first number: 5
And here is what its supposed to do:
Welcome to Roman Calculator. Hail Caesar!
Enter first number: MMXI
The first number is : 2011
Enter second number: VIII
The second number is : 8
Enter operation (* / + - ): +
Answer: 2019
In Roman: MMXVIIII
Press Q to quit, any other key to continue.
Enter first number: XXII
The first number is : 22
Enter second number: VII
The second number is : 7
Enter operation (* / + - ): /
Answer: 3
In Roman: III
Press Q to quit, any other key to continue. Thanks for playing!
Press any key to continue . . .
Someone said I was supposed to have had a \r in there, but I'm not sure where it would go to be honest.
Question
Terabojin
I'm trying to build a roman numeral calculator....I'm not quite sure what happened with my code, it was working just fine and now it wont do what it's supposed to do. I've gone over line by line with a fine toothed comb and cannot seem to find where it's going wonky. Here is what I have
Here is what it's doing:
Welcome to the Roman Calculator.
Please enter your first number: 5
And here is what its supposed to do:
Welcome to Roman Calculator. Hail Caesar!
Enter first number: MMXI
The first number is : 2011
Enter second number: VIII
The second number is : 8
Enter operation (* / + - ): +
Answer: 2019
In Roman: MMXVIIII
Press Q to quit, any other key to continue.
Enter first number: XXII
The first number is : 22
Enter second number: VII
The second number is : 7
Enter operation (* / + - ): /
Answer: 3
In Roman: III
Press Q to quit, any other key to continue. Thanks for playing!
Press any key to continue . . .
Someone said I was supposed to have had a \r in there, but I'm not sure where it would go to be honest.
Link to comment
Share on other sites
14 answers to this question
Recommended Posts