Trying to decide which programming language I want to learn

AI Thread Summary
Choosing a programming language to learn can be challenging, especially for someone with a background in older languages like assembly, Fortran, and Pascal. C# and C++ are considered for firmware design, while Python is favored for its ease of use and relevance to gaming, particularly with grandchildren. C++ is noted for its speed and compactness, making it suitable for game programming, but it may require more effort to learn compared to C#. Resources like Visual Studio for C# and various online tutorials can help beginners get started, while microcontroller programming can be explored through platforms like Arduino and Raspberry Pi. Ultimately, the choice should align with personal interests in scientific or gaming applications.
  • #101
This is why I would recommend that you start with C or some other language like Python. They will be a lot more fun and easy to learn. Here is some documentation for constexpr. Even as a professional programmer, I would not like to read it. C++ is full of things like that. The only thing I would miss in C is object-oriented programming. There are a lot of languages that allow OO programming.
 
Technology news on Phys.org
  • #102
Last edited:
  • #103
  • #104
FactChecker said:
If all you need is the documentation, then what is wrong with this? constexpr specifier (since C++11)
I hope if I have 4 books, one of them is going to have answers for me. Like you and other said, no one book is going to explain all the syntax, but one of them hopefully will have the answer for me so I don't have to keep asking simple question here.

Thanks
 
Last edited:
  • #105
Sorry guys, I got it. I was confused. I originally thought line 3 is part of the syntax to define the constexpr of the array in line 10. I see line 3 is just defining of the variable Square. Line 10 just use the variable to put it in a CONSTANT array.

Half a day of walking away from the problem gives me a fresh eye to look at it again.
C++:
#include<iostream>
using namespace std;
constexpr int Square(int number) { return number * number; }

int main()
{
    const int ARRAY_LENGTH = 5;

    int myNumbers[ARRAY_LENGTH] = { 5, 10, 0, -101, 20 };
    int moreNumbers[Square(ARRAY_LENGTH)];

    cout << "Enter index of the element to be changed: ";
    int elementIndex = 0;
    cin >> elementIndex;

    cout << "Enter new value: ";
    int newValue = 0;
    cin >> newValue;

    myNumbers[elementIndex] = newValue;
    moreNumbers[elementIndex] = newValue;

    cout << "Element" << elementIndex << "in array mynuNumbers is: " << myNumbers[elementIndex] << endl;

    cout << "Element"<< elementIndex <<" in array moreNumbers is:" << moreNumbers[elementIndex] << endl;

    return 0;
}

Sorry for all the troubles
 
Last edited by a moderator:
  • #106
One thing that you should realize is that the documentation of C++ is so large that on-line and computerized documentation has huge advantages and might be the only practical way to do it. They can include hyperlinks, search tools, and syntax-dependent recommendations that no number of books can match.
 
  • Like
Likes sysprog
  • #107
FactChecker said:
One thing that you should realize is that the documentation of C++ is so large that on-line and computerized documentation has huge advantages and might be the only practical way to do it. They can include hyperlinks, search tools, and syntax-dependent recommendations that no number of books can match.
I use google search on everything, but actually to my big surprise, I find searching on line is not helpful for me in C++. This is because I am barely starting to learn, all the links are from like forums where people are much more advance than me. When I read their programs, there are more terms that I don't know than I know. So the programs don't even make sense to me. I don't see and simple explanation on anything.

On line search is very useful if someone already know the subject and just have a specific question. I am surprised like if I have question on very basic electronics, you actually can find article teaching you what is V=IR. But I yet to see any on C++ that get down to this fundamental stuffs. So it's worth to spend extra money to get a few textbooks.

Actually, it's really not the book this is the problem this time, I must be more tired than I realize. I just got stuck in something really simple. I thought both statement ( line3 and line 10 are one syntax), but actually line 3 is just simple definition of a constant(Square), then the constexpr just use that constant to define the size of the array. It's just that simple and I was to blind to see it. After bickering here and wrote a bad review on the book instead of keep working on the program, this morning, when I look at the codes, it just dawn on me that I was stuck in the wrong direction! Now I still have to go and delete my review on the book!

Now I know better, don't rush, don't just keep at it more than 3 or 4 hours a day. I don't have a deadline. I actually read and understand the rest of the Chapter 4 with strings with no problem. I am not in the 20s like before that I can just keep at it anymore.
 
  • #108
You can start with your own simple programs and limit how much of C++ you want to get into. If you are trying to understand other people's code, then they might take you down every C++ rabbit hole that they put in their code. The example code using constexpr is like that. I gave you a link to the documentation on constexpr. That is the best I can do. If you can understand that documentation, then you are doing good. If you can not, then you may want to change your approach so that you can limit the parts of C++ that you need to learn and use. C++ code can be as simple as C, or it can be extremely complicated. Do not expect the complete documentation of C++ to be understandable without years of study and hard work.
 
  • #109
I have a question in this program. I am playing with adding something in the same program I had issues... I added a line to check the size of the array "moreNumbers[]". That is line 27. I learn it from the character string section to check the length of an array by command sizeof(Array).
C++:
//Assign Values to Elements in an Array
#include<iostream>
using namespace std;
constexpr int Square(int number) { return number * number;}// set constant function Square.

int main()
{
    const int ARRAY_LENGTH = 5;

    int myNumbers[ARRAY_LENGTH] = { 5, 10, 0, -101, 20 };
    int moreNumbers[Square(ARRAY_LENGTH)];// replace the constant number for length with a constant function.
   
    cout << "Enter index of the element to be changed: ";
    int elementIndex = 0;
    cin >> elementIndex;

    cout << "Enter new value: ";
    int newValue = 0;
    cin >> newValue;

    myNumbers[elementIndex] = newValue;
    moreNumbers[elementIndex] = newValue;

    cout << "Element" << elementIndex << "in array mynuNumbers is: " << myNumbers[elementIndex] << endl;

    cout << "Element" << elementIndex << " in array moreNumbers is: " << moreNumbers[elementIndex] << endl;
    cout << "The size of moreNumbers[] is: " << sizeof(moreNumbers) << endl;
    return 0;
}

If you run the program, it will return the size is 100 instead of 25. What did I do wrong?
 
  • #110
In your definition of the square, write

return (number*number)

that might be the issue.
 
  • #111
sizeof returns the size in bytes and there are 4 bytes per int
 
  • Like
Likes FactChecker and yungman
  • #112
Jarvis323 said:
sizeof returns the size in bytes and there are 4 bytes per int
How can I forgot that!

Thanks
 
  • #113
Jarvis323 said:
sizeof returns the size in bytes and there are 4 bytes per int
I even modified the array by declaring the array is characters: char morNumbers[]

The length now say it's 25 as characters are 1byte long only.

Thanks
 
  • #114
Jarvis323 said:
there are 4 bytes per int

8 bytes if you're compiling 64-bit code. (And way back in the Stone Age there was code where an int was only 2 bytes, or even 1 byte.)
 
  • #115
I don't think that one byte is large enough to hold the numbers that you want to put in it. newValue is an integer and needs 4 bytes. You can always leave the array large enough for an integer and print sizeof(Array)/sizeof(int).
 
  • Like
Likes yungman
  • #116
FactChecker said:
I don't think that one byte is large enough to hold the numbers that you want to put in it. newValue is an integer and needs 4 bytes. You can always leave the array large enough for an integer and print sizeof(Array)/sizeof(int).
I am just verify the line sizeof() if I change to character as it's 1 byte. Just playing around. I am playing around with the example given by the book by adding things and try it out. It's been fun.

Thanks for the new syntax for integer ( 4 bytes)

I am not at the point of designing and writing my own program, so I just take the sample program, adding stuffs to try to get familiar with the terms and the syntax. I am finishing up chapter 4 tonight, I'll be working with chapter 5...Operators, expressions and statements tomorrow. That's should be a lot more interesting.
 
  • #117
Jarvis323 said:
there are 4 bytes per int
PeterDonis said:
8 bytes if you're compiling 64-bit code. (And way back in the Stone Age there was code where an int was only 2 bytes, or even 1 byte.)
An int is 4 bytes (currently) whether you're compiling 32-bit or 64-bit code. That is definitely the case with Visual Studio, and is probably the case with other compilers as well. The long long (AKA long long int) type is 8 bytes, as is unsigned long long.

When I was first learning C, in 1985, an int was the same size as a short (2 bytes), but all that changed along about 1995 or so. I'm not aware of any machines and compilers for which an int was only one byte. Not saying it didn't happen, but Kernighan and Ritchie were designing a language to be used under Unix, and surely the machine word size was greater than 8 bits.
 
  • #118
yungman said:
I am just verify the line sizeof() if I change to character as it's 1 byte. Just playing around. I am playing around with the example given by the book by adding things and try it out. It's been fun.
Thanks for the new syntax for integer ( 4 bytes)
That's not really syntax -- it's just the size (in bytes) of that data type. You can use the sizeof operator (it's considered an operator, like +, -, *, /, etc., rather than a function) to determine the size of any type or any variable.

When you do more with arrays (the C-style arrays as opposed to the C++ Standard Template Library array template) that are initialized, you can use sizeof to count how many elements there are in the array.

C:
int List[] = {2, 5, 8, 10, 11, 31, -2, 15, 25};

int size = sizeof(List) / sizeof (int);
When this code is run, size will be set to 9. sizeof(List) == 36, and sizeof(int) == 4, so the quotient gives the number of array elements. Similar code will work whatever the base type of the array is: int, short, float, double, structure type, and so on.
 
  • Like
Likes yungman and FactChecker
  • #119
EDIT: I changed "function" to "operator". Thanks, @Mark44 .
The sizeof() function operator can be very useful sometimes. If you make a structure that is a combination of characters, integers, floats, and smaller structures, the computer will often insert padding space to make things align for efficient use. It is tricky to know how big the final result is. The sizeof() function operator can tell you that.
 
Last edited:
  • Like
Likes yungman
  • #120
Anyone know how much one semester in the college C++ class covers? I am using this book:
https://www.amazon.com/gp/product/0789757745/?tag=pfamazon01-20

Seems like it's a lot to cover the whole book in one semester, I just want to know what chapters I have to cover to equal to one college semester. You can see the description of the chapters if you look inside the book in the Amazon link.

Thanks
 
  • #121
C++ used to be a one semester course but now many institutions expect you to learn the language in your spare time and only offer courses in specific applications in C++ for e.g. CFD.

For instance here is what the Cambridge Engineering faculty give you: http://www-h.eng.cam.ac.uk/help/tpl/languages/C++.html. The programiz link there is a good online tutorial with its own compiler that runs in the browser so you don't need to download anything.
 
  • #122
In my experience, C++ was just the language used in an object oriented programming course, and was the 3rd programming course you would take.

They skipped over the stuff in part 1 of that book (which was the subject of a previous course in C), and focus instead on classes, encapsulation, polymorphism, and inheritance (part 2). The course was project based and the book was just used for students as a reference.

A course following object oriented programming was data structures and algorithms. In that course you'd learn about some of part 3 in the book. The focus would be on the underlying algorithms and when to use them, analyze expected and worst case performance, and implement some of them and others yourself from scratch as part of different projects.

The content of parts 4 and 5 were not really covered.

No courses focused specifically on learning C++, just used basic/simple parts of C++ as a base to teach general concepts. I think that they expect people to learn the details of specific languages independently, as there are many of them and they change. Learning new languages and technologies independently is also a sort of long term survival skill in industry.

Anyway, I think your book is about 3 semesters worth of content, but they would include a lot of projects.
 
Last edited:
  • Like
Likes yungman
  • #123
FactChecker said:
The sizeof() function can be very useful sometimes.
Agreed it is very useful, but technically, sizeof is an operator, just like +, -, *, <<, ^, and so on.
 
  • Like
Likes FactChecker
  • #124
Jarvis323 said:
In my experience, C++ was just the language used in an object oriented programming course, and was the 3rd programming course you would take.

They skipped over the stuff in part 1 of that book (which was the subject of a previous course in C), and focus instead on classes, encapsulation, polymorphism, and inheritance (part 2). The course was project based and the book was just used for students as a reference.

A course following object oriented programming was data structures and algorithms. In that course you'd learn about some of part 3 in the book. The focus would be on the underlying algorithms and when to use them, analyze expected and worst case performance, and implement some of them and others yourself from scratch as part of different projects.

The content of parts 4 and 5 were not really covered.

No courses focused specifically on learning C++, just used basic/simple parts of C++ as a base to teach general concepts. I think that they expect people to learn the details of specific languages independently, as there are many of them and they change. Learning new languages and technologies independently is also a sort of long term survival skill in industry.

Anyway, I think your book is about 3 semesters worth of content, but they would include a lot of projects.
That makes me feel a lot better. I was looking at the book, there's no way they can cover the whole book in one semester. It is so deceiving that the tittle of the book is "Learn C++ one hour a day", one hour a day, it will take a long time! I put like 3 to 4 hours a day for almost a week, I barely cover the first 4 chapters and starting on the 5th. It's just have a lot of new names (terms) and it's so hard to remember. I don't think the book is that good, it goes over a lot of terms in the first 4 chapters with no details, then repeat again in the later chapters. Particular Chapter 4, it actually said for detail, refer to chapter 17!

So for someone like me that have not touch programming for 40 years, is it like 3 semesters to cover Part 1 to 3?( learning back C and other things I missed?). I decided not to touch C and bull through C++ because I took a quick look at C, the words used are different from C++, meaning I have to learn more terms. Learning all the terms is the HARDEST part for me. My weak point is remembering people's names, the last thing is to remember more terms. The rest are quite easy, other than one particular problem I got stuck, I pretty much going through the rest without problem at all.

I am hoping I can finish Part 1 to 3 by Christmas.

Thanks
 
  • #125
yungman said:
I am hoping I can finish Part 1 to 3 by Christmas.
You are putting in an impressive level of effort. I think that you will be in very good shape by Christmas. There is a lot to get oriented on initially and it can make progress seem slow. But I think that you may be beyond that and will start to get traction quickly.
 
  • Like
Likes yungman
  • #126
FactChecker said:
You are putting in an impressive level of effort. I think that you will be in very good shape by Christmas. There is a lot to get oriented on initially and it can make progress seem slow. But I think that you may be beyond that and will start to get traction quickly.
Thanks for you complement. I am trying hard to learn. But I think I bull through the 4 chapter too fast, too many new names. I am going to spend the next day to go through them again and update my notes to make sure I have a better feel of the names. It's all the class, function, object etc. that really throws me off. Just have to keep reading and hopefully they will sink in.
 
  • #127
Object Oriented Programming can come from left field sometimes. I came from a background of computer discrete event simulation and OOP was exactly what I wanted. Suppose you are simulating a road intersection and want to generate a stream of cars coming and going. You want each car to control itself according to some rules and to collect its own statistics without getting it confused with all the other cars. You would make a class of vehicles and each car that you generate (instantiate) is an object in that class. The class defines how it will operate and collect data (using methods and hidden data), but the object itself does all the work and collects data internally, without getting it confused with the other cars (objects). Thousands of cars can be generated (instantiated), do their thing (using methods defined for the class), and destroyed (using the destructor method) as they enter and leave the intersection. OOP greatly simplifies simulations like that.
 
  • Like
Likes yungman
  • #128
FactChecker said:
Object Oriented Programming can come from left field sometimes. I came from a background of computer discrete event simulation and OOP was exactly what I wanted. Suppose you are simulating a road intersection and want to generate a stream of cars coming and going. You want each car to control itself according to some rules and to collect its own statistics without getting it confused with all the other cars. You would make a class of vehicles and each car that you generate (instantiate) is an object in that class. The class defines how it will operate and collect data (using methods and hidden data), but the object itself does all the work and collects data internally, without getting it confused with the other cars (objects). Thousands of cars can be generated (instantiated), do their thing (using methods defined for the class), and destroyed (using the destructor method) as they enter and leave the intersection. OOP greatly simplifies simulations like that.
Wow, this is what I need. Perfect timing while I am reviewing the first 4 chapters. I have a few questions:1) I put this in the heading of my notes and I change a little. I highlighted the key words. I want to confirm using #include<iostream>:

Is iostream a HEADER file where std is the CLASS and cout is OBJECT?

2) What do you call the .cpp text file that I created in VS? Is the compiler transforms the text .cpp file into machine code called OBJECTIVE file? Then the next step is to link with other files to form the complete .exe file?

3) Do you call main() {multiple lines of codes} that is the body of the program as FUNCTION? In fact I can declare any myfunction() { multiple lines of codes} as FUNCTION within the .cpp text file.

I am sure I have more question later.

Thanks
 

Attachments

  • #129
I want to confirm declaration of constant:

int pi = 22/7 = 3
float pi =22/7 = 3.1429

So if I want decimal number, I declare float?
 
  • #130
I have another question about DYNAMIC ARRAY. It supposed to automatically increase the size as needed as shown in this program:
C++:
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> dynArray(3);// define dynamic array of 3 elements
    dynArray[0] = 365;
    dynArray[1] = -421;
    dynArray[2] = 789;
    cout << " Numberof integers in array: " << dynArray.size() << endl; //display the array length

    cout << "Enter another element to insert" << endl;
    int newValue = 0;
    cin >> newValue;
    dynArray.push_back(newValue);

    cout << " Numberof integers in array: " << dynArray.size() << endl;// Display the new array length

    cout << " The last element of array: ";
    cout << dynArray[dynArray.size() - 1] << endl; //The Nth element is dynArray(N-1)

    return 0;
}
I understand the line 17 the number of elements increased by 1 using .push_back() to stuff in the new number. My question is if I don't need that many elements, is there a command to SHRINK the array back?

Sorry I bombard with so many question. I am just going through chapter 1 to 4 with fresh eyes.
 
  • #131
yungman said:
I want to confirm declaration of constant:

int pi = 22/7 = 3
float pi =22/7 = 3.1429

So if I want decimal number, I declare float?
In your first line, pi is initialized to 3. In the second line, pi is initialized to 3.0. This is because 22/7 == 3, due to integer division. Because pi is declared as a float in that line, the value 3 gets promoted to a float value, 3.0.
 
  • #132
Mark44 said:
In your first line, pi is initialized to 3. In the second line, pi is initialized to 3.0. This is because 22/7 == 3, due to integer division. Because pi is declared as a float in that line, the value 3 gets promoted to a float value, 3.0.
Oh! I am still wrong! So how do I get 3.1428?

there goes to show it's never too dumb to ask!
 
  • #133
yungman said:
Wow, this is what I need. Perfect timing while I am reviewing the first 4 chapters. I have a few questions:1) I put this in the heading of my notes and I change a little. I highlighted the key words. I want to confirm using #include<iostream>:

Is iostream a HEADER file where std is the CLASS and cout is OBJECT?
No, std is a namespace: a collection of classes and other stuff. Yes, cout is an object -- a stream object.
yungman said:
2) What do you call the .cpp text file that I created in VS? Is the compiler transforms the text .cpp file into machine code called OBJECTIVE file? Then the next step is to link with other files to form the complete .exe file?
The .cpp file is your source code. The compiler translates it into machine code -- an object file (not objective file). Note that the meaning of object here is different from its use in object-oriented programming. The linker merges in code for whatever library functions you've used.
yungman said:
3) Do you call main() {multiple lines of codes} that is the body of the program as FUNCTION? In fact I can declare any myfunction() { multiple lines of codes} as FUNCTION within the .cpp text file.
Yes, main() is a function. Your program can consist of multiple functions. Note that in C, C++, you can't define a function inside of another function.
yungman said:
I am sure I have more question later.

Thanks
 
  • Like
Likes yungman
  • #134
yungman said:
Oh! I am still wrong! So how do I get 3.1428?

there goes to show it's never too dumb to ask!
Declare pi so that floating point division is performed.
C++:
double pi = 22.0/7;
Now because one operand is double, floating point division will be performed rather than integer division.
 
  • #135
yungman said:
I understand the line 17 the number of elements increased by 1 using .push_back() to stuff in the new number. My question is if I don't need that many elements, is there a command to SHRINK the array back?
The erase() member function of the vector class will shrink the array.
 
  • Like
Likes yungman
  • #136
From your notes, Ch. 1:
* #include <iostream> to perform cout to output streams to display using std::cout << follow by
the “asdfongfasd” for words etc. Where << operator to shift bits LEFT.
The << operator is overloaded, meaning that it has two different purposes.
When it appears with cout, the meaning is to send a stream of bytes to output.
When it appears in an arithmetic express, such as count << 2, it shifts all the bits left by two places.

Several operators are overloaded in C++. These include >>, which is used for both input as well as shifting bits; * is used for multiplication and pointer dereferencing, & is used for bitwise AND and for getting the address of a variable.
 
  • Like
Likes FactChecker
  • #137
Mark44 said:
From your notes, Ch. 1:
The << operator is overloaded, meaning that it has two different purposes.
When it appears with cout, the meaning is to send a stream of bytes to output.
When it appears in an arithmetic express, such as count << 2, it shifts all the bits left by two places.

Several operators are overloaded in C++. These include >>, which is used for both input as well as shifting bits; * is used for multiplication and pointer dereferencing, & is used for bitwise AND and for getting the address of a variable.
What is the meaning of overloaded? Just simple having two purposes? Why don't they call it dual purpose?
 
  • #138
yungman said:
What is the meaning of overloaded? Just simple having two purposes? Why don't they call it dual purpose?
There can be more than two purposes. In C++, but not C (unless it has changed in recent new standards), a function or operator can be overloaded. Here's an example showing the declarations (prototypes) of a function named fun with four overloads.
C++:
int fun(int, int);
int fun(unsigned, unsigned);
int fun(float, float);
int fun(double, double);
A possible motivation might be that each overload adds a pair of numbers of each type. This is a bit fakey, since the function's return type is int, and all overloads must share the same return type.
 
  • #139
yungman said:
What is the meaning of overloaded? Just simple having two purposes? Why don't they call it dual purpose?
They would have to call it n-purpose or multi-purpose because there is no limit. When you define a class, you are allowed to redefine the common operations to make sense in the context of that class. A class of vectors can redefine '+' to add vectors component-wise. A class of data can redefine '+' to put the new data into a database. etc., etc.
I would advise you not to get carried away with this because you are, in a sense, making your own context-sensitive language. One of my annoyances was when I had to figure out someone's code who thought that he would make his own language to do something that is simple and only one-case use. Some people always seem to think that they are creating the next great language.
 
  • #140
Mark44 said:
There can be more than two purposes. In C++, but not C (unless it has changed in recent new standards), a function or operator can be overloaded. Here's an example showing the declarations (prototypes) of a function named fun with four overloads.
C++:
int fun(int, int);
int fun(unsigned, unsigned);
int fun(float, float);
int fun(double, double);
A possible motivation might be that each overload adds a pair of numbers of each type. This is a bit fakey, since the function's return type is int, and all overloads must share the same return type.
I don't think I learn fun(int, int) type, what is this?
 
  • #141
yungman said:
I don't think I learn fun(int, int) type, what is this?
This is the declaration or prototype of a function with two int arguments, and that returns an int value. In a declaration, the names of the parameters are optional, but in the actual definition, the names are required.
 
  • Like
Likes yungman
  • #142
Mark44 said:
This is the declaration or prototype of a function with two int arguments, and that returns an int value. In a declaration, the names of the parameters are optional, but in the actual definition, the names are required.
Ah, it's like math function f(x,y) = x^2 + y^2.
 
  • #143
Mark44 said:
There can be more than two purposes. In C++, but not C (unless it has changed in recent new standards), a function or operator can be overloaded. Here's an example showing the declarations (prototypes) of a function named fun with four overloads.
C++:
int fun(int, int);
int fun(unsigned, unsigned);
int fun(float, float);
int fun(double, double);
A possible motivation might be that each overload adds a pair of numbers of each type. This is a bit fakey, since the function's return type is int, and all overloads must share the same return type.
Now that I understand fun() is a function with two arguments. How can the program have 4 lines each defining fun() differently particularly fun ( int, int) and fun ( double, double). You can have fun (double, double) be integer, but not the other way around.
 
  • #144
Hi
For once, I have no question! I am hot on the trod on Chapter 5. I looked at one example and I don't like the way the book did, I changed it. I even create functions inside the program so the main() can call and it works. This is on prefix and postfix
C++:
//Difference between prefix and postfix
#include <iostream>
using namespace std;

int PostfixInc()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = num1++;
    cout << "Result of Postfix increment num2=num1++:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}
int PostfixDec()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = num1--;
    cout << "Result of Postfix decrement num2=num1--:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}

int PrefixInc()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = ++num1;
    cout << "Result of Pretfix increment num2=++num1:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}
int PrefixDec()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = --num1;
    cout << "Result of Pretfix decrement num2=--num1:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}
int main()
{

    PostfixInc();
    PostfixDec();
    PrefixInc();
    return PrefixDec();

}

I know this is kindergarten stuff for you guys, but I am happy! I can't wait to get to the next topic on boolean stuffs etc. Then the next chapter is If-Then-Else...About time to move onto some real programming.:biggrin::biggrin:
 
  • Like
Likes FactChecker
  • #145
Here are a couple comments on your latest program.
1. It's not a good idea to do this: "using namespace std;" There's the potential of creating problems for yourself if you happen to choose identifiers that are also using the the std namespace.
A better choice is this, since you are using only two of the identifiers in that namespace:
using std::cout;
using std::endl;

2. None of your functions return anything useful, so it would be better to define them all as void functions, like this. The only thing I've done is to change the return type, and comment out the return statement. It is an error for a void function to return a value.
C++:
void PrefixDec()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = --num1;
    cout << "Result of Pretfix decrement num2=--num1:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    // return 0;   -- commented out, since a void function cannot return a value              
}

3. Call each of the functions like this: PrefixDec();
The fourth function you called like this: return PrefixDec(); -- this doesn't make any sense, because the returned value is not being stored in a variable or being displayed.
 
  • Like
Likes FactChecker
  • #146
Mark44 said:
Here are a couple comments on your latest program.
1. It's not a good idea to do this: "using namespace std;" There's the potential of creating problems for yourself if you happen to choose identifiers that are also using the the std namespace.
A better choice is this, since you are using only two of the identifiers in that namespace:
using std::cout;
using std::endl;

2. None of your functions return anything useful, so it would be better to define them all as void functions, like this. The only thing I've done is to change the return type, and comment out the return statement. It is an error for a void function to return a value.
C++:
void PrefixDec()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = --num1;
    cout << "Result of Pretfix decrement num2=--num1:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    // return 0;   -- commented out, since a void function cannot return a value          
}

3. Call each of the functions like this: PrefixDec();
The fourth function you called like this: return PrefixDec(); -- this doesn't make any sense, because the returned value is not being stored in a variable or being displayed.

I only use what I learn in the 4 chapters and a part of the 5th chapter. I have not learn "void" function.
Can you explain why it's not a good idea to use using namespace std? What is an identifier?

I use " return PrefixDec()" because it gave me the correct display. I never learn this, just saw an example in chapter 2 or 3. Then I just experiment. If I put return in the earlier statement, it will end without going to the function after that. If I don't put return, it gave me an error.

Remember, I do a lot of guessing here as I am only at the beginning of the book. I am just trying things out. The book is not good in the sense it gave out examples with stuffs that have not been covered yet, they even point out it would be in the later chapters. So I just experiment on my own to get the right outcome.

What is the right way to call a function?
 
Last edited:
  • #147
Good comments, but I would like to clarify this:
Mark44 said:
3. Call each of the functions like this: PrefixDec();
The fourth function you called like this: return PrefixDec(); -- this doesn't make any sense, because the returned value is not being stored in a variable or being displayed.
It should run fine, but the idea of returning a hard-coded 0 hidden in the PrefixDec function is questionable. The main() can return a zero to indicate a normal completion or a non-zero error code. What it returns should not be hidden that way in a lower-level function. Typically, if a lower level function can not complete normally, it can return an error indicator and the main can determine what it should do about that. Then main can decide what completion code to return to the operating system.
 
  • #148
FactChecker said:
Good comments, but I would like to clarify this:

It should run fine, but the idea of returning a hard-coded 0 hidden in the PrefixDec function is questionable. The main() can return a zero to indicate a normal completion or a non-zero error code. What it returns should not be hidden that way in a lower-level function. Typically, if a lower level function can not complete normally, it can return an error indicator and the main can determine what it should do about that. Then main can decide what completion code to return to the operating system.
I never even look the display in the debugger "5.2.exe (process 3372) exited with code 0" until you said this. I got the right answer on num1 and num2, I got it to display nicely the way I want it, that's as far as I went on checking.

What does that mean?
 
  • #149
yungman said:
I never even look the display in the debugger "5.2.exe (process 3372) exited with code 0" until you said this. I got the right answer on num1 and num2, I got it to display nicely the way I want it, that's as far as I went on checking.

What does that mean?
It just means that your main() told the operating system that it completed with a completion code 0. It means nothing except that your code always returns 0. If your code did any error checking, you can make main() return a non-zero value (zero usually indicates a successful completion). You decide what the main() returned value should be for different situations. It's up to you. You can run your program in ways that will take special actions if the program did not complete normally.
 
  • #150
I went back and look, I copied pasted something wrong in the original program, main on "return 0" in all the subfunctions. Now it makes a little more sense to me. Everything is the same EXCEPT I put "return 0" at the end of every subfunction and at the end of the main().
Code:
//Difference between prefix and postfix
#include <iostream>
using namespace std;

int PostfixInc()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = num1++;
    cout << "Result of Postfix increment num2=num1++:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}
int PostfixDec()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = num1--;
    cout << "Result of Postfix decrement num2=num1--:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}

int PrefixInc()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = ++num1;
    cout << "Result of Pretfix increment num2=++num1:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}
int PrefixDec()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = --num1;
    cout << "Result of Pretfix decrement num2=--num1:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}
int main()
{

    PostfixInc();
    PostfixDec();
    PrefixInc();
    PrefixDec();

    return 0;
}

This book has very few problem at the end. I am waiting for the other 2 books, hopefully they have more assignments I can write programs. For now, I can only play around like this.BTW, how come there's no color on my program this time? Just black characters only, but with all the indents.
 
Last edited:
Back
Top