Get Help with C++ Homework and Code: Correct Output vs. Program Results

In summary, the program takes in two Roman numeral inputs and an arithmetic operation, and then outputs the result of the operation in Roman numerals and decimal form. It also includes error handling for invalid inputs.
  • #1
kylekn
1
0
I NEED THIS C++ CODE TO WORK.The CORRECT output (line numbers added for reference):


THIS IS WHAT IS SUPPOSED TO HAPPEN.

1: MCCXXVI
2: The first number is 1226
3: LXVIIII
4: The second number is 69
5: +
6: Arithmetic operation is +
7: The sum of 1226 and 69 is MCCLXXXXV (1295)
8:
9: DCX
10: The first number is 610
11: MCI
12: The second number is 1101
13: -
14: Arithmetic operation is -
15: The difference of 610 and 1101 is -CCCCLXXXXI (-491)
16:
17: LXVI
18: The first number is 66
19: CCLXI
20: The second number is 261
21: /
22: Arithmetic operation is /
23: The quotient of 66 and 261 is zero (0)
24:
25: MD
26: The first number is 1500
27: XXX
28: The second number is 30
29: /
30: Arithmetic operation is /
31: The quotient of 1500 and 30 is L (50)
32:
33: LXVIIII
34: The first number is 69
35: XXVIIII
36: The second number is 29
37: *
38: Arithmetic operation is *
39: The product of 69 and 29 is MMI (2001)


THIS IS WHAT I GET WHEN I RUN IT

Output generated by your program from test test1.txt (line numbers added):

1: MCCXXVI
2: LXVIIII
3: +
4: The first number is 1226
5:
6: Arithmetic operation is +
7: The sum of 1226 and 69 is MCCCCLLLLLXXXXV (1295)
8:

HERE IS MY CODE.

#include <iostream>
#include <string>
using namespace std;

const int BAD_VAL = -1;
const int HIGH_NUM = 2;

const int ROMAN_I = 1 ;
const int ROMAN_V = 5 ;
const int ROMAN_X = 10 ;
const int ROMAN_L = 50 ;
const int ROMAN_C = 100 ;
const int ROMAN_D = 500 ;
const int ROMAN_M = 1000 ;


int GetRomanNumeral ();
int RomanDigitValue ( char );
char RomanDigitChar ( int );
void PrintRomenNumeral ( int );
int Choose ( int val );
void Symbol_Display ( char calc );
int calculation ( int num_1, int num_2, char calc );
int Negatives ( int val );
void Display_Text ( int num_1, int num_2, int count );
void Display_Answer ( int num_1, int num_2, int val, char calc );
void Error_Message ( int count );

int main ()
{
int fail = 0;
char calc;
int num_1, num_2 = 1;
int num = 0;

num_1 = GetRomanNumeral ();
while ( cin && fail == 0 )
{
if ( num_1 != BAD_VAL )
{
num_2 = GetRomanNumeral ();
if ( num_2 != BAD_VAL )
{
cin.get ( calc );
num = calculation ( num_1, num_2, calc );
Display_Text ( num_1, num_2, fail );
Display_Answer ( num_1, num_2, num, calc );
num_1 = GetRomanNumeral ();
return 0;
}
else
fail= 1;
Error_Message ( fail );
}
else
{
fail = 1;
Error_Message ( fail );
}
}

return 0;
}

int GetRomanNumeral ()
{
int val = 0;
int total = 0;
char roman_digit = 0;
cin.get ( roman_digit);
while ( cin && ( roman_digit != '\n' ) )
{
total = RomanDigitValue ( roman_digit );
if ( total == BAD_VAL )
return BAD_VAL;
else
val = val + total;
cin.get ( roman_digit );
}
return val;
}

int RomanDigitValue ( char roman_digit )
{
int number;

if ( roman_digit == 'I' )
number = ROMAN_I;
else if ( roman_digit == 'V' )
number = ROMAN_V;
else if ( roman_digit == 'X' )
number = ROMAN_X;
else if ( roman_digit == 'L' )
number = ROMAN_L;
else if ( roman_digit == 'C' )
number = ROMAN_C;
else if ( roman_digit == 'D' )
number = ROMAN_D;
else if ( roman_digit == 'M' )
number = ROMAN_M;
else
number = BAD_VAL;
return number;
}

char RomanDigitChar ( int val )
{
if ( val >= ROMAN_M )
return 'M';
else if ( val >= ROMAN_D )
return 'D';
else if ( val >= ROMAN_C )
return 'C';
else if ( val >= ROMAN_L )
return 'L';
else if ( val >= ROMAN_X )
return 'X';
else if ( val >= ROMAN_V )
return 'V';
else if ( val >= ROMAN_I )
return 'I';
return 0;
}

void PrintRomenNumeral ( int val )
{
char rome_dig;
val = Negatives ( val );
if ( val == 0 )
cout << "zero";
while ( val != 0 )
{
rome_dig = RomanDigitChar ( val );
cout << rome_dig;
val = Choose ( val );
}
}

int Choose ( int val )
{
if ( val >= ROMAN_M )
val = val - ROMAN_M;
else if ( val >= ROMAN_D )
val = val - ROMAN_D;
else if ( val >= ROMAN_C )
val = val - ROMAN_L;
else if ( val >= ROMAN_X )
val = val - ROMAN_X;
else if ( val >= ROMAN_V )
val = val - ROMAN_V;
else if ( val >= ROMAN_I )
val = val - ROMAN_I;

return val;
}

void Symbol_Display ( char calc )
{
if ( calc == '+' )
cout << "sum ";
else if ( calc == '-' )
cout << "difference ";
else if ( calc == '*' )
cout << "product ";
else if ( calc == '/' )
cout << "difference ";
}

int calculation ( int num_1, int num_2, char calc )
{
int total_sum;
if ( calc == '+' )
total_sum = num_1 + num_2;
else if ( calc == '-' )
total_sum = num_1 - num_2;
else if ( calc == '*' )
total_sum = num_1 * num_2;
else if ( calc == '/' )
total_sum = num_1 / num_2;
return total_sum;
}

int Negatives ( int val )
{
if ( val < 0 )
{
cout << "-";
val = val * -1;
}
return val;
}

void Display_Text ( int num_1, int num_2, int fail )
{
if ( fail == 0)
cout << "The first number is " << num_1 << endl;
else if ( fail == 1 )
cout << "The second number is " << num_2 << endl;
cout << endl;
}

void Display_Answer ( int num_1, int num_2, int val, char calc )
{
cout << "Arithmetic operation is " << calc << endl;
cout << "The ";
Symbol_Display ( calc ) ;
cout << "of " << num_1 << " and " << num_2 << " is ";
PrintRomenNumeral ( val );
cout << " (" << val << ") " << endl;
cout << endl;
}

void Error_Message ( int fail )
{
if ( fail == 0 )
cout << "The first number is invalid!";
else
cout << "The second number is invalid!";
}
 
Physics news on Phys.org
  • #2
</code>Output generated by your program from test test1.txt (line numbers added):1: MCCXXVI2: The first number is 12263: LXVIIII4: The second number is 695: +6: Arithmetic operation is +7: The sum of 1226 and 69 is MCCLXXXXV (1295)8: 9: DCX10: The first number is 61011: MCI12: The second number is 110113: -14: Arithmetic operation is -15: The difference of 610 and 1101 is -CCCCLXXXXI (-491)16: 17: LXVI18: The first number is 6619: CCLXI20: The second number is 26121: /22: Arithmetic operation is /23: The quotient of 66 and 261 is zero (0)24: 25: MD26: The first number is 150027: XXX28: The second number is 3029: /30: Arithmetic operation is /31: The quotient of 1500 and 30 is L (50)32: 33: LXVIIII34: The first number is 6935: XXVIIII36: The second number is 2937: *38: Arithmetic operation is *39: The product of 69 and 29 is MMI (2001)
 
  • #3


I would suggest that the individual seeking help with their C++ homework should provide more information about what exactly is not working in their code. They should also try to identify any specific errors or bugs they are encountering and provide that information as well. Additionally, they may want to consider seeking help from a tutor or classmate who has experience with C++ programming to troubleshoot any issues and improve their understanding of the language. It is important to thoroughly test and debug code to ensure it produces the desired results before submitting it as a final assignment.
 

What is C++?

C++ is a high-level, general-purpose programming language that was developed by Bjarne Stroustrup in 1983. It is an extension of the C programming language and is commonly used for developing operating systems, graphics, and games.

Why do I need help with my C++ homework?

C++ is a complex language that requires a deep understanding of its concepts and syntax. It is not uncommon for students to struggle with their C++ homework, especially if they are new to programming. Seeking help can assist in better understanding the material and improving grades.

Where can I find help with my C++ homework?

There are various resources available for students seeking help with their C++ homework. You can consult with your professor or teaching assistant, join study groups, or seek help from online tutoring services. Additionally, there are many online forums and communities dedicated to C++ where you can ask for assistance.

What are some common challenges students face with C++ homework?

Some common challenges students face with C++ homework include understanding concepts such as pointers and memory management, debugging errors, and implementing complex algorithms. These challenges can be overcome with practice and seeking help from experienced individuals.

How can I improve my skills in C++?

To improve your skills in C++, it is important to practice regularly and seek help when needed. Reading books and online resources, attending workshops and seminars, and working on personal projects can also help in enhancing your understanding of the language. Additionally, collaborating with others and seeking feedback can also aid in improving your skills.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
932
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
872
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
Back
Top