r/cpp_questions • • 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.

4 Upvotes

14 comments sorted by

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.

1

u/shutupyawrr 2d ago

I'm still learning that's why make mistakes, I will read the instructions carefully from now on.

2

u/aocregacc 2d ago

looks like maybe it wants a space after the "inches" in the output.

1

u/shutupyawrr 2d ago

There is a space already, it doesn't want any space. I added \n at the end for that, but still no results.

3

u/aocregacc 2d ago

I meant after the word "inches", the code you posted doesn't have a space after the word "inches" in the displayDistance function

1

u/shutupyawrr 2d ago edited 2d ago

Omg thank you so much!!!!!! I was stuck on this problem since idk when, it's corrected now.

2

u/aocregacc 2d ago

yeah having a space before the newline is pretty diabolical, but at least their "expected vs actual" output is pretty good about showing whitespace.

1

u/shutupyawrr 2d ago

Yes, maybe I was too frustrated to notice the space lol.

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."

1

u/shutupyawrr 2d ago

Yes it required a space after "inches". I have corrected it now.

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/shutupyawrr 2d ago

Yeah it's very frustrating as it's either 0 score or 100%, the efforts or thought process behind the problem doesn't matter at all

1

u/alfps 2d ago

Use a space instead of underscore after "inches".

 The·first·distance·:·5·feet·11·inches·⏎ | The·first·distance·:·5·feet·11·inches_

1

u/shutupyawrr 2d ago

yes I corrected it, thanks tho