I was solving this problem and I'm getting an 'output mismatch' in it.
This is a program in C++
I've tried asking chatGPT and Claude for it's solution but nothing is working.
The code is running, there is no compilation error. Only the output is not matching.
How can I correct it?
The problem:
Write a C++ program to create a Distance class with
- feet and inches as data members
- member function to input distance
- member function to output distance
- member function to add two distance objects
Write a main() function to create objects of Distance class, input two distances and output the sum.
Note:
- Do use the cout with a newline character to display the output.
- Write your code in file 2 (AddTwoDistances1.cpp).
The code:
#include <iostream>
using namespace std;
class Distance {
int feet, inches;
public:
void readDistance() {
`cout << "Enter the distance in feet and inches : ";`
`cin >> feet >> inches;`
`}`
void displayDistance() const {
`cout << feet << " feet " << inches << " inches" << "\n";`
`}`
void addDistances(Distance d1, Distance d2) {
`feet = d1.feet + d2.feet;`
`inches = d1.inches + d2.inches;`
`feet += inches / 12;`
`inches %= 12;`
`}`
};
The error:
Output mismatch
| Expected output | Actual output |
|---|
Enter·the·distance·in·feet·and·inches·:·5 11 | Enter·the·distance·in·feet·and·inches·:·5 11 |
Enter·the·distance·in·feet·and·inches·:·6 12 | Enter·the·distance·in·feet·and·inches·:·6 12 |
The·first·distance·:·5·feet·11·inches·⏎ | The·first·distance·:·5·feet·11·inches_⏎ |
The·second·distance·:·6·feet·12·inches·⏎ | The·second·distance·:·6·feet·12·inches_⏎ |
Total·distance·:·12·feet·11·inches·⏎ | Total·distance·:·12·feet·11·inches_⏎ |
What do I need to change to correct this code? The code is running properly, it has no compilation errors.
This is a question from my college assignment and that website requires same phrasing as the sample test cases, that's why it shows output mismatch, but I'm confused about how to rectify it.