I am having issues when I run my c++ code it passes all of the test cases besides this one can, someone please see what I am doing wrong. Thank you :)

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

I am having issues when I run my c++ code it passes all of the test cases besides this one can, someone please see what I am doing wrong. Thank you :)

Code:

// Add appropriate headers

/* KEEP THIS COMMENT
* class Rational
* represents a Rational number. Remember rational means ratio-nal
* which means there is a numerator and denominator having
* integer values. Using good ADT techniques, we have made member
* variable private (also known as instance variables) and made member
* functions public.
*/
#include<iostream>
using namespace std;

class Rational{
private:
int n, d;

public:
// constructor - default
Rational(){
n = 0;
d = 1;
}
//constructor with 1 parameter
Rational(int n){
this->n = n;
this->d = 1;
}
//constructor with 2 parameters
Rational(int n,int d){
this->n = n;
this->d = d;
}

//getters
int getNumerator(){
return n;
}
int getDenominator(){
return d;
}
//mutators
void setNumerator(int n){
this->n = n;
}
void setDenominator(int d){
this->d = d;
}

// input function, body is definded outside class
void input();
// to display "this" Rational number
void output(){
cout<<n<<"/"<<d<<endl;
}

// set Nr and Dr of "this" Rational number with sum of r1 and r2
void sum(Rational r1, Rational r2);

// check if "this" Rational number is equal to Rational number "r"
bool isEqual(Rational r){
return this->n==r.getNumerator() && this->d==r.getDenominator();
}

};

// outside class definition of member function - sum
void Rational :: sum(Rational r1, Rational r2){
int a = r1.getNumerator();
int b = r1.getDenominator();

int c = r2.getNumerator();
int d = r2.getDenominator();

// set numerator and denominator of "this" Rational number
setNumerator(a*d + b*c);
setDenominator(b*d);
}


// outside class definition of member function - input()
void Rational :: input(){
string str="";
cin>>str;
int n,d;
bool checkInput = false;
while(checkInput == false){
n=d=0;
int i;
for(i=0;i<str.size();++i){
if(str[i]=='/') break;
n = n*10 + (str[i]-'0');
}

// i is currently pointing to '/' or end of string if '/' didn't
// exist in input string so increment it for reading Denominator
++i;


for(;i<str.size();++i){
d = d*10 + (str[i]-'0');
}

if(d!=0) {
// correct input
checkInput=true;
}
else{
// either input string didn't contain '/'
// or Denominator, d = 0
cout<<"wrong input."<< endl;
   cout << "\nEnter a rational (p/q):";
cin>>str;
}
}

// set numerator and denominator of "this" Rational number
setNumerator(n);
setDenominator(d);

}

int main()
{
// ToDo: declare three rational objects using the default constructor
Rational op1, op2, C;
char answer;

// Main loop to read in rationals and compute the sum
do {
  
cout << "\nEnter op1 (in the format of p/q):" ;
// ToDo: use your input member function to read the first rational
op1.input();


cout << "\nEnter op2 (in the format of p/q):";
// ToDo: use your input member function to read the second rational
op2.input();


// ToDo: use the third rational to call Sum with first and second as parameters
C.sum(op1, op2);

cout << "\nThe sum of op1 and op2 is:";
// ToDo: ouptput the third rational
C.output();


//cout << endl;
cout << "\nTry again (Y/N)?";
cin >> answer;
} while (answer == 'y' || answer == 'Y');

// ToDo: test getters on rational that has the sum above.
cout << "\nC's numerator is: " << C.getNumerator();
cout << "\nC's denominator is: " << C.getDenominator();
cout<<endl;

// TODO: Use two constructors to declare a whole number 3/1 and a 4/5
Rational D(3);
Rational E(4,5);

// TODO: Use output to print both rationals
//D.output();
//E.output();

return 0;
}

Debugging Information for Non-standard Test Case O
DEBUG INFO
Your instructor has chosen to give you the following information to help you debug your code and improve your
submission.
COMPILER STACK TRACE
None
PROGRAM EXECUTION STACK TRACE
None
INPUT OF THE TEST CASE
1 -1/3
2 4/5
3 -2/9
4 0/4
5 -8/0
6 n
YOUR CODE'S OUTPUT
Enter op1 (in the format of p/q):
3 Enter op2 (in the format of p/q):
4 The sum of op1 and op2 is:-133/15
6 Try again (Y/N)?
7 C's numerator is: -133
8 C's denominator is: 15
THE CORRECT OUTPUT OF THE TEST CASE
2 Enter op1 (in the format of p/q):
3 Enter op2 (in the format of p/q):
The sum' of op1 and op2 is:7/15
6 Try again (Y/N)?
7 C's numerator is: 7
9 C's denominator is: 15
10
*UNIX DIFF` OF CORRECT OUTPUT AND YOUR OUTPUT
4c4
< The sum of op1 and op2 is:-133/15
> The sum of op1 and op2 is:7/15
7c7,8
< C's numerator is: -133
> C's numerator is: 7
PRETTY DIFF
This diff is colored to make it clear what parts of the output are wrong. Green indicates things in the correct output
that you are missing, red indicates things in your output that shouldn't be there.
The character refers to newlines, so the green - character refers a newline you are missing in your output and the
red - refers to a newline you need to remove from your output.
Enter op1 (in the format of p/q):-
Enter op2 (in the format of p/q):d
The sum' of op1 and op2 is:-1337/15-
t.
6 Try again (Y/N)?-
7 C's numerator is: -1337-
9 C's denominator is: 15-
10
Transcribed Image Text:Debugging Information for Non-standard Test Case O DEBUG INFO Your instructor has chosen to give you the following information to help you debug your code and improve your submission. COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None INPUT OF THE TEST CASE 1 -1/3 2 4/5 3 -2/9 4 0/4 5 -8/0 6 n YOUR CODE'S OUTPUT Enter op1 (in the format of p/q): 3 Enter op2 (in the format of p/q): 4 The sum of op1 and op2 is:-133/15 6 Try again (Y/N)? 7 C's numerator is: -133 8 C's denominator is: 15 THE CORRECT OUTPUT OF THE TEST CASE 2 Enter op1 (in the format of p/q): 3 Enter op2 (in the format of p/q): The sum' of op1 and op2 is:7/15 6 Try again (Y/N)? 7 C's numerator is: 7 9 C's denominator is: 15 10 *UNIX DIFF` OF CORRECT OUTPUT AND YOUR OUTPUT 4c4 < The sum of op1 and op2 is:-133/15 > The sum of op1 and op2 is:7/15 7c7,8 < C's numerator is: -133 > C's numerator is: 7 PRETTY DIFF This diff is colored to make it clear what parts of the output are wrong. Green indicates things in the correct output that you are missing, red indicates things in your output that shouldn't be there. The character refers to newlines, so the green - character refers a newline you are missing in your output and the red - refers to a newline you need to remove from your output. Enter op1 (in the format of p/q):- Enter op2 (in the format of p/q):d The sum' of op1 and op2 is:-1337/15- t. 6 Try again (Y/N)?- 7 C's numerator is: -1337- 9 C's denominator is: 15- 10
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY