Hi. New to C++ and using youtube tutorials

Hi. I'm hoping I can find some help here.
So... I'm watching this youtube channel, "Trevor Payne, Let's Learn C++", episode 4 of basics: https://youtu.be/5u6Oq06Iask. I'm trying to work on the final challenges (around timestamp 20:17). The first challenge, 1.) Create an int array with min/max int values.

Would it be like this:

#include <iostream>
#include <string>
using namespace std;

int main()
{
int my_array[0][1] = {-32767,32767};

cout << my_array[0][1] << endl;

string y;
getline(cin, y);
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;

int main()
{
int my_array[0][1] = {-32767,32767};

cout << my_array[0][1] << endl;

string y;
getline(cin, y);
return 0;
}
Last edited on
...Just making sure I'm doing it right if I am at all. Maybe someone else looking at the challenge and looking at my solution might think I'm totally misinterpreting the task.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <limits>
#include <climits>

int main()
{
    // create an int array with min/max int values.
    int min_max_values[] { std::numeric_limits<int>::min(), std::numeric_limits<int>::max() } ;

    int min_max_values_alt[] { INT_MIN, INT_MAX } ; // macro constants from <climits>

    std::cout << std::showpos
              << "min: " << min_max_values[0] << ' ' << min_max_values_alt[0] << '\n'
              << "max: " << min_max_values[1] << ' ' << min_max_values_alt[1] << '\n' ;
}

http://coliru.stacked-crooked.com/a/c8532b309d95165a

Get a Book; for instance:
'Programming: Principles and Practice Using C++, 2nd Edition' By Bjarne Stroustrup
http://www.informit.com/store/programming-principles-and-practice-using-c-plus-plus-9780321992789
Last edited on
IMO YouTube is not the best way to learn programming - though there are some good talks, but there is much more crab.

If you are an absolute beginner C++ isn't the best choice since it's currently the most complicated and difficult language. Have a look at Java, Python or C#.
I'm sorry, but how is Java simpler than C++?
Not trying to be an ass, I'm just curious as to why someone would suggest that language to a beginner.
Lehti wrote:
I'm sorry, but how is Java simpler than C++?

I'm inclined to agree!


C++
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <limits>
using namespace std;

int main()
{
   int a[] = { numeric_limits<int>::min(), numeric_limits<int>::max() };
   cout << "Array: ";
   for ( int i : a ) cout << i << " ";
}

Array: -2147483648 2147483647 



Fortran
1
2
3
4
5
6
program main
   implicit none
   integer, parameter :: imax = huge( 1 )
   integer :: a(2) = (/ -imax-1, imax /)
   write( *, * ) "Array: ", a
end program main

 Array:  -2147483648  2147483647



Java
- No, I'm not convinced!
I found learning Java much easier than C++.
Java doesn't have undefined behaviour which is a big problem for beginner, instead you get an exception with a stack trace printed, also the error messages are much clearer.

Better type safety - There's no automatic conversions that can cause problems.
1
2
string('*', 60)
string(60, '*')

Same applies between bool and int and probably more.

Java has garbage collection so there's no need to learn about smart pointer, move sematics, rvalues...
For a long time Java was the official teaching language so there much better learning material available. Show me a free online university course with automatic testing service in C++ like
http://mooc.fi/courses/2013/programming-part-1/

BTW: I don't consider you an ass. People have different opions. The only agreement that seems to exist is that C++ is the most complicated programming language.
Should a beginner really start with the most difficult instead of easier options ?
int my_array[0][1] = {-32767,32767};

This absolutely does not do whatever you're trying to do with it.
@Thomas I'm having the opposite experience, actually.
Yes, c++ code can leak like crazy if you're not extra careful, the error messages it gives you and a missing pair of parentheses can make you waste a lot of time, but I find writing c++ code much more satisfying than writing Java.
I think that learning the most difficult language will help you pick up other languages more easily. I'm studying C++ for my undergraduate course and more recently I've picked up C# and Python and I'm finding them a piece of cake, compared to C++ (yay for generic functions and Claes without a fugly syntax and yay for the simple syntax of Python).
Wow. Thanks for the responses.

I'm definitely staying with C++. I've already invested a couple months in trying to to learn. I'm not backing off and trying to learn another language. Besides, I'm having fun with it. I have Stroustrup's beginning programming book. It's really dry reading for me, but I read a little of it here and there.

As far as the code I provided, I think I understand what I'm doing wrong. I'll be back later with code.
To be honest, I don't think Stroustrup books are a good resource for total newcomers to the language. From the parts I've read, it's mostly written for people who are already familiar with programming (as in the logic behind how to program). I'm personally studying from Lippman's C++ Primer.

Additionally, I suggest not limiting yourself to exercises or challenges you find online, but try to write programs that solve problems you encounter in real life. Those types of programs are what are helping me improve my coding skills.
Fine if you want to continue with C++, but try to learn modern c++, not some old stuff.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>
#include <climits>

int main()
{
  std::vector<int> numbers = 
  {
    std::numeric_limits<int>::min(),
    std::numeric_limits<int>::max(),
  };

  for (int i : numbers)
    std::cout << i << '\n';
}
Thomas, I'm trying to follow this tutorial and what I know is basic basic stuff. With that being said, we haven't covered climits yet. Tutorial series are usually a procedural thing. I'm sure we'll get there. For now, this is what I know. I have yet to post my remedy. Stay tuned.

I'm a mechanic who will learn to be a c++ programmer.
"Covering" <climits> doesn't deserve a topic all onto itself, like <vector> or other data structures would. <climits> simply is a way replace the old C macros like INT_MAX with more generic/typesafe templates.

For your case, std::numeric_limits<int>::min() just means INT_MIN. It's a bit wordier but used exactly the same.

There's also good books listed at https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

______________________________________

If this is the first programming language you're learning, I would personally not get too worried learning about every nook and cranny, it's better to learn how to think like a programmer, by practicing breaking down problems into steps that you can solve.

PS: If we're talking about first languages, my college had Python classes for first-semester engineers, so while my first language was C++, I do think Python can be a very simple procedural language to learn basic logic with.
Last edited on
I wish I would have thought about learning python a few months ago. I'm getting pretty familiar with Visual Studio. I want to continue C++. You know, I understand a lot of it, but for some reason, my issue is putting it to type or having a conversation about it. My learning style with anything has been 'messing things up and doing better the next time around' and so on.

I can't afford courses right now, but I will able to eventually. When I am ready for formal training or certification, I want to ace this instead of trying to learn in a classroom. I hate school. I have a regular job which has nothing to do with programming
Last edited on
I was thinking about doing pluralsight
closed account (E0p9LyTq)
there are tutorials right here at cplusplus, not updated to C++14/17:
http://www.cplusplus.com/doc/tutorial/

Another site that is updated:
http://www.learncpp.com/

Learning how to use C++, especially how to use it well, will never stop. There will be new updates to the language, what is informally being called C++20 is being worked on now. As well as understanding how to use older features in better ways.
Topic archived. No new replies allowed.