r/cpp_questions • • 23h ago

SOLVED Need help regarding a problem, it shows 'output mismatch' all the time.

4 Upvotes

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 outputActual output
Enter·the·distance·in·feet·and·inches·:·5 11Enter·the·distance·in·feet·and·inches·:·5 11
Enter·the·distance·in·feet·and·inches·:·6 12Enter·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.


r/cpp_questions • • 2h ago

SOLVED Memory leak in a virtual destructor of an abstract base class

3 Upvotes

I have an abstract base class from which other classes are derived. It has a pure virtual method and a virtual destructor with an empty body:

class LibraryItem
{
protected:
    int id;
    std::string title;
    std::string author;
    int year;
    int cost;

    friend class BookEditor;
public:
    
// other methods...

    virtual void Print() const = 0;

    virtual ~LibraryItem() {}
};

My teacher said that I have a memory leak here, but I don't understand why.
The destructor is virtual, so when an object of a derived class is deleted through a LibraryItem*, the derived destructor should be called first, and then the base destructor.
I have always written destructors this way and have never received any complaints.

Some other students told me that because I declared the destructor with an empty body:

virtual ~LibraryItem() {}

because "it does NOTHING**"**. But I thought it does nothing EXCEPT destroy the fields automatically. None of the fields in LibraryItem are raw pointers that would need to be deleted manually, so I don't understand where exactly my mistake / memory leak is.

I have seen people suggest writing:

virtual ~LibraryItem() = default;

I would be very grateful if someone could explain:

  1. whether an empty virtual destructor like virtual ~LibraryItem() {} can itself cause a memory leak;
  2. what the difference is between virtual ~LibraryItem() {} and virtual ~LibraryItem() = default; in this context.

I was looking for similar questions, but I couldn't figure it out on my own.


r/cpp_questions • • 15h ago

OPEN Help With Using Standard Libraries In My Hyper-Specific Project

2 Upvotes

Hello, everyone. I've recently taken up a project where I work to streamline and rewrite an originally vibecoded project which allowed for the creation of custom DLLs which could be loaded by the game Serious Sam 2. From what I've gathered, the AI that wrote the original project created an environment isolated from the rest of the system and does not allow access to any standard library features. I've looked through this project's makefile to try and find what exactly is causing this and how to rectify it. Unfortunately, I do not believe that I am skilled or knowledgeable regarding makefiles to diagnose the problem and solve it. I have moving the code elsewhere and using auto-generated cmake files from IDEs such as CLion, and the library would compile, but Serious Sam 2 then refuses to recognize it. I know this is more of a niche problem compared to most of what gets posted here, but I'd really really really appreciate if some more experienced members could help out here! I would love to learn what the issue is and maybe how makefiles in general work. My experience with them is kinda subpar admittedly.

Here's the repo to the current version of my project:

https://github.com/GrouganDev/Another-SS2-Redo-DLL


r/cpp_questions • • 16h ago

OPEN Unable to fix C++ output

1 Upvotes
Undefined symbols for architecture arm64:
  "_main", referenced from:
      <initial-undefines>
ld: symbol(s) not found for architecture arm64
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

Hi. I've started learning C++ for Arduino and while practicing, I ran my code and the output is saying its a Clang++ error. I downloaded clang as it was recommended by a tutor on youtube,and it works for mark. Even basic print code wont work. Anyway to solve this?


r/cpp_questions • • 18h ago

OPEN Help getting external libraries working

1 Upvotes

I'm decently inexperienced with C++ (Only did one class using the language during school, and we used std for everything). I'm not actually sure everything I need to do to get an external library working for my project.

I'm using CLion IDE, most of my settings are still untouched, and I'm trying to get CImg (with libpng) I researched the topic online, but I didn't quite figure out how to get it working... in part, because I'm not sure which part of libpng I'm supposed to use.

I usually prefer to recreate all my functions from scratch, but I couldn't really do that for images, especially if I wanted access to .png files. I'm not picky about using Cimg, I really just need something that can display, rotate, resize, and edit images together (like putting a character on a background).

I backed up my code to github, and made a public branch. If you wanna tear apart an amatuer's code, there's probably a lot to laugh at in here, but I might learn something from people pointing out all the mistakes.
https://github.com/Viteris/Jivelthresh


r/cpp_questions • • 22h ago

OPEN C++ IntelliSense and autocomplete are not working in VS Code Jupyter Notebooks with xeus-cling

0 Upvotes

Hi everyone,

I'm trying to set up an interactive C++ programming environment in VS Code using Jupyter Notebooks and the xeus-cling kernel. The main reason for choosing xeus-cling was its ability to execute C++ code interactively, with variables, structures, and functions persisting across cells without requiring a main() function.

For the setup, I installed xeus-cling in a separate Conda environment managed by Miniforge3, running inside Ubuntu on WSL2. I have connected VS Code to WSL and installed the necessary C/C++ and Jupyter extensions. I have also configured the GCC/G++ compiler correctly inside WSL.

IntelliSense works perfectly in standalone .c and .cpp files. However, the same functionality does not seem to work in Jupyter Notebook (.ipynb) cells. I'm particularly having trouble with tab completion, rich function signature and snippet suggestions, predefined namespace and standard library function suggestions, and suggestions for variables declared in previous cells.

For example, typing std::vec does not suggest std::vector, and pressing Tab does not provide the expected C++ completions. I have enabled Jupyter kernel completions and tried changing the relevant VS Code settings, but the problem persists. Interestingly, basic suggestions such as #include Snippets and some previously typed identifiers appear in JupyterLab, but I haven't been able to get proper C++ kernel-based completion to work. The C++17 kernel itself works correctly, and I can execute code that uses variables and functions that persist across cells.

Has anyone encountered a similar issue when using xeus-cling with VS Code Jupyter Notebooks? Is there a way to enable proper C++ IntelliSense, kernel-based autocomplete, function signatures, and namespace suggestions inside notebook cells?

I'd appreciate any suggestions, workarounds, extensions, or configuration changes that could improve the C++ notebook editing experience.

Thanks in advance!