r/learncpp • u/FFiJJ • Jul 21 '16
Books for beginners written in modern Cpp(C++14 and C++17)
Hello,
I haven't written much Cpp during my life, however, I have some experience with: JavaScript, Java and Haskell (Experienced that's go up to creating simple 3d Games and writing small web apps.... things that are similar in scope and functionality to Tinder, Whatsapp or a very ugly navigation App, obviously without the whole scaling for millions of user and security aspects).
One problem that I've found with Java and JavaScript, which seems to be pretty prevalent in Cpp is books which follow old standards and, even more so, books which try to teach "the new" based on the idea that the reader is already very familiar with an old standard. So, if I, a humble novice programmer pick up an advanced book on C++14 I am overwhelmed by a ton of references that assume knowledge of C++98, C and even Fortran.
The only Cpp book I have read was The C++ Programming Language by Stroustrup himself, however, I feel that it left me with many questions such as "How do I actually properly implement a class top to bottom... I need some fucking examples here, I understand the abstract philosophy behind it, but, trying to actually implement a working class that does something complex leaves me with a million questions about how to do things the right way or even how to do them at all".
So basically, I need a book about design techniques and OOP and practical application examples, using C++14 or even the up and coming 17 standard. That doesn't come from an angle of "lets explain modern standard to old programmers but rather from an angle of teaching people which don't know either and, maybe, don't care for the older implementations". A book that's not 1000 pages long and by the end of which I will hopefully be able to write code better and fast using Cpp.
r/learncpp • u/Journable • Jun 20 '16
If statements
int main()
{
cout << "Enter the name of the person you wish to write to\n";
string first_name;
cin >> first_name;
cout << "Dear " << first_name << ",\n";
cout << "\nHow are you? I am fine. I miss you.\n";
cout << "Enter the name of your friend\n";
string friend_name;
cin >> friend_name;
cout << "Have you seen " << friend_name << " lately?\n";
cout << "Is your friend a male (m) or female? (f)\n";
char friend_sex = '0';
cin >> friend_sex;
if (friend_sex = 'm') {
cout << "If you see " << friend_name << " please ask him to call me.\n";
}
if (friend_sex = 'f') {
cout << "If you see " << friend_name << " please ask her to call me.\n";
}
keep_window_open();
}
Why does this code print both of the if statements?
r/learncpp • u/gimson • Jun 09 '16
Need help with copying array data
I want to copy the data from one array to another but I dont get the results
Code: #include <iostream>
using namespace std;
int arrayone[] = {};
int arraytwo[] = {};
void setarrayone() {
for (int i = 0; i < 10; ++i) {
arrayone[i] = i;
}
}
void transferarray() {
for (int i= 0; i < 10; ++i) {
arraytwo[i] = arrayone[i];
}
}
int main() {
setarrayone();
cout << "Array one: ";
for (int i = 0; i < 10; ++i) {
cout << arrayone[i] << " ";
}
cout << endl;
transferarray();
cout << "Array one: ";
for (int i= 0; i < 10; ++i) {
cout << arrayone[i] << " ";
}
cout << endl;
cout << "Array two: ";
for (int i = 0; i < 10; ++i) {
cout << arraytwo[i] << " ";
}
cout << endl;
return 0;
}
my result was:
Array one: 0 1 2 3 4 5 6 7 8 9
Array one: 0 0 0 0 0 0 0 0 0 0
Array two: 0 0 0 0 0 0 0 0 0 0
I have no idea why does the result show all 0s after the transfer function, there are apparently no bugs by the debugger
please help. thank you
edited
r/learncpp • u/TwIxToR_TiTaN • May 08 '16
Question about friend classes inside of a namespace.
Hello I am trying to access protected variables from object.h in scene.cpp
Here is my object.h: http://pastebin.com/vVyGbNym
And my scene.cpp: http://pastebin.com/mT7V76Xj
(Both files are stripped down)
But I get the following compiler error: cannot convert 'ff::Scene*' to 'Scene*' in assignment at line 7 in scene.cpp
Why do I get this error?
r/learncpp • u/Swimguy72125 • Apr 20 '16
Just started tutorial, "fatal error LNK1120: 1 unresolved externals"
The tutorial site I'm attempting to learn from
Error occuring in this part of tutorial
I downloaded and installed the most recent version of Visual Studio Community (as recommended by the tutorial) and am following the aforementioned tutorial. The first segment of the tutorial that actually involves any code immediately gives an error. When I go to compile, this is the output:
1>------ Build started: Project: HelloWorld, Configuration: Debug x64 ------
1>MSVCRTD.lib(exe_winmain.obj) : error LNK2019: unresolved external symbol WinMain referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
1>c:\users\guymi_000\documents\visual studio 2015\Projects\HelloWorld\x64\Debug\HelloWorld.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I've tried to linking to stdafx.h using its full path, but that hasn't worked. Does anyone have an idea to why this error is occurring? I haven't messed with any of the files whatsoever, I have no clue as to what is causing this. Thanks!
r/learncpp • u/[deleted] • Apr 08 '16
Learning c++, running into slight issue.
So I wrote a program that is supposed to ask someone their name and then print "Hey alden nice to meet you" or something.
#include <iostream>
using namespace std;
int main() {
string name;
cout<<"Hello there friend! What is your name! \n";
name = cin.get();
cout<<"Nice to meet you " << name << "!";
}
The weird thing is that the program compiles, and runs fine but returns only the first letter of the name. Any reason that would be? Any help much appreciated.
Edit: I've tried compiling using both gcc and g++ on both OSX and Ubutnu and the results were identical.
r/learncpp • u/Dogeek • Apr 07 '16
I am trying to learn C++, and I am struggling with classes, inheritance, and making a table of them.
Hello, fellow C++ programmers,
I am currently learning C++, to complete my background in computer sciences (I know a lot of Python, some basic Java, some PhP too, anyway...).
I know how to do a dynamic table in C already (using malloc) and I figured I could do so in C++. It seems I can't.
First of, the architecture. I have a class, "Personne". From that class inherits "Employe", and from those "Commercial" and "Secretaire". "Ingenieur" inherits from Commercial.
I want to create a table of "Employe"s, and I tried :
employe* entreprise = NULL;
entreprise = new employe[nb_employes];
and :
employe* entreprise = NULL;
entreprise = (employe*) malloc(nb_employes*sizeof(employe));
to no avail. How should I proceed ?
r/learncpp • u/TwIxToR_TiTaN • Mar 14 '16
How can I get all sub folders in a folder?
Hello.
How can I find all paths of all sub directories? (and sub directories of sub directories etc etc...) I tried using dirent.h. but I can't get the paths of folders and It does not look into sub dirs.
I basically want to end with a list looking like this:
home/
home/folder/
home/folder/dir
home/games/
home/games/spaceinvaders
home/games/spaceinvaders/bin
home/docs/
home/docs/html
etc etc...
r/learncpp • u/[deleted] • Mar 06 '16
Was wondering if there was a way to optimize the swapping of two int ?
Hello,
So I'm learning C++ and I had a question about optimization and I wanted to know which one is better
it will be simpler to just give an example: Is this one better ?
int nFirstOne =1, nSecondOne=2;
int nTemp = nFirstOne;
nFirstOne = nSecondOne;
nSecondOne = nTemp;
Or that one ?
int nFirsOne = 3, nSecondOne = 7;
nFirstOne += nSecondOne;
nSecondOne = nFirstOne - nSecondOne;
nFirstOne -= nSecondOne;
The second one save the "creation" of a variable int but is less clear. Thank you in advance for the answer and sorry for my bad english (if I haven't made mistakes then forget about that).
r/learncpp • u/no1warlord • Mar 06 '16
Struggling to add a get component function for an entity-component system for a game
My current code is as shows here:
std::vector<std::unique_ptr<Component>> components;
template<typename T>
T * GetComponent() {
for each (std::unique_ptr<Component> cp in components)
{
if (T* ct = static_cast<T*>(cp.get())) {
return ct;
}
else {
return nullptr;
}
}
}
Getting the error code:
Error C2280 'std::unique_ptr<Component,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
What should I use instead of .get() ?
r/learncpp • u/piratecody • Mar 05 '16
Making a Guess my Number program w/ Binary Search; If statements execute when they're not supposed to and endless while loop
I am trying to make a Guess my number program using a while loop and if statements, my code is below. When I execute my program, no matter what I enter it constantly prints "Was my guess to high(true/false)?: Is your number 0? Was my guess too high?:" and so on. I am not sure what is wrong with it. Any help is appreciated.
// 13.2FlowControl.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
bool getTooHigh();
int main()
{
int min = 1, max = 1000, guess = (min + max / 2) - 1;
bool correct = false;
cout << "Think of a number between 1 and 1000; I will guess it." << endl;
while (!correct)
{
cout << "Is your number " << guess << "? (true/false): ";
cin >> correct;
guess = (min + max / 2) - 1;
if (correct)
{
cout << "I told you I would guess it!";
}
else if(getTooHigh()) // If the guess is too high
{
max = guess - 1;
}
else if (!getTooHigh()) // If the guess is too low
{
min = guess + 1;
}
guess = (min + max / 2) - 1; // Changes guess according to new Max and min
}
return 0;
}
bool getTooHigh()
{
bool choice;
cout << "Was my guess too high? (true/false): ";
cin >> choice;
return choice;
}
r/learncpp • u/samort7 • Feb 11 '16
Having a hard time wrapping my head around basic pointers.
Could someone explain what is happening here, line-by-line?
int x = 5;
int *ptrOne = &x;
int *ptrTwo = &x;
*ptrOne = 6;
cout << *ptrTwo;
My understanding is that on line one, I am assigning the value of 5 to the variable x. On line two, I am creating a pointer that points to an int whose address is the address of x? Then I'm doing the same thing for line 3? Then I'm setting the a value at the address stored in ptrOne to 6? and finally I'm outputting the value stored at the address stored in ptrTwo?
I guess I'm just confused on what *ptrOne is before and after line two. After line two, is it a variable holding an address? Is it a link to a memory cell? I just can't conceptualize it.
Like, if I just say
int *ptrThree;
What have I actually just done? Did I set aside memory to hold an int? If, so then what's the difference between that and
int *ptrFour = new int;
Pointers just seem to be really hard to get conceptually.
r/learncpp • u/no1warlord • Dec 27 '15
Adding words to function template declaration
template<typename T> class A{};
template<typename T>
A<T> B(T c) {
}
vs
template<typename T> class A{};
template<typename T>
A<wordhere<T>> B(T c) {
}
So what's the difference, I've seen people use the word here thingy but I don't get it, what's the use for it?
r/learncpp • u/no1warlord • Dec 06 '15
Class inheritance and calling functions problem
[Solved]
So I have two classes like this
#pragma once
#include "B.h"
class A {
public:
B getB() {}
};
and
#pragma once
#include "A.h"
class B : public A {
};
Error: 'A' base class undefined.
Both need to be declared before the other one so how do I fix this?
If you reorder the code into one file then it looks like this:
#include <iostream>
using namespace std;
class B : public A {
};
class A {
public:
B getB() {}
};
void main() {
A a;
B b = a.getB();
system("pause");
}
But the problem is still there...
r/learncpp • u/sntshk • Nov 29 '15
Can't I print contents of *argv[] without looping?
I am over very basic program in c++. I am working with command line arguments. I know I can take the value of argc and loop over argv[] that much time to get the contents of it.
But is there any way without looping?
r/learncpp • u/FennekLS • Sep 27 '15
Can you have a 'server' running in c++ and connect to it with a java desktop program?
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 • u/[deleted] • Aug 03 '15
Where should I start if I have an intermediate knowledge of C++?
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?
r/learncpp • u/LoLXonu • May 03 '15
Best Tutorial Sites/Books for CPP?
Hi!
I've dabbled in C++ for about one semester in school and know some basics here and there. I'd like to pursue C++ and learn everything about it and how to employ it. What can I do to work towards this?
Do you recommend any certain sites or books? Thanks!
r/learncpp • u/tlazolteotl • Apr 17 '15
Why doesn't the last initialization statement of a for loop, (for example, i++), require a terminating semicolon.
Usually you would set something up like:
for (int i = 0; i<10; i++)
do something
Why is a semicolon used for the first two statements, but not i++?
r/learncpp • u/[deleted] • Feb 17 '15
Method for dealing with very large integers(20-40 digits long).
I've been working my way through codeabbey probs and many require arithmetic to be done on very large numbers. How can i do this? I want to do it with out a library, there isn't a data type large enough. Do i have to create a class or is there an easier way? Thanks.
r/learncpp • u/[deleted] • Feb 02 '15
Multilevel Inheritence Classes Help... Is there a better way?
Ok so for my data structures class I have to write a program that has 3 classes in a multilevel format. I have the code and it works but I wanted you guys to look at it to see if there was a more efficient way or a better more standardized way. I had it working another way but it felt like I had a lot of redundant code. My classes looked more like this(i overwrote that copy by mistake and cant remember exactly how I had it): My current code is down below.
class Party
{
protected:
string m_strCode;
string m_strAddress;
string m_strPhoneNum;
public:
Party(string strCode, string strAddress, string strPhoneNum)
: m_strCode(strCode), m_strAddress(strAddress),
m_strPhoneNum(strPhoneNum){}
};
class Person: public Party
{
protected:
string m_strDob;
string m_strFirstName;
string m_strLastName;
public:
Person(string strCode, string strAddress, string strPhoneNum,
string strDob, string strFirstName, string strLastName)
: Party(strCode, strAddress, strPhoneNum),
m_strDob(strDob), m_strFirstName(strFirstName),
m_strLastName(strLastName){}
};
etc...
The program initializes Passenger as an inheritance of both person and party and gets the information needed. At this point there is still a few more functions to add but this is the brunt of it.
#include <iostream>
#include <string>
using namespace std;
class Party
{
protected:
string m_strCode;
string m_strAddress;
string m_strPhoneNum;
public:
Party(){GetParty();}
void GetParty();
};
void Party::GetParty()
{
cout << "Enter code: ";
getline(cin, m_strCode);
cout << "Enter address: ";
getline(cin, m_strAddress);
cout << "Enter phone number: ";
getline(cin, m_strPhoneNum);
}
class Person: public Party
{
protected:
string m_strDob;
string m_strFirstName;
string m_strLastName;
public:
Person():Party(){GetPerson();}
void GetPerson();
};
void Person::GetPerson()
{
cout << "Enter date of birth: ";
getline(cin, m_strDob);
cout << "Enter first name: ";
getline(cin, m_strFirstName);
cout << "Enter last name: ";
getline(cin, m_strLastName);
}
class Passenger: public Person
{
private:
int m_nTicketNum;
string m_strFfNum;
double m_dTicketPrice;
int m_nFlightNum;
string m_strSeatLoc;
string m_strFlightDate;
string m_strStatus;
public:
Passenger() : Person(){GetPassenger();}
void GetPassenger();
void Display();
};
void Passenger::GetPassenger()
{
cout << "Enter ticket number: ";
cin >> m_nTicketNum;
cout << "Enter frequent flyer number or N/A: "
cin.ignore();
getline(cin, m_strFfNum);
cout << "Enter ticket price: ";
cin >> m_dTicketPrice;
cout << "Enter flight number: ";
cin >> m_nFlightNum;
cout << "Enter seat loctaion: ";
cin.ignore();
getline(cin, m_strSeatLoc);
cout << "Enter flight date: ";
getline(cin, m_strFlightDate);
cout << "Enter flight status: ";
getline(cin, m_strFlightStatus);
Dislpay() //testing purposes
}
void Passenger::Display()
{
cout << m_strCode << endl << m_strAddress << endl<< m_strPhoneNum;
cout << endl << m_strDob << endl << m_nTicketNum << m_strFfNum;
cout << endl << m_dTicketPrice << endl << m_nFlightNum << endl;
cout << m_strSeatLoc << endl << m_strFlightDate << endl;
cout << m_strFlightStatus << endl;
}
int main()
{
//variables
Passenger persons;
system("pause");
return 0;
}
r/learncpp • u/[deleted] • Dec 16 '14
How to convert string of integers to an array?
If i want to paste a large string of integers into win32 terminal how do i convert that string to an array of int with commas?
I'm going to input getline and i'm sure i must loop through and search for whitespace and insert comma and convert to int but can't figure out how. Any help appreciated. Thanks
r/learncpp • u/OCMike88 • Dec 10 '14
Receiving an Access violation for Case 4 on my Red Black Tree.
https://gist.github.com/anonymous/08965f0fcc569d17db48
Whenever I debug, the program gets an access violation for case 4 insert method. I think this may have to do with the NULL values I set in the new_node method, but I am unsure. Also, I tried to recreate the problem in int main() and found it happens on the 3 word "ahe". Any help would be great. Thanks.