Trending questions in C++

0 votes
0 answers

How can I get double quotes into a string literal?

I used the printf() command to produce the output seen below: printf("She said time flies like an arrow, ...READ MORE

Aug 5, 2022 in C++ by Nicholas
• 7,760 points
806 views
0 votes
0 answers

how to print \" in C++

I need to print the following string: std::string("-I\"/path/to/dir\" "); Basically, I need to accomplish this since I am generating C++ code using C++ code. I want to use an ofstream to write the above string, so something like ofstream fout; fout << the_string ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
538 views
0 votes
1 answer

Create a reverse LinkedList in C++ from a given LinkedList

A simpler solution is to just let the current node point to the previous node while going through your linked list, saving the previous and next nodes as you go. void LinkedList::reversedLinkedList() { if(head == ...READ MORE

Aug 5, 2022 in C++ by Damon
• 4,960 points
766 views
0 votes
0 answers

In C, are arrays pointers or used as pointers?

First, look at this code When this is ...READ MORE

Aug 11, 2022 in C++ by krishna
• 2,820 points
503 views
0 votes
0 answers

How to generate a random number in C++?

I'm attempting to develop a dice game, and I need random numbers to represent the sides of the die.  I can make a number between 1 and 6).  Using #include <cstdlib> #include <ctime> #include <iostream> using namespace ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
494 views
0 votes
0 answers

Insert object at index of vector c++

I have to add a new item to the existing object vector.  Although I am aware that an iterator must be used, I am not entirely sure how it operates. I need to insert a new item by name in the precise index that I found after some searching into an alphabetically sorted vector.  I have this, then. vector<Person>people; int index =54; Person temp; people.push_back(temp);//insert at end of ...READ MORE

Aug 5, 2022 in C++ by Nicholas
• 7,760 points
711 views
0 votes
0 answers

How do you properly use namespaces in C++?

I come from a Java background where ...READ MORE

Aug 8, 2022 in C++ by Nicholas
• 7,760 points
582 views
0 votes
1 answer

Socket Programming in C++

The C++ Standard does not have a ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
792 views
0 votes
0 answers

How do you open a file in C++?

I want to open a file in ...READ MORE

Aug 8, 2022 in C++ by Nicholas
• 7,760 points
554 views
0 votes
0 answers

What is a stream in C++?

I've been hearing a lot about streams, ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
409 views
0 votes
0 answers

memory allocation for objects

When we instantiate a variable in C++, ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
408 views
0 votes
1 answer

Reverse Contents in Array

My strategy would be as follows: #include <algorithm> #include <iterator> int main() { const int ...READ MORE

Aug 5, 2022 in C++ by Damon
• 4,960 points
626 views
0 votes
0 answers

How to use vector::push_back()` with a struct?

How do I push a struct back into a vector? struct point { int ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
1,208 views
0 votes
0 answers

How come an array's address is equal to its value in C?

The pointer values and pointer addresses in ...READ MORE

Aug 8, 2022 in C++ by krishna
• 2,820 points
470 views
0 votes
0 answers

What are Containers/Adapters? C++

What exactly are containers/adapters? I am familiar ...READ MORE

Aug 8, 2022 in C++ by Nicholas
• 7,760 points
468 views
0 votes
1 answer

Why should I use reference variables at all?

References themselves are unrelated to the issue. The issue is because C++ manages object lifetimes differently from run-time systems that employ garbage collectors, such as Java.  There is no standard built-in garbage collector in C++.  Both automatic (within local or global scope) and manual (explicitly allocated/deallocated in heap) object lifetimes are possible in  C++. A C++ reference is nothing more than an object's alias.  It has no knowledge of object lifespan (for the sake of efficiency).  The coder must give it some thought.  A reference bound to a temporary object is an exception; in this situation, the temporary object's lifespan is prolonged to include the lifetime of the bound reference. References play a crucial role in the fundamental ideas of C++, and they are required for 90% of jobs.  Otherwise, pointers must be used, which is typically far worse. You can use references, for instance, when you need to give an object as a function parameter by reference rather than by value: void f(A copyOfObj); ...READ MORE

Aug 5, 2022 in C++ by Damon
• 4,960 points
593 views
0 votes
1 answer

how could I use the power function in c/c++ without pow(), functions, or recursion

It is part of a series.  Replace pow() with the previous iteration's value. There is no need for code to call pow ().  Pow(x, 5 * I - 1) and pow(-1, I - 1) may be formed since both have an int exponent dependent on the iterator I from the previous loop iteration. Example: Let f(x, i) = pow(x, 5 * i ...READ MORE

Jun 21, 2022 in C++ by Damon
• 4,960 points
2,512 views
0 votes
0 answers

Confused with object arrays in C++

I have some Java knowledge and now ...READ MORE

Aug 11, 2022 in C++ by krishna
• 2,820 points
312 views
0 votes
0 answers

How can I get the size of a C++ function?

In C++, how can I find the size& ...READ MORE

Aug 5, 2022 in C++ by Nicholas
• 7,760 points
546 views
0 votes
0 answers

Nested try...catch inside C++ exception handler?

I may wish to run code that might throw an exception in my exception handler. Is the following C++ structure acceptable?  Are there any drawbacks if so? try { // ... } catch (const ...READ MORE

Aug 5, 2022 in C++ by Nicholas
• 7,760 points
538 views
0 votes
1 answer

When to use extern in C++

This is useful when dealing with global variables.  Global variables are declared in a header so that any source file that contains the header is aware of them, but you only need to "define" them once in one of your source files. To explain, using extern int x; informs the compiler that an int object named x exists elsewhere.  It is not the compiler's responsibility to know where it exists; it just needs to know the type and name so that it may utilise it.  After compiling all of the source files, the linker will resolve all x references to the one definition found in one of the generated source files. For it to operate, the x variable's declaration must have "external linkage," which simply means that it must be defined outside of a function (at what's known as "the file scope") and without the static keyword. header: #ifndef HEADER_H #define HEADER_H // any source file that ...READ MORE

Jun 21, 2022 in C++ by Damon
• 4,960 points
2,354 views
0 votes
0 answers

How can i sort a map by its .second parameter

How can I output all the int ...READ MORE

Jul 28, 2022 in C++ by Nicholas
• 7,760 points
764 views
0 votes
1 answer

"We do not use C++ exceptions" — What's the alternative? Let it crash?

If you don't utilise exceptions, by definition, ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
501 views
0 votes
1 answer

Examples of good gotos in C or C++

Here's one method I've heard of folks use. But I've never seen it in the wild.  And that only pertains to C since RAII allows you to accomplish this more idiomatically in C++. void foo() { if (!doA()) ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
479 views
0 votes
0 answers

Which is better way to calculate nCr

Approach 1: C(n,r) = n!/(n-r)!r! Approach 2:  I discovered this in Wilf's book Combinatorial Algorithms: C(n,r) may be represented as C(n-1,r) + C (n-1,r-1). e.g. C(7,4) = C(6,4) + ...READ MORE

Jul 25, 2022 in C++ by Nicholas
• 7,760 points
838 views
0 votes
0 answers

C++ Switch Cases

I was taking an online quiz based on the C++ switch statement.  I came across a question, and while I have a good knowledge of how switch statements function, this particular question made no sense to me.  Could you please explain? Why is the answer D and not ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
958 views
0 votes
0 answers

Storing C++ template function definitions in a .CPP file

I have some template code that I'd rather have saved in a CPP file rather than inline in the header.  I know this is possible if you know which template types will be utilised.  As an example: .h file class foo { public: template <typename ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
771 views
0 votes
0 answers

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

I looked up the differences between cout, ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
716 views
0 votes
0 answers

Conversion of base 10 to base 6

I attempted to convert a base 10 number to a base 6 number in C, but my code failed two hidden test cases. I don't see any logical flaws in it. Could you do it? //convert base 10 to base 6 #include<stdio.h> int main() { ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
760 views
0 votes
0 answers

C++ - upcasting and downcasting

As an example: Shouldn't the second d.print() call during upcasting print "base"? Isn't it a "d" derived object that has been upcasted to a base class object? And what advantages does downcasting have? Could you describe upcast and downcast in more detail? #include <iostream> using namespace std; class Base { public: ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
756 views
0 votes
0 answers

Adding to a vector of pair

I have a pair vector that looks like this: vector<pair<string,double>> revenue; I'd want to add a string and a double from a map that looks like this: revenue[i].first = "string"; revenue[i].second = map[i].second; However, because revenue is not initialised, it returns an out of bounds error.  So I ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
724 views
0 votes
0 answers

Check if a string contains a string in C++

I've got a std::string variable. I'd want ...READ MORE

Jul 28, 2022 in C++ by Nicholas
• 7,760 points
621 views
0 votes
0 answers

How to copy turbo c++ output?

How can I replicate the output of Turbo C++?  I had already Googled the issue, but in vain.  It suggests to either print scrn and paste or right click, select all, and paste.  I tried both, but neither worked.  The issue is that it simply copies what is on the current screen.  But I want the entire screen from the start.  (Alt+printscrn is also ineffective).  What should I do in this situation? printScrn Alt+printScrn markall None of them are operational!! I can't assist you if you require this archaic technique of programming for whatever reason, but I'd want to find a solution.  I tried forwarding the output stream to the file in this manner, but it did not work. #include<iostream.h> #include<conio.h> #include<stdlib.h> const int max=50; class dequeue{ int dq[max],r,f,c,x,i; public: dequeue(); void insertRear(); void insertFront(); void ...READ MORE

Jul 7, 2022 in C++ by Nicholas
• 7,760 points
1,434 views
0 votes
0 answers

Simple dictionary in C++

Changing some Python code to C++. BASEPAIRS = { "T": "A", "A": "T", ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
604 views
0 votes
0 answers

Understanding c++ Code: Tower of Hanoi using Recursion

I'm studying about recursion in C++, but the following C++ code used to solve the Tower of Hanoi issue has me baffled. void Hanoi(int m, string start, string middle, ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
577 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

Jun 1, 2022 in C++ by Damon
• 4,960 points
2,922 views
0 votes
0 answers

How to declare an array of strings in C++?

I am trying to iterate over all ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
542 views
0 votes
0 answers

What is meant with "const" at end of function declaration?

I received a book in which it is written: class Foo { public: int ...READ MORE

Jul 28, 2022 in C++ by Nicholas
• 7,760 points
455 views
0 votes
0 answers

Is there a default return value of C++ functions?

I'm amazed that a file with the  ...READ MORE

Jul 25, 2022 in C++ by Nicholas
• 7,760 points
548 views
0 votes
0 answers

Run C++ in command prompt - Windows

What may be the cause? I know ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
670 views
0 votes
0 answers

Simple C++ swap function

Why is it that if I have a method like this to swap two numbers, it doesn't work[swap], (I know I can accomplish this by defining pointers in the prototype and then passing the addresses of the relevant variables in main()), yet it works for arrays without having to supply pointers and addresses? It does not work. void num_exchange(int m, int n); int main(){ int num1 ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
448 views
0 votes
0 answers

What is the difference between a concrete class and an abstract class?

I'm learning C++, but I'm having trouble ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
484 views
0 votes
0 answers

Accumulate function in Vector (STL) giving negative sum

I'm receiving a negative total when I run the code below (-294967296). #include<iostream> #include<vector> #include<numeric> using namespace std; int main() { ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
651 views
0 votes
0 answers

How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers

As a mathematician, one of my pet peeves of C-derived languages is : (-1) % 8 // comes out as ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
417 views
0 votes
0 answers

What type of members can I add in a c++ abstract class

Hello, let's say I have an abstract class with a few pure abstract functions and a few classes that derive from it, and all of the data from these classes eventually becomes similar, I was wondering if it would be wise, or even possible, to declare a vector under protected in the abstract class to collect the data so something like that. class A { protected: vector <string> ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
407 views
0 votes
0 answers

Container Class vs Class - C++

I'm new to programming and have only ...READ MORE

Jul 28, 2022 in C++ by Nicholas
• 7,760 points
355 views
0 votes
1 answer

Ternary operator, if I avoid writing ' expression 2 ' it works but if I not write 'expression 3 ' it gives an error [duplicate]

6.8 Conditionals with Missing Opponents In a ...READ MORE

Jun 28, 2022 in C++ by Damon
• 4,960 points
1,565 views
0 votes
0 answers

Why do we use volatile keyword?

I've never used it, but I'm curious ...READ MORE

Jul 25, 2022 in C++ by Nicholas
• 7,760 points
434 views
0 votes
0 answers

What are the new features in C++17?

C++17 is currently feature complete, therefore major ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
389 views
0 votes
0 answers

How to concatenate two strings in C++?

I have a private class variable char ...READ MORE

Jul 25, 2022 in C++ by Nicholas
• 7,760 points
431 views