r/cpp_questions • u/shutupyawrr • 2d ago
SOLVED Need help regarding a problem, it shows 'output mismatch' all the time.
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.
5
u/AKostur 2d ago
Well, this is really a question about the program evaluating your output. But I can see that you haven't strictly followed their instructions. They specifically said to use the newline character, and you have not done that. And the "actual output" doesn't match the expected output.
2
u/aocregacc 2d ago
looks like maybe it wants a space after the "inches" in the output.
2
u/flyingron 2d ago
You're going to have to ask your teacher (or whoever wrote this test hardness), what the diagnostics mean. If the dot means they want a space, indeed your program doesn't not put a space after the word "inches."
2
u/Independent_Art_6676 2d ago
the lazy auto graders are a constant problem for everyone. For the teacher, you get 100% score if you know how to make it show you what it wants and hard code the output. I could probably get an entire CS degree doing that (I spent a semester helping a relative and showed him how to get the output from the grader so he would know what he was doing wrong. He did it right, but it would have been very, very easy to cheat out). For the student, who doesn't know how to make them assist you, they are a neverending frustration to try to get a perfect score.
1
u/alfps 2d ago
Use a space instead of underscore after "inches".