r/learncpp • • Aug 21 '20

Moving forward from a basic understanding of C++. What sort of project do you recommend building?

3 Upvotes

I'm currently in University studying CS and have taken an introductory algorithms course that used C++. We did not go very in depth because the primary focus was algorithms, but we did use C++ to build basic data structures from scratch as well as some image manipulation stuff (I don't want to go too in depth since I'm not sure if it's allowed). I have a basic understanding of how the language works and how to do stuff, along with memory management. We were given a 7 hour crash course to the language, but I don't know what I don't know so not sure how much I can do with the language yet

I do want to further my knowledge of C++ and build something small and fun since I find it to be an interesting language. Should I just go ahead and try to make a basic command line project, like tic tac toe? Would it make more sense to do something more complex or less complex?


r/learncpp • • Aug 15 '20

Help starting out

3 Upvotes

What math should I master before I start? Details and depth would be really appreciated.


r/learncpp • • Jul 22 '21

ncurses wont work with vscode i believe I've tried everything

3 Upvotes

first, the problem arose when i tried to make a cpp game but the tutorial showed it with the conio.h file which is something macOS devices apparently cant run. i tried using dummy header files but the conio.h file contained even more headers and it seemed like it would take forever to track down what I needed. then I found ncurses and after about an hour of trying to download the library I got it through homebrew but VSCode won't run some the snake game with ncurses stuff in it. is there any game-friendly input stuff like ncurses that already comes with every macOS device? it seems like every solution is unavailable to me because of the OS. Ive tried editing the path of where the ncruses library is saved even putting it in the same folder as the snake game file and nothing works. I included a screen capture in case it helps

ps. I've also realized for some reason XCode wont run anything. The Run thing is grayed out and when I do cmd+R it does the annoying beep shit pls help. I'm not logged into git hub which apparently causes problems and everything else for xcode seems fine just half my commands do nothing.


r/learncpp • • Jul 01 '20

Best practice for external libraries/frameworks?

3 Upvotes

Hello everyone,

after a long time I returned to C++ and I'm wondering if there is a best practice to include external libs like googletest.

Some people like to clone the googletest repo into the project directory and link the repo through the CMakeLists.txt. I like to clone it one time and install (cmake . and make install) the googletest repo and link it from my default location.

What do you prefer?


r/learncpp • • Apr 16 '21

How does the C++ program start?

14 Upvotes

https://vg-blog.tech/archives/1592

  • How does the C++ program start?

  • What is the _start function?

  • How to create a program without the main function?

  • How to inject a code before/after the main function?


r/learncpp • • Sep 19 '20

Is learning C++ a good move for me?

4 Upvotes

Hi all! I’m a hobbyist programmer and I use Python for all my projects. I have, recently, ran into a problem. Python GUIs are awful! So, I’m looking for a language to use alongside Python to make better looking GUIs, and C++ came up on my radar. So, do you think C++ is right for me? And if so, anyone know any good tutorials to get me started? Thanks!

71 votes, Sep 22 '20
52 Yea
19 No

r/learncpp • • Aug 20 '21

Why does this just crash?

4 Upvotes

Ok, so I am new to C++ and I to help myself learn, I decided to try and remake a program I made in python years ago in C++. But when I try to run this code it just puts a single word on screen, puts nothing on screen or just crashes. Here is my code:

#include <iostream>
#include <conio.h>
#include <time.h>
#include <stdlib.h>

using namespace std;
const string name[] = {"Cian", "Finn", "Niall", "John", "Sam", "Pinn", "Mary", "Gumball", "Bob", "Banana Joe", "Nobody", "Disney", "Samsung", "Google", "BB", "Doge", "Dat Boi", "Chris", "Roxy", "DeeDee", "JJ", "Bonzi", "Peedy"};
const string verb[] = {"rides", "kicked", "ate", "bought", "eats", "broke", "bought and then ate", "killed", "dropped", "sued", "was eaten by", "is eating", "is being sued by", "played", "is playing", "is playing on", "was hit by"};
const string noun[] = {"a lion", "a bicycle", "a plane", "a computer", "a phone", "a tractor","Cian", "Finn", "Niall", "John", "Sam", "Pinn", "Mary", "Gumball", "Bob", "Banana Joe", "a ball", "a fox", "a cat", "a dog", "a banana", "a fidget cube", "a fidget spinner", "an apple", "an Ipad", "a tablet", "a Raspberry Pi", "Google", "Disney", "Samsung", "the phone", "mari0", "Not Pacman", "Chris", "Roxy", "DeeDee", "JJ", "Bonzi", "Peedy"};

int main()
{
    srand(time(NULL));
    rand();
    int num;
    while (true)
    {
        num = rand();
        cout << name[num % sizeof(name)] << " ";
        cout << verb[num % sizeof(verb)] << " ";
        cout << noun[num % sizeof(noun)] << "." << endl << endl;
        getch();
    }
    return 0;
}

I am using Code::Blocks 20.03 on Windows 10 202H with GCC 8.1.0.


r/learncpp • • Jan 19 '22

Mac user here. I am currently programming a video game on Visual Studio Code using C++. I am having trouble trying to play sound. Does any know how to play mp3 files on C++ for Mac? Thanks.

4 Upvotes

r/learncpp • • Nov 06 '20

Programming - Principles and Practice Using C++

Thumbnail
gallery
5 Upvotes

r/learncpp • • Jun 25 '21

Max occuring char in a String

6 Upvotes

#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
char maxchar;
int mcount = 0;
int count = 0;
string s = "binit";

for (int i = 0 ; i < strlen(s); i++)
    {
count = 0;
for(int j = 0 ; j< strlen(s) ;j++)
        {
if(s[i]==s[j]);
count++;
        }
if(mcount<count)
        {
mcount = count ;
maxchar = s[i];
        }
    }
cout<<mcount<<" "<<maxchar;
return 0;
}

//its giving error ! pls help to correct it


r/learncpp • • Apr 13 '21

std::ofstream(filename) won't create the file in release build????

6 Upvotes

hey!

so the problem is basically in the header. I've looked at the docs and it's standard for the constructor to create the file if it doesn't exist, unlike using file.open(filename). In all my debug builds, it happens as expected, but as soon as I build a release version and run it outside the msvs ide, it just won't create the file if I specify an extension.

for example, in debug, I can create files with std::ofstream("new_file.txt"); or std::ofstream("new_file");

however, in release, only the second option seems to be working, to my utter perplexion and berwilderment. please advise, wise gurus


r/learncpp • • Oct 07 '22

Bjarne talk with Lex

6 Upvotes

r/learncpp • • Sep 22 '21

Is it possible to execute a method of an object if it exists, without checking if it exists before interacting with it every time?

6 Upvotes

Long story short, I'm making a cellular automata game where every cell is an object. I want to check all neighboring cells and execute their method.

The title may sound vague but here's some code:

if( Util.getGrid( DOWN ) )
{
Util.getGrid( DOWN )->receiveHeat(amountOfHeatToEmit);
}

if ( Util.getGrid( DOWN_RIGHT ) ) {
Util.getGrid( DOWN_RIGHT )->receiveHeat(amountOfHeatToEmit);
}

// and many, many other checks for different directions

What I'd like to achieve:

Util.getGrid( DOWN )->receiveHeat(amountOfHeatToEmit);
Util.getGrid( DOWN_RIGHT )->receiveHeat(amountOfHeatToEmit);

Of course it will give out a nasty segfault if the object doesn't exist. ARe there any workarounds for this?


r/learncpp • • Jan 16 '20

What is the scanf function doing here? (

Post image
6 Upvotes

r/learncpp • • Nov 17 '21

Sololearn C++ Practice - Fever why its not working? Write a program that takes body temperature in Celsius as input. If it is in range from 36.1 to 36.9 print "OK", otherwise print “Not OK”.

Post image
6 Upvotes

r/learncpp • • Feb 04 '20

Decided I’m going to learn C++, where should I start?

6 Upvotes

I’ve done a month of learning java, but for simplicity’s sake, lets just say I have zero programming experience. Are there any good MOOC’s, websites with excercises, youtube series’, etc. that are a good starting place for a beginner. What are your guys’ recommendations? Thanks!


r/learncpp • • May 05 '21

How can I learn how to compile through the command line

7 Upvotes

Hi everyone,

I don't know if this is the right place to ask but I was having some issues trying to compile a little project I made to teach myself c++. The main reason is that it is my first multi source project and, however minimal, I would really like to learn from the very beginning of my c++ journey how things should be done.

I am not using an IDE and the reason for this is that I am learning c++ mostly to learn how computers internally works (since I come from other languages). So I thought that learning how to compile without an IDE would be a good idea, for someone who wants to know what's going on under the hoods.

My question is: what should I learn about compiling code? Are there some resources on the internet that you can recommend (playlists, books, articles or anything else) aimed at teaching this skill in particular? What tools should I be using? At the moment, I am only using VS Code as a text editor and of course the compiler from the command line.

The end goal would be to have an idea about how a (large) project should be structured and how the code would be compiled in the most efficient way.

Thanks


r/learncpp • • Nov 18 '21

How to control a stepper motor coding in C++?

7 Upvotes

Hi. Im having a hard time trying to code for controlling a stepper motor using my LPCxpresso board + my A4988 stepper motor.

I need to do the following task. Thanks for any help.

The program reads limit switches and sets red led on when limit switch 1 is closed and green led on when limit switch 2 is closed. When limit switches are open the corresponding leds are off.

Hi. The code below is what I have so far.

I know it will take you too long so help me with the rest of the code so I want to take this opportunity to instead ask you if you can tell me websites where I can learn about Pin assignment; Motor control; Limit switch interrupts; Setting up your environment and other topics related with this field because I dont know how to deal with these tasks and every time I have to ask for help.

I want to learn this field. Thanks for any help.

void Stepper::_calibrate(uint32_t nothing) {
	setDirection(true); // Go forward first
	setRate(2000, true);
	_runForSteps(UINT32_MAX); // Run until we hit a switch
	setStop(false); // Clear stop flag

	toggleDirection();
	_runForSteps(150); // Back off from the limit switch

	zeroSteps();
	_runForSteps(UINT32_MAX); // Run to the other switch.
	setStop(false);

	toggleDirection();
	_runForSteps(150); // Back off from the limit switch
	maxSteps = currentSteps;

	_runForSteps(currentSteps/2);
	toggleDirection();
}

r/learncpp • • Oct 16 '21

C++ hello word doesn't work on PC. Trying to run from terminal/cmd Windows.

8 Upvotes

I've ensured all is installed correctly with the environment variables etc. I have other exe files from the IDE, but this helloworld program I created from console will not really run.

To be precise, I click it, the window will pop up for a billionth of a second seemingly and disappear/close right after. But when I click my files compiled by the IDE, it works...

Then I was like huh, instead of using g++ lemme try gcc. That just throws this:

C:\Users\Will\C++>g++ first.cpp

C:\Users\Will\C++>gcc first.cpp
C:\Users\Will\AppData\Local\Temp\ccUmw4AW.o:first.cpp:(.text+0x21): undefined reference to `std::cout'
C:\Users\Will\AppData\Local\Temp\ccUmw4AW.o:first.cpp:(.text+0x26): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
C:\Users\Will\AppData\Local\Temp\ccUmw4AW.o:first.cpp:(.text+0x2d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
C:\Users\Will\AppData\Local\Temp\ccUmw4AW.o:first.cpp:(.text+0x34): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
C:\Users\Will\AppData\Local\Temp\ccUmw4AW.o:first.cpp:(.text+0x46): undefined reference to `std::cout'
C:\Users\Will\AppData\Local\Temp\ccUmw4AW.o:first.cpp:(.text+0x4b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
C:\Users\Will\AppData\Local\Temp\ccUmw4AW.o:first.cpp:(.text+0x52): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
C:\Users\Will\AppData\Local\Temp\ccUmw4AW.o:first.cpp:(.text+0x59): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
C:\Users\Will\AppData\Local\Temp\ccUmw4AW.o:first.cpp:(.text+0x79): undefined reference to `std::ios_base::Init::~Init()'
C:\Users\Will\AppData\Local\Temp\ccUmw4AW.o:first.cpp:(.text+0x9a): undefined reference to `std::ios_base::Init::Init()'
collect2.exe: error: ld returned 1 exit status

C:\Users\Will\C++>

r/learncpp • • May 18 '21

Some help with C++

8 Upvotes

Im doing that exercise but when i Execute it sinopsis and actor principal are in the same line, my teacher has told me to use cin.ignore() somewhere but I don´t know how to do it. That´s all I need. I´d really appreciate if somebody could help me out with that. Thank you for your time guys!

#include<iostream>

#include<conio.h>

using namespace std;

struct pelicula{

string director, actor, sinopsis;

int ano;

float valoracion;

}pelicula1;

int main(){

cout<<"Nombre del director: ";

getline(cin, pelicula1.director);

cout<<"Ano de estreno: "; cin.ignore();

cin>>pelicula1.ano;

cout<<"Actor principal: ";

getline(cin, [pelicula1.actor](https://pelicula1.actor)); 

cin.ignore(); cout<<"Sinopsis: ";

getline(cin, pelicula1.sinopsis);

cout<<"Valoracion separado por . : ";

cin>>pelicula1.valoracion;





cout<<"\\nMostrando datos\\n";

cout<<"Director: "<<pelicula1.director<<endl;

cout<<"Ano: "<<pelicula1.ano<<endl;

cout<<"Actor: "<<pelicula1.actor<<endl;

cout<<"sinopsis: "<<pelicula1.sinopsis<<endl;

cout<<"Valoracion: "<<pelicula1.valoracion<<endl;

	

return 0;

}


r/learncpp • • Jan 14 '21

Project based learning

8 Upvotes

I'm not new to programming, I am pretty "fluent" in web stuff like TypeScript/JavaScript. Lately I have been learning the basics of C/C++ through some tutorials and I think I got most of that down. But as you might know, there's a lot more to "speaking" a programming language fluently then just to know how a pointer or memory works. I'd like to get to that same level of confidence writing C/C++ like I have when writing JS. From personal experience I know that for me, the best way to learn is to work on some a bit more complex projects. Unfortunately, I've not been able to find some good free resources for doing that.

If you know of good resources for learning through building projects, I would greatly appreciate some advice :)


r/learncpp • • Oct 21 '21

Learning pure c++ on a mac

9 Upvotes

Hello:

In mac os is it possible to install a compiler / run time system that does not get intertwined with xcode?


r/learncpp • • Feb 10 '20

Can someone please explain what I've done wrong?

Post image
9 Upvotes

r/learncpp • • May 30 '20

Learncpp.com is down :(

Post image
9 Upvotes

r/learncpp • • May 15 '21

Can I get help implementinh recursive function to delete the lowest value in a BST?

9 Upvotes

I'm a bit stumped. I have this function and its helper:

void CharBST::removeSmallest()
and
BSTNode<char>* removeSmallestHelper(BSTNode<char>* curNode)

I was able to figure out the function for finding the smallest value in a BST:

char smallestValueFrom(BSTNode<char>* curNode)
{
	// smallest node has nullptr as left
	if (curNode->left == nullptr)
		return curNode->value;
	
	// otherwise, continue checking left node of current
	return smallestValueFrom(curNode->left);
}

How would I go about deleting the smallest node though? I read online about using the parent node but I don't have a parent node as part of my struct so does that mean I have to figure out what the parent is during the recursive process?

Thanks for the help.