This forum is in archive mode. You will not be able to post new content.

Author Topic: Day 1 of me learning C++  (Read 11634 times)

0 Members and 1 Guest are viewing this topic.

Offline Sparky712

  • Peasant
  • *
  • Posts: 117
  • Cookies: 14
    • View Profile
Re: Day 1 of me learning C++
« Reply #15 on: June 22, 2013, 01:51:08 PM »
to follow on from theifyppl's post, you have no doubt seen at least one escape sequence.
Code: (CPP) [Select]
mystring = "this is a string with a newline escape\n" ;


as you can see.... you may either use an endl; which ends the current line and starts a new one, or you may use \n which starts a new line.

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: Day 1 of me learning C++
« Reply #16 on: June 22, 2013, 05:22:15 PM »
Literals are the most obvious kind of constants. They are used to express particular values within the source code of a program. We have already used these previously to give concrete values to variables or to express messages we wanted our programs to print out, for example, when we wrote:

 
a = 5;
the 5 in this piece of code was a literal constant.
 
This is copy/pasted from the tutorial i'm going through. My question is, isn't "5" also the variable? And "a" the variable identifier?

I got a hang of how to write a code to display text, and solve math problems along w/ write strings but now I'm getting hit in the face w/ constants...


Are my notes correct? Could someone explain why I needed to use "int main ()" and its purpose and when I need it?
Code: [Select]
//Introduction to strings in C++


#include <iostream> //the basic input/output library for C++
#include <string> //the library to be able to use strings

using namespace std; //this is the entity in which we are writing code

int main ()
{
string mystring = "Hello World!"; //string is the variable and mystring is the variable identifier
cout << mystring; //cout is the basic statement line commonly used in C++
return 0; //return 0; tells the computer the code is over
}[code]
« Last Edit: June 22, 2013, 06:18:51 PM by Chef »
"To find happiness is to not always laugh."

Offline theifyppl

  • Serf
  • *
  • Posts: 44
  • Cookies: 20
    • View Profile
Re: Day 1 of me learning C++
« Reply #17 on: June 22, 2013, 06:33:51 PM »
5 is a literal constant because it won't change. You typed the value directly into the program. It sounds weird and obvious, but 5 is going to stay 5.

a = 5; is assigning the value of the literal constant 5 to the variable "a".

b = 0;

0 is a literal constant, I literally typed 0 into the program and 0 is going to stay 0. BUT, the value of b doesn't have to stay 0.

b++;

Now b is 1. The value of b isn't a literal constant. It's not constant at all. Am I making sense?

As for your other question, "int main()" is the main function of your program. It's the function that executes when your program is ran. The reason there's an "int" in front of it is because the function returns an integer (in your case, it returns 0). You'll understand this more when you get into functions. But for now, just know you'll need it in every program you write. The number it returns can be like a status code. Usually, when a program's main function returns 0, it means everything ran smoothly. But once you get into error handling, returning 1 (or some other status code) could mean that an error occurred.
« Last Edit: June 22, 2013, 06:35:56 PM by theifyppl »

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: Day 1 of me learning C++
« Reply #18 on: June 22, 2013, 06:38:55 PM »
Very nice response. I'm getting a hang of this!
I got it I will just keep using "int main ()" when starting to write until I get into the depth of functions.
My question from what you wrote is when you said:
b = 0;
b++; So by putting "++" next to a variable in the line of code it increases the value by 1?
 
Below is from my tutorial. How in the hell do they all equal the same value of 75? That is crazy!

75 //int
0113 //octal
0x4b //hexadecimal

All of these represent the same number: 75 (seventy-five) expressed as a base-10 numeral, octal numeral and hexadecimal numeral, respectively.
« Last Edit: June 22, 2013, 06:45:35 PM by Chef »
"To find happiness is to not always laugh."

Offline theifyppl

  • Serf
  • *
  • Posts: 44
  • Cookies: 20
    • View Profile
Re: Day 1 of me learning C++
« Reply #19 on: June 22, 2013, 06:54:48 PM »
Yes, in C++ and in some (if not most) other common languages, the "++" operator increases the value by 1. "b++;" is equivalent to "b = b + 1;". This is the idea behind the name of C++. It's predecessor (although still highly used) was a programming language called C. C++, hence the name, was supposed to be sort of the sequel of C (C + 1).

And ah, I see you've discovered other numbering systems besides base-10. I wouldn't stress about any of that right now because that's a whole other topic on it's own. You should just know that base-10 (our normal numbering system) is called base-10 because it goes from 0-9 (ten digits). Octal is base-8, and hexadecimal is base 16. Hexadecimal is 0-9, but it's base-16, so it needs six other "digits." To express those, it uses A, B, C, D, E, and F after 0-9. You can research how to read such values and convert them to base-10 if you'd like. There's also binary (base-2) if you want to research how to read and convert that as well. But I wouldn't worry too much about it, for now at least.
« Last Edit: June 22, 2013, 06:57:00 PM by theifyppl »

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: Day 1 of me learning C++
« Reply #20 on: June 22, 2013, 07:06:32 PM »
Yeah I decided to read that part of the tutorial, still not fully grasp it, and move onto the next. I just started using the escape codes. Backlash has a whole entire new meaning to me now! Lol.
Below is from the tutorial. I want to know the point of changing a (char type) to wide characters? It's basically just changing the font right?
 
Finally, if we want the string literal to be explicitly made of wide characters (wchar_t type), instead of narrow characters (char type), we can precede the constant with the L prefix:

 
L"This is a wide character string"

 Wide characters are used mainly to represent non-English or exotic character sets

So here is my new question! It did not explain to me what "double" is or when I should use it! Any thoughts?
Code: [Select]
   
//Below we will be defining constants \
 
this feature is handy when it comes to memory-consuming \
 
variables like 3.14159
   
#include <iostream>
 
#include <string>
 
#define PI 3.14159 //#define is a directive for the preprocessor
 
#define NEWLINE '\n'
 
using namespace std;
 
int main ()
   
{
     double r=5.0; //this is the radius
 
   double circle;
   
   circle = 2 * PI * r;
 
   cout << circle;
 
   cout << NEWLINE;
     return 0;
   
}
 
« Last Edit: June 22, 2013, 07:14:15 PM by Chef »
"To find happiness is to not always laugh."

Offline theifyppl

  • Serf
  • *
  • Posts: 44
  • Cookies: 20
    • View Profile
Re: Day 1 of me learning C++
« Reply #21 on: June 22, 2013, 07:22:42 PM »
A double is a double-precision floating point number. A "float" variable type is a value that allows decimal points, like "1.78". A double is the same thing, but with two times the precision.

For simple stuff like this, feel free to google it. You'll get a much faster response than posting here and waiting for someone to write back. Take a look at this: http://stackoverflow.com/questions/2386772/difference-between-float-and-double

The answer given to the question answers your question very well.

Edit: Sorry, I didn't see the other part of your post, but I'm sure you can find an answer with google as well. Not that I won't help you, I just haven't ever needed to use those.
« Last Edit: June 22, 2013, 07:26:20 PM by theifyppl »

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: Day 1 of me learning C++
« Reply #22 on: June 22, 2013, 07:25:03 PM »
I read that link. If double is so much more precise shouldn't I just always use it over float?
 
Also, does anyone know how to execute the code I wrote using MS Visual C++ 2010?
« Last Edit: June 22, 2013, 08:03:43 PM by Chef »
"To find happiness is to not always laugh."

Offline dense

  • Serf
  • *
  • Posts: 23
  • Cookies: 4
    • View Profile
Re: Day 1 of me learning C++
« Reply #23 on: June 23, 2013, 12:41:22 AM »
If double is so much more precise shouldn't I just always use it over float?

Sure, but it's more expensive in terms of memory.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Day 1 of me learning C++
« Reply #24 on: June 23, 2013, 07:18:35 AM »
Sure, but it's more expensive in terms of memory.

Not just memory, but math involving it too can impact speed and performance. Imagine games using math involving nth places when rendering a vertex matrix of a map, you would get like 5fps. In programming it is up to the coder to decide appropriate datatypes.
>>>import this
-----------------------------

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: Day 1 of me learning C++
« Reply #25 on: June 23, 2013, 07:56:52 AM »
Not just memory, but math involving it too can impact speed and performance. Imagine games using math involving nth places when rendering a vertex matrix of a map, you would get like 5fps. In programming it is up to the coder to decide appropriate datatypes.

And now it all makes since when I'd hear someone say "This game is coded like shit."
Lol!
"To find happiness is to not always laugh."

Offline waspxor

  • NULL
  • Posts: 2
  • Cookies: 0
    • View Profile
Re: Day 1 of me learning C++
« Reply #26 on: June 24, 2013, 07:32:41 PM »
I would advise you dive straight into using cstrings or char arrays.
Experience tells me cstrings are something you will see a large amount of in any C / C++ environment.


Personally I strongly prefer them to strings. Try both out and see which you like more~

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: Day 1 of me learning C++
« Reply #27 on: June 24, 2013, 08:18:25 PM »
I would advise you dive straight into using cstrings or char arrays.
Experience tells me cstrings are something you will see a large amount of in any C / C++ environment.


Personally I strongly prefer them to strings. Try both out and see which you like more~
Roger that. I just got introduced to string streams last night...
"To find happiness is to not always laugh."

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: Day 1 of me learning C++
« Reply #28 on: June 26, 2013, 01:32:40 AM »
Experience tells me cstrings are something you will see a large amount of in any C / C++ environment.

In my experience that isn't true but it probably varies a lot.  C++ code will almost entirely use C++ strings and then use c_str() to convert when necessary, C code will of course, use C strings.

Personally I strongly prefer them to strings. Try both out and see which you like more~

"like" is not a good judge here.  C++ strings are *always* better than C Strings (char* and char[]).  You can always convert from a C++ string to a C string if you need to (or want to....) but C++ strings have so much more useful features like size(), substr(), insert(), including auto managing memory of the buffer for you, something you would have to manually do with C strings (malloc and realloc is a pain!).
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: Day 1 of me learning C++
« Reply #29 on: June 26, 2013, 04:26:57 AM »
In my experience that isn't true but it probably varies a lot.  C++ code will almost entirely use C++ strings and then use c_str() to convert when necessary, C code will of course, use C strings.

"like" is not a good judge here.  C++ strings are *always* better than C Strings (char* and char[]).  You can always convert from a C++ string to a C string if you need to (or want to....) but C++ strings have so much more useful features like size(), substr(), insert(), including auto managing memory of the buffer for you, something you would have to manually do with C strings (malloc and realloc is a pain!).
So in C++ there are C strings and C++ strings? Oh lord, this is gonna be a pain to learn.
"To find happiness is to not always laugh."

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.