Hey guys, I'm new to c++ and i'm struggling quite a bit.
I've been watching thenewboston videos, tried reading a few E-books such as "jumping into c++ with alex allain" and "C++ for dummies" but it's too difficult.
I sort of get the hang of a few things such as
cout << "text" << endl;
But there a few basic things which I dont understand like intergers, voids, return types.
My English isn't too good so if I try looking up for help, it just doesn't make any sense and it's extremely frustrating. The tutorials I read/look up also have complicated words, or just go too fast.
An integer is simply a whole number, so it is a number that doesn't have any decimal points in it; a number like 7 or 302. The number that is stored in this variable can either be positive or negative if it is signed, or just positive if it is unsigned.
Void types are slightly more complicated than that but it simply means that there is no data type or is an unknown data type. When a function returns a void, it usually means that it returns no value whatsoever. When it is used as a pointer, it refers to a variable that has an unknown data type to it; so it can "point" to a variable of any data type. Don't worry about this for now as this is more advanced.
Finally, a return type is just what type of value the function returns when it finishes running, like an integer or a string.
Here is an example that simply gets 2 numbers as input, adds them together, then outputs the result.
#include <iostream>
using std::cout;
using std::cin;
int calculate(int, int); // function prototype
int main() {
int num1;
int num2;
cout << "Enter a number: ";
cin >> num1; // get input
cin.ignore(); // clear the buffer
cout << "Enter second number: ";
cin >> num2; // get second input
cin.ignore(); // clear the input buffer
int res;
res = calculate(num1, num2);
cout << "Result: " << res;
cin.get(); // pause the console
return 0;
}
int calculate(int num1, int num2) {
int result;
result = num1 + num2;
return result;
}
There's also parts such as Int main() which I do not understand
I understand Intergers are a whole number, but what does a whole number main even mean..
"int main()" is where the code starts from. It is just a function that returns an integer value; when it returns a value, the program ends. The value it returns tells the operating system whether the program closed normally or closed down due to an error.
+1 for tutorials on this website. An integer, int, is simply a number declared like int x = 4; or like the code given above the user can input the number.
Void is a function similar to your int main(). The exception is that you don't have a return in a void. An example would be:
Returns are used in your int functions like int main(). In your main function it is to tell the operating system that there was no errors. Other times you may want to return the result of something and our it into your main function like
1 2 3 4 5 6 7 8 9 10 11 12 13
int addition
{
int a = 5;
int b = 5;
return a + b
}
int main()
{
cout << addition();
return 0;
}
Which would give you "10" as an output. I hope I explained it well, I'm terrible at explaining
Just wondering - do you have an IDE / compiler ? What sort - people can give better help if they know exactly what you are using.
If so, try some stuff yourself and see what happens. What errors do you get from the code in your last post? Post them here and we can explain them to you.
I would recommend the clang compiler (llvm) it has nice error / warning messages that are easier to understand.
Also, whatever system you use, set the warning levels to their highest. On gcc or clang use -Wall -Wextra -pedantic as a bare minimum.
@atro
When it comes to a void there is no returns. It can be a little confusing at first with when to use returns. If there is a function like int main() there will be a return. I don't know how to explain when to use one or the other (maybe someone will see and tell you) as I can't word it. I will give an example of a void and an int function though:
void duh(int a)
{
if(a = 5)
{
cout << "Well of course 5 is equal to 5!" << endl;
} else {
cout << "That does not equal 5!!" << endl;
}
// No return here
}
int years(int year, int old)
{
int born;
born = year - old;
// return here
return born;
}
int main()
{
/* Whenever you have an int in a function like "void duh(int a)" you have to input a number */
duh(5);
cout << "You were born in: " << years(2014, 16) << endl;
//return here
return 0;
}
this would output
Well of course 5 is equal to 5!
You were born in: 1998
I use a void function when I simply want to carry out code for its "side effects". So if I had a function that created a main menu for a console application, I would set its return type to void since I don't need it to return anything. Whereas, if I had a function that did some sort of calculation as done above a couple of times, you need a return value like an integer.
Bear in mind that if you are working with decimals then you need a data type that can hold decimals, such as a double or float. If you didn't do so, then your program would probably crash and spit out an error.
@feare56
I'm not trying to be rude or anything but there is a tiny mistake in your code on line 3. You used the assignment operator rather than the comparison one.
To get it clear, to make a function ,
1) Specify return type, if none, the return type is void
2) Function name, this is what the function is referred as.
3) Arguments, these are parameters ro be passed on to the function, if no parameters, leave blank.
4) Function body, code and processing goes here.