r/learncpp • • Mar 10 '20

::Seeking Advice::

3 Upvotes

Been in the culinary arts(back of house fine dining chef) for coming up on 10 years and looking to make a career change to something that A.) I can make a legitimate living doing. B.) A job that I will truly be challenged with. C.) A career that has longevity. I’ve always LOVED/ been obsessed with video games and how I would change them if I could, but never knew how I could into that industry. I have been reading as much as I possibly can about C++ but I have ZERO knowledge of programming. Reddit and YouTube has been a great source of knowledge for resources of where to look and what to read but I am just now discovering this Sub, so I figured I’d post in here for advice on where to focus my attention if I’d like to learn about game dev specifically ?

Edit:: By “where to focus my attention” I mean should I watch more game dev tutorials or intro to programming tutorials being as I know nothing.


r/learncpp • • Jan 03 '20

question about pointers

3 Upvotes

I have a question about pointers regarding how to get the memory address. I understand that I am passing the variable by reference into the print_names function. I wanted to make sure that the name/address lines within the function were giving me the same address. As I understand it

colleges[i] is equivalent to *(colleges+i)

and

&colleges[i] is equivalent to (colleges+i)

is this because of the peculiarities of calling the pointer address for an array versus directly addressing the pointer via the dereferencing operator?

#include <cstdio>

struct College {

char name\[256\];

};

void print_names(College* colleges, size_t n_colleges) {

printf("memory address: %p\\n",colleges);

for (size_t i=0; i< n_colleges; i++) {

	printf("College %s \\n", colleges\[i\].name);

	printf("College name %s and addr %p \\n", colleges\[i\].name,&colleges\[i\]);

	printf("College name %s and addr %p \\n", (\*(colleges+i)).name,(colleges+i));

}

}

int main() {

College oxford\[\] = {{"Magdalen"},{"Nuffield"},{"Kellogg"},{"Crampus"}};

printf("Address of array: %p\\n", oxford);

printf("Size of college: %lu\\n",sizeof(oxford));

printf("Size of college struct: %lu\\n", sizeof(College));

printf("Size of array: %lu\\n\\n\\n",sizeof(oxford)/sizeof(College));

print_names(oxford, sizeof(oxford)/sizeof(College));

return 0;

}


r/learncpp • • Dec 12 '19

Please critique my first C++ program - to screenshot windows.

3 Upvotes

Link to the code -

main code:

https://github.com/stuaxo/capture_window/blob/master/capture_window/capture_window.cpp

project and README: https://github.com/stuaxo/capture_window

I normally develop in python on Linux, so there was a lot of S/O based development here.

It would be great to get feedback on if I did anything weird, and obvious bugs (I'm not used to having to free memory for instance).

I wrote this almost a year ago, but only now have time to come back to it.


r/learncpp • • Dec 08 '19

[C++11/14/17/20] Can't get rid of warning C26444 in Visual Studio

Thumbnail self.learnprogramming
3 Upvotes

r/learncpp • • Nov 22 '19

Instantiating a class type within a switch statement?

3 Upvotes

I am attempting to create a game-selection screen. I am receiving IDE clang errors when trying to create a new class instance within a switch statement. The instantiation syntax is OK when used within main. Why can I not use it within a switch?

#include <iostream>
#include "Games/RockPaperScissors.h"
#include "Games/GuessMyNumber.h"

using namespace std;

void gameSelect()
{
    unsigned sel;

    cout << "Welcome to Mallery's game hub v1.0" << "\n"
         << "Please select a game to play: " << "\n"
         << "1) Rock Paper Scissors (V1.1)" << "\n"
         << "2) Guess My Number (V1.0)" << endl;

    while(!(cin >> sel) || (sel > 2))
    {
        cout << "That is not a valid selection" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    switch(sel)

        case 1:
            App = new RockPaperScissors();    // 'Use of undeclared indentifier App'
            App.startNewGame();
            break;

        case 2:
            GuessMyNumber App2;    // 'Use of undeclared indentifier App2'
            App2.startGame();
            break;
}

int main()
{
    /// Start our game selection screen using switch statement.
    gameSelect();

    /// Initializing like this works fine, but we cannot pick between the games.
    RockPaperScissors App;
    App.startNewGame();

    GuessMyNumber App2;
    App2.startGame();

    return 0;
}

r/learncpp • • Oct 19 '19

C++ book with deep practice problems

3 Upvotes

Are there any good C++ books that have questions left to the reader after every concept that is introduced?


r/learncpp • • Jul 16 '19

sstream << std::setw(2) << std::setfill("0"); without "<<"

3 Upvotes

Hey,

is it possible to write sstream << std::setw(2) << std::setfill("0"); without "<<" operator? as iam getting Unsequenced function calls?

sstream.setw(2) doesn't seem to work.

Thanks in advance!


r/learncpp • • Jun 27 '19

How does copy elision work on a stack-frame level?

3 Upvotes

I understand N-RVO on an abstract level: the compiler uses the memory space of the object to directly construct it, even if it is being initialized within the function. Through doing so, because direct copying from the function's memory space to the object's is unnecessary, the compiler can safely get rid of unnecessary temporary copies that are generated within the function.

OK... but what is a way I can understand what exactly is going on down below, on a step-by-step basis? How does the compiler achieve this, exactly? Does anybody have some good resources to share or want to help me on the way to true understanding?


r/learncpp • • Jun 02 '19

Another help post: passing and isPositive statement using bool

3 Upvotes

Again im working from the packt "learning c++ by building games with unreal engine 4: second edition"

the exercise asked us: "Write an isPositive statement that returns true when the double parameter passed to it is indeed true."

my code was way wrong but so is the books solution, which is:

bool isPositive( double value )

{

return value > 0;

}

now on my screen this executed by its self returns errors, if i use:

#include <iostream>

using namespace std;

int main()

and then their solution i still get errors.

the debugger is telling me that an ';' is expected on line 6

and telling me that 'isPositive' being used locally is illegal. (idk where they get a sick bird but...)

I'm just kinda confused on why this is like the 4th time in as many chapters this book's code copied is coming up errors that im not equipped enough yet to understand?

any help would be greatly appreciated! thanks again!!


r/learncpp • • May 05 '19

OOP

3 Upvotes

Quick question.

Why should you create private classese in c++?


r/learncpp • • May 03 '19

I'm somewhat new to C++ programming. Any tips and tricks I should know?

3 Upvotes

After playing around with Scratch back in my first year of high school, I moved to a new school where they said they'd input C++ robotics into the school system in the next school year. What are some basic things I need to know in order to learn this new type of programming?


r/learncpp • • Apr 21 '19

Places to learn

3 Upvotes

I just started coding with c++ and I’m wondering where I could find tutorials/exercises for advanced c++. I just finished some beginner stuff with this YouTube video: https://youtu.be/vLnPwxZdW4Y I want to get into the more advanced stuff. If anyone knows any free/cheap tutorials I would love to hear it!


r/learncpp • • Apr 19 '19

'int' to 'const char *' for 1st argument

3 Upvotes

Hej!

How can i convert an int to char const *? I wrote a function int Log(char const *message) that accept char const * msg as parameter to print it. But i also want to use the same function to print int value.

i tried to convert it with `reinterpret_cast<const char *>(value)` but it doesn't print anything.

#include <iostream>

int Log(char const *message) {
 printf("%s\n", message);
}

int multiply(const int a, const int b) {
 Log("Multiply function called!");
 return a * b;
}

int main() {
 int value = multiply(2, 3); // value = 6  :p 
 Log(value); // known conversion from 'int' to 'const char *' for 1st argument
 Log(reinterpret_cast<const char *>(value)); // nothing is printed
}

Thanks in advance


r/learncpp • • Apr 18 '19

Atbash Cipher incomplete

3 Upvotes

Hello everyone, I'm trying to create an Encryption program. The key is A = Z, B = Y. The program is working from A -> M, the program is encrypting words that has letters from A -> M, but the problem is, it won't encrypt words that has letters with N -> O in it. What should I do guys?

#include <iostream>
#include <string>
using namespace std;
int main() {
    string RealAlpha ="abcdefghijklmnopqrstuvwxyz";
    string RevAlpha = "zyxwvutsrqponmlkjihgfedcba";
    string word;
    cout << "Enter word: ";getline(cin,word);
    for(int i = 0;i < word.length(); ++i){
        for(int j = 0;j < 13;j++){
            if(word[i] == RealAlpha[j]){
                word[i] = RevAlpha[j];
            }
        }
    }
    cout << word << endl;
    return 0;
}

Output:

Enter word: hello

svooo

A reply will be greatly appreciated!


r/learncpp • • Apr 10 '19

C++ Primer 5th Edition Is Challenging

3 Upvotes

Hello,

I am an experienced C# and Python developer who has recently had an interest in learning CPP. As per many online recommendations I bought C++ Primer 5th edition. As it stands, I am on page 200 and am not liking the structure of this book. The way topics are brought to light and taught are kind of confusing for me. For example the book spends explaining and teaching classes without introducing the 'class' keyword and its place. All in all, this book is not for me.

Does anybody have any other good book recommendations for C++11 or even more up to date.

Thank you,


r/learncpp • • Mar 19 '19

Is there a source where various c++ (or just in general) concepts are visualized by real life analogies?

3 Upvotes

When teaching OOP one often sees how it is tried to equate different c++ concepts with real life examples, like "is-a" relationship and "has-a" relationship etc.

Is there a source which teaches similar "rule of thumbs" so that one can visualize those different concepts? Unfortunately I only managed to find such analogies only when discussing inheritance or other some other OOP situation as to when to use the keyword status.


r/learncpp • • Jan 20 '19

If you are playing with GLFW and struggling to setup CMake I want to share this config that worked like a charm!

Thumbnail
reddit.com
3 Upvotes

r/learncpp • • Dec 21 '18

C++ Quick Reference

Thumbnail
github.com
3 Upvotes

r/learncpp • • Nov 13 '18

I don't understand why I'm getting undeclared identifier, coming from java background

3 Upvotes

So this is for a software engineering class, I was assigned by the professor to handle the entire implementation for my whole group for our semester project...one person...semester project...whole implementation...in just a week and a half...

Lamenting aside, I think I have...something, and I'm trying to test it but when I try to run the test file I keep getting the following errors

AlgorithmTest.cpp

c:\users\fool\source\repos\consoleapplication2\algorithmtest.cpp(16): error C2065: 'Algorithm': undeclared identifier

c:\users\fool\source\repos\consoleapplication2\algorithmtest.cpp(16): error C2065: 'algie': undeclared identifier

c:\users\fool\source\repos\consoleapplication2\algorithmtest.cpp(16): error C2061: syntax error: identifier 'Algorithm'

Now the issue probably is I don't know how to import things in cpp, but when I tried google I couldn't find what I was doing wrong. At first I thought the issue could have been errors in the original file prevented it from compiling and being used in the test harness but the only things I'm getting from the original file are

algorithm.cpp

c:\users\fool\source\repos\consoleapplication2\algorithm.cpp(35): warning C4244: '=': conversion from 'time_t' to 'long', possible loss of data

c:\users\fool\source\repos\consoleapplication2\algorithm.cpp(44): warning C4244: '=': conversion from 'time_t' to 'long', possible loss of data

c:\users\fool\source\repos\consoleapplication2\algorithm.cpp(63): warning C4244: '=': conversion from 'time_t' to 'long', possible loss of data

c:\users\fool\source\repos\consoleapplication2\algorithm.cpp(132): warning C4244: '=': conversion from '__int64' to 'long', possible loss of data

c:\users\fool\source\repos\consoleapplication2\algorithm.cpp(144): warning C4244: '=': conversion from '__int64' to 'long', possible loss of data

Now I know the above is a mess but if it's only a warning wouldn't it not cause the issues (though if you even have tips for that feel free to go ahead and tell me them)

The following is code for AlgorithmTest.cpp

https://paste.ofcode.org/hikFdc97FUVCkC6yuaAB6R

The following is algorithm.h

https://paste.ofcode.org/LTdr3KVRQNTez3P4SsMnYM

The following is algorithm.cpp

https://paste.ofcode.org/hQj3pMHsH9PetzCFD7wA2N

Regardless of how poorly thought out some design decisions were (but again feel free to tell me how poorly thought out they are and how to improve them) what could be causing the undeclared identifier errors? Do I simply not understand how to import in cpp? If so can someone explain it to me? I've taken classes in cpp (like a data structures class, an operating systems class) and never run into this issue but I have maybe 6 months of on and off experience with the language so I could just not understand it.

UPDATE:

Thanks to the two helpful people below, and a lot of review of past code, classes, slide, I have gotten rid of all excessive errors and reformatted a lot of my code:

https://paste.ee/p/IHlm5

Now I just have the same three damn errors

1>c:\users\fool\source\repos\consoleapplication2\algorithmtest.cpp(16): error C2065: 'Algorithm': undeclared identifier

1>c:\users\fool\source\repos\consoleapplication2\algorithmtest.cpp(16): error C2146: syntax error: missing ';' before identifier 'algie'

1>c:\users\fool\source\repos\consoleapplication2\algorithmtest.cpp(16): error C3861: 'algie': identifier not found

I've been fiddling with the different ways I found online to declare an object and I can't understand what I'm doing wrong.


r/learncpp • • Oct 07 '18

Having a tough time with Pointers and References in Functions

3 Upvotes

Hello All,

Ive been reading a lot about pointers and reference and how they work. So far Ive come to the conclusion that a pointer can point to a memory location, and then to another one, etc. While a reference can only act as a const alias to another variable.

While I understand that they work in similar ways I am having a hard time understanding why we would want to use one over the other, especially in functions Such as:

const &Money const getSalary {return salary;}

Here we are assuming that Money is the name of the class. Why do we want this to be an alias and not a pointer?


r/learncpp • • Oct 06 '18

C++ for C programmers

3 Upvotes

I just taught a 1 day workshop with the above title. Course material can be found here: book and lecture

Share and enjoy. Feedback welcome.


r/learncpp • • Dec 17 '16

Released a new video in my tutorial series

Thumbnail
youtube.com
3 Upvotes

r/learncpp • • Aug 29 '16

Classes & Headers in C++

3 Upvotes

Hello all. This is my first reddit post, and incidentally my first forum question(!|.) Background: I've decided that the language i would like to learn at home as a hobby is C++, as it is versatile, portable, and most of all fairly verbose (as odd as this sounds, I really like verbose language styles...). I've checked various websites & help files but I'm having an (un-encouragingly) large amount of difficulty understanding a few things: Headers/Class files:
-) What should (ideally) go in the *.hxx file?
-) What therefore should go in the *.cxx file?
It's so far my understanding that actual classes should be defined in the header file, along with variables for the class, and that the constructor & the main logic of the class should be in the class file itself. Please correct me if i am wrong!
-) Pointers! I understand the concept of a fixed address in memory (*) of a certain type, and therefore corresponding size. I also understand referring to a memory addressed value (&).
What i don't understand is how pointers (which I believe are allocated on the heap) are accessible between classes and functions.
My line of though is that it is inherently publicly accessible by name due to it being allocated on the heap, but may be totally wrong! If someone could enlighten me on how this is applied i would be most grateful.

Many thanks, G

Edit: formatting....


r/learncpp • • Sep 27 '15

Can you have a 'server' running in c++ and connect to it with a java desktop program?

3 Upvotes

Pretty much just title. If the question isn't clear enough please ask. I didn't know a better way to formulate that question


r/learncpp • • Aug 03 '15

Where should I start if I have an intermediate knowledge of C++?

3 Upvotes

I know almost about the syntaxes and use of almost every basic command(loops, functions, arrays, pointers, structures, classes, e.t.c.), I can solve easy to medium difficult level problems. I have yet to learn some advanced things like inheritance and such, and I find it really difficult to solve difficult problems, plus I don't think I know about all the different built in functions.

Can anyone here guide me what tutorial, book or project I should pick up so that I can get more familiar with the language?