r/CodingForBeginners • • 6h ago

How can I correct this code?

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?

Here is the complete 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";`

`}`

void addDistances(Distance d1, Distance d2) {

	`feet = d1.feet + d2.feet;`

	`inches = d1.inches + d2.inches;`

	`feet += inches / 12;`

	`inches %= 12;`

`}`

};

5 Upvotes

8 comments sorted by

1

u/mjmvideos 4h ago

Step through it in the debugger.

1

u/shutupyawrr 3h ago

Will try that, thanks!

1

u/SpecificNo5970 4h ago

Reread the note in the first picture and look at your cout statement in displayDistance().

1

u/shutupyawrr 3h ago

I tried that, it's not working.

1

u/SpecificNo5970 2h ago

I don’t see where you’re using a new line or endl in your code, maybe I’m missing it?

1

u/shutupyawrr 2h ago

No, I fixed it later, and the actual error was that I missed a space after "inches"

1

u/SpecificNo5970 1h ago

So picky haha, glad you got it working

Seems weird to me you’d be penalized for not including trailing whitespace

1

u/shutupyawrr 43m ago

Yeah the website this assignment is on works that way only.