Trying to decide which programming language I want to learn

Click For 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.
  • #151
Suppose your main() had a line like:

C++:
int anErrorOccured = 0;
...
...
int PostfixIncReturnValue = PostfixInc();
if( PostfixIncReturnValue  != 0 ){
    cout << "ERROR: Could not PostfixInc. Error code = " << PostfixIncReturnValue  << endl;
    ...
    ... (take appropriate corrective action) 
    ...
    anErrorOccured = 5;  // Error code 5 indicates that a problem occurred in PostfixInc
}
...
...
return anErrorOccured;
Then your function PostfixInc could return an error code and it would be checked by main() and appropriate actions could be taken.
 
Technology news on Phys.org
  • #152
FactChecker said:
Suppose your main() had a line like:

C++:
int anErrorOccured = 0;
...
...
int PostfixIncReturnValue = PostfixInc();
if( PostfixIncReturnValue  != 0 ){
    cout << "ERROR: Could not PostfixInc. Error code = " << PostfixIncReturnValue  << endl;
    ...
    ... (take appropriate corrective action)
    ...
    anErrorOccured = 5;  // Error code 5 indicates that a problem occurred in PostfixInc
}
...
...
return anErrorOccured;
Then your function PostfixInc could return an error code and it would be checked by main() and appropriate actions could be taken.
I don't follow you. I put in the code in main(), I got rid of all the "..." and comments, it did something strange, but no error. Notice on the last program, I put in return 0 in all the subfunctions?

Why is the last program I posted have no color?
 
  • #153
I play with what you suggested, it is strange. I deleted 3 of the subfunction and just use postfixInc() to simplify and make it shorter. If you run it, it will display the num1 and num2 TWICE.

I intentionally set the condition IF statement to "if (PostfixIncReturnValue = 0)". As you can see, the last line of postfixInc() is "return 0", but the program won't display the error line.

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 main()
{
    PostfixInc();
    int anErrorOccured = 0;
    int PostfixIncReturnValue = PostfixInc();
    if (PostfixIncReturnValue = 0)    {
     cout << "ERROR: Could not PostfixInc. Error code = " << PostfixIncReturnValue << endl;
    };
    return 0;
}
 
  • #154
The "code" in my post #151 was only meant as pseudocode to give you the idea. It was to show how main() could detect an error indicator returned from PostfixInc, but you have nothing in PostfixInc that would send an error indicator. The reason that you get num1 and num2 displayed twice is that you are calling PostfixInc twice. This code shows one of the most common errors in C and C++ (I have made this mistake a million times.):
yungman said:
"if (PostfixIncReturnValue = 0)"
That does not test if the return value is 0. Rather it sets PostfixIncReturnValue to zero. It needs to be "if (PostfixIncReturnValue == 0)"
 
  • Like
Likes yungman
  • #155
yungman said:
Can you explain why it's not a good idea to use using namespace std?
I gave a reason for this in post #145.
yungman said:
What is an identifier?
A name for a variable, function, class, and so on. In your program, num1, num2, cout, PrefixInc, and main are identifiers.
yungman said:
I use " return PrefixDec()" because it gave me the correct display.
Your version of PrefixDec() would behave exactly the same if you called it in either of these ways:
PrefixDec();
return PrefixDec();
The last line of your function is return 0; . This causes the value 0 to be moved to the EAX register on your computer, but the calling function, main() is not set up to do anything with that value.
Since main() doesn't do anything with the returned value, there's no point in having the return statement in any of your four functions.
yungman said:
What is the right way to call a function?
It depends on whether the function is intended to perform an action or return a value. A function the just performs an action should be a void function. A function that is intended to return a value should be called so that the returned value is saved in a variable or printed or used in some calculation.
Here's an example of a void function.
C++:
void Print42()
{
    std::cout << 42 << std::endl;
}
Call this function like so: Print42();

Here's an example of a function that returns a value.
C++:
int HalfOf(int number)
{
    return number / 2;
}
This function can be called in any of these ways:
int answer = HalfOf(18); // answer will be set to 9
std::cout << HalfOf(answer); // displays 4, since 9 /2 == 4
int val = HalfOf(2) + HalfOf(4); // val will be set to 3

I don't know if you have seen any examples of functions with parameters yet in the book you're using, but I'm sure you'll see them in whatever chapter discusses functions.
yungman said:
BTW, how come there's no color on my program this time?
Because the BBCode tag you used was
Code:
 instead of [code=cpp].
 
  • Like
Likes yungman
  • #156
I have a more pressing question regarding VS. I first ran into problem with one program I wrote, so I was to exprimenting and "save .cpp as" "Temp.cpp" in another location(another folder). But it SAVED in .cpp of my original program ( meaning it erased my original .cpp that I intend to save). Then if I change the .cpp of my original program, it change my Temp.cpp that is in another location. So I ended up erasing both!

This cause me a lot of problem. All the other simulation programs, schematic, pcb layout programs, they all distinct this and never erase other file like this. How can I avoid this. This is causing me a lot of grieve.

I am running into a lot of problems with VS this last two days. I really don't have a chance to experiment with your suggestions as this is what causing the erasing and I lost most of the stuffs.

I ended up having to copy the .cpp from what I posted here before, but then I got wrong results when I ran the program. For the life of me, I just don't get it. That was the program that was working beautifully. I closed the VS, I even restarted the computer, it is still wrong. I double checked the .cpp, everything is right.
 
Last edited:
  • #157
I encounter a strange result I cannot explain.
C++:
#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 1;
}
int main()
{
    PostfixInc();

   int PostfixIncReturnValue = PostfixInc();
    if (PostfixIncReturnValue |= 0) {
        cout << " The return code = " << PostfixIncReturnValue << endl;
    };
    return 0;
}
When I run this it, it display this:
num1 = 101
Result of Postfix increment num2=num1++: num1 = 102 num2 = 101

num1 = 101
Result of Postfix increment num2=num1++: num1 = 102 num2 = 101

The return code = 1

Notice it repeats the same two lines 2 times instead once? If I take out the IF-THEN state, then it just display once like intended. Seems like the IF statement somehow run the PostfixInc(), so if I run the program, it goes through the PostfixInc() twice. Why?

ThanksBTW, I now understand the "void" instead "int" if I don't want anything to return from the subfunction. I understand I don't need to return anything from PostfixInc() etc. I am just experimenting the suggestions from you guys. Like I have not study the IF-THEN statement yet. I don't want to bore you guys too much on things I have not learn and waste your time trying to explain to me. I should just keep going and learn them later. they should all be there in the next two chapters.
 
  • #158
You have two calls to PostfixInc. One on line 15 and another on line 17. Line 17 is only used to set a variable for the IF condition of line 18. If you remove line 18, an optimizing compiler might realize that there is no need for the variable PostfixIncReturnValue and might be removing the line 17 call.
 
  • Like
Likes yungman
  • #159
yungman said:
I have a more pressing question regarding VS. I first ran into problem with one program I wrote, so I was to exprimenting and "save .cpp as" "Temp.cpp" in another location(another folder). But it SAVED in .cpp of my original program ( meaning it erased my original .cpp that I intend to save). Then if I change the .cpp of my original program, it change my Temp.cpp that is in another location. So I ended up erasing both!
In the dialog box for "Save Program.cpp As...", if you don't change the path, it will change the name of your source code file to whatever you typed in, and you will have two source code files -- one with the old name, and one with the new name. This is pretty much the behavior of any software. Use Windows Explorer, not Visual Studio, to look in the directories under the top level for your program. You may think that VS erased the files, but I doubt that it did this.

If you modified the path shown in the dialog box, VS will save a copy of Program.cpp with a new name at the new path you specified.
 
  • Like
Likes yungman
  • #160
C++:
   if (PostfixIncReturnValue |= 0) {
NOOOOOOOOOOOOOOOOOO! That should be:
C++:
   if (PostfixIncReturnValue != 0) {

I'm not going to go into detail here but if you don't believe me run this code:
C++:
#include <iostream>
using std::cout;

int main() {
  int test = 2;
  if (test |= 2) {
      cout << "Didn't expect that!\n";
  }
  if (test |= 1) {
      cout << "That seemed to work, but wait...\n";
  }
  cout << test;
}
 
Last edited:
  • Like
Likes FactChecker and yungman
  • #161
FactChecker said:
You have two calls to PostfixInc. One on line 15 and another on line 17. Line 17 is only used to set a variable for the IF condition of line 18. If you remove line 18, an optimizing compiler might realize that there is no need for the variable PostfixIncReturnValue and might be removing the line 17 call.
I guess the only way is to pull the cout << " " part out of the PostfixInc() and just return the information, then cout<<" " once in main() to display once while access the PostfixInc() twice.

But anyway, now I know.
 
  • #162
pbuk said:
C++:
   if (PostfixIncReturnValue |= 0) {
NOOOOOOOOOOOOOOOOOO! That should be:
C++:
   if (PostfixIncReturnValue != 0) {

I'm not going to go into detail here but if you don't believe me run this code:
C++:
#include <iostream>
using std::cout;

int main() {
  int test = 2;
  if (test |= 2) {
      cout << "Didn't expect that!\n";
  }
  if (test |= 1) {
      cout << "That seemed to work, but wait...\n";
  }
  cout << test;
}
Problem with old eyes, I thought that was |.
 
  • #163
yungman said:
C++:
    int num2 = num1++;
This is not a good idea. num1++ is ok, we know that it increments num1. But does int num2 = num1++; increment num1 first and then assign the value to num2 or does it do the assignment first? I can't remember*, and I don't want my code to depend on me remembering it properly. It is never necessary to do this (just replace with separate increment and assignment statements), and it won't even produce more efficient code (any modern compiler will optimise to the same code whether in one statement or two).

* Note that whatever the answer is there is a prefix incrementer ++num1 that does the opposite - now erase this from your memory. Only ever use i++ (and i--) and use them 'stand alone'.
 
  • Like
Likes FactChecker
  • #164
pbuk said:
This is not a good idea. num1++ is ok, we know that it increments num1. But does int num2 = num1++; increment num1 first and then assign the value to num2 or does it do the assignment first? I can't remember*, and I don't want my code to depend on me remembering it properly. It is never necessary to do this (just replace with separate increment and assignment statements), and it won't even produce more efficient code (any modern compiler will optimise to the same code whether in one statement or two).

* Note that whatever the answer is there is a prefix incrementer ++num1 that does the opposite - now erase this from your memory.
I am just following the book, I am not to the point to judge, just an exercise on prefix and profix.

I think I should just keep working out the examples. The book throws out too many things that it has not covered yet, all these likely to be resolved after I go through a few more chapters.
 
  • #165
pbuk said:
But does int num2 = num1++; increment num1 first and then assign the value to num2 or does it do the assignment first?
The assignment operator has very low precedence, only one higher than the lowest operator, the comma operator. The post- and prefix operators (in that order) are at the top of the precedence list.
 
  • #166
Mark44 said:
The assignment operator has very low precedence, only one higher than the lowest operator, the comma operator. The post- and prefix operators (in that order) are at the top of the precedence list.
I don't have a C or C++ compiler to test it, but I think that is wrong. I can only test this in Perl, which I think follows the C precedence.

[CODE lang="perl" title="Using this Perl code"]$n1=1;
$n2=1;
$x1= ++$n1;
$x2= $n2++;
print "x1=$x1 x2=$x2\n";[/CODE]
[CODE title="I get this printed:"]x1=2 x2=1[/CODE]
The issue is not how high the precidence of '=' is, but rather what the precidence on the right side is as far as which value of the variable is used on the right side (before or after the increment).
 
  • #167
FactChecker said:
I don't have a C or C++ compiler to test it, but I think that is wrong.
Yes, you are right -- it has nothing to do with precedence. That was a momentary brain lapse on my part. This statement
num2 = num1++;​
sets num2 with the value of num1 before the increment happens. After the sequence point (the semicolon in this case), num1 is incremented. Assuming the values in your Perl code, num2 is still 1, and num1 is 2.
 
  • Like
Likes FactChecker
  • #168
Mark44 said:
Yes, you are right -- it has nothing to do with precedence. That was a momentary brain lapse on my part. This statement
num2 = num1++;​
sets num2 with the value of num1 before the increment happens. After the sequence point (the semicolon in this case), num1 is incremented. Assuming the values in your Perl code, num2 is still 1, and num1 is 2.
In any case, this proves the point that @pbuk made. -- This is treacherous, hard to keep straight, and is safest to use only by itself.
 
  • Like
Likes pbuk
  • #169
I have a simple question to verify in this program:
C++:
//Bitwise Operator to perform NOT, AND, OR, XOR on individual bits in an integer
#include <iostream>
#include <bitset>
using namespace std;

int main()
{
    unsigned short inputNum = 0;
    cout << "Enter a number (0 - 255): "; cin >> inputNum;
    bitset<8> inputBits(inputNum);
    cout << inputNum << " in binary is " << inputBits << endl;

    return 0;

}
The unsigned short inputNum is 2bytes or 16 bits. But the bitset<8> is set to 8 bits only. Is the bitset<8> just specify using the L.S.8bits only? I actually change to read bitset<16> and read the M.S.Byte to be all 0.

Does this mean you can use long int and only specify to look at the number of bits by using bitset<>?

Thanks
 
  • #170
Another really stupid question, I don't have the bitwise NOT symbol on my keyboard. That is the horizontal wiggle symbol. Now what?
 
  • #171
The tilde ( ~ ) should be just to the left of the exclamation point (if it's really not there, you can use Alt 126), but C++ commonly uses ! to mean NOT. The tilde can be used for bitwise NOT; however, the tilde is also used for a class destructor.
 
Last edited:
  • Like
Likes yungman
  • #172
sysprog said:
The tilde ( ~ ) should be just to the left of the exclamation point (if it's really not there, you can use Alt 126), but C++ commonly uses ! to mean NOT. The tilde can be used for bitwise NOT; however, the tilde is also used for a class destructor.
I not only old, I am blind too. You don't know how many times I scan through the keyboard! There's nothing good about getting old other than better than the alternative.
 
  • #173
I just ran into problem running this program and I cannot figure out why it gave me an error. Also, it only display the first line that divide the number by 2 and won't display the other 3.
C++:
#include <iostream>
using namespace std;

int main()
{
    int inputNum = 0;
    cout << "Enter a number 0 to 255 = "; cin >> inputNum;// read in a number between 0 to 255.
    int halfNum = inputNum >> 1;
    int quarterNum = inputNum >> 2;
    int doubleNum = inputNum << 1;
    int quadrupleNum = inputNum << 2;

    cout << " inputNum divided by 2 = " << halfNum << endl;
    cout << " inputNum divided by 4 = " << quarterNum << endl;
    cout << " inputNum X 2 = " << doubleNum << endl;
    cout << " inputNum X 4 = " << quadrupleNum << endl;

    return 0;
}
I removed all my own codes, this is EXACTLY the one in the book, still something wrong. The error message is on line 1. This is the error message from VS:

Listing 5.8 error message.jpg


I don't see anything wrong. Please help

Thanks
 
  • #174
yungman said:
I have a simple question to verify in this program:
C++:
//Bitwise Operator to perform NOT, AND, OR, XOR on individual bits in an integer
#include <iostream>
#include <bitset>
using namespace std;

int main()
{
    unsigned short inputNum = 0;
    cout << "Enter a number (0 - 255): "; cin >> inputNum;
    bitset<8> inputBits(inputNum);
    cout << inputNum << " in binary is " << inputBits << endl;

    return 0;

}
The unsigned short inputNum is 2bytes or 16 bits. But the bitset<8> is set to 8 bits only. Is the bitset<8> just specify using the L.S.8bits only?
Yes. Your bitset instance can hold only 8 bits, so if you use a type to initialize this instance, only the low 8 bits will be used. If you change the declaration of inputNum to unsigned int, the result will be the same; that is, only the least significant 8 bits are used.
yungman said:
I actually change to read bitset<16> and read the M.S.Byte to be all 0.
If your bitset template instance is 16 bits, and inputNum is still unsigned short, then all 16 bits will be used in the initailization.
yungman said:
Does this mean you can use long int and only specify to look at the number of bits by using bitset<>?
Yes. Whichever bitset template type you use, the initialization will use the lower bits, assuming that your bitset template is for fewer bits than the type you use to initialize the template object.
 
  • Like
Likes yungman
  • #175
This is a link error, so this version of your code can not be run, but your description sounds like it started to run something old that was already linked.
 
  • #176
FactChecker said:
This is a link error, so this version of your code can not be run, but your description sounds like it started to run something old that was already linked.
Something is funny about VS, I ran into a .cpp that was running well and all of a sudden it gave me funny result when I reopen the file. Somehow it's ok again.

The last two days, it kept saying a new version of VS is available. I did not click yes because it's a big file and look like it's going to take a long time to upgrade. I thought while the going is good, I don't want to rock the boat. But now that I want to upgrade, the window doesn't show up anymore. Now I have no choice, it's running right now.Update, it doesn't help, still the same thing. Now I definitely need you guys to help. I really don't see anything wrong with what I have. This is a very simple program, I just don't see what I did wrong even ignoring the error message, it only display the first line as if it skip the next 3 cout display.

Thanks
 
  • #177
Another question
I am reading the section of Compound Assignment Operators like:

num1 += num2 that gives the result store in num1 where num1 = num1 + num2
num1 *= num2 that gives the result store in num1 where num1 = num1 * num2
num1 /= num2 that gives the result store in num1 where num1 = num1 / num2

This is like Prefix and Profix that doing two steps with one statement.

Does anyone actually find this useful? To me it's confusing and hard to remember and easy to make mistakes. I would prefer to write two statements to do the same thing like

num3 = num1 * num2; num1 = num3; instead of num1 *= num2.

The prefix and postfix is even more confusing. It's so easy to use

num2 = num1; num1 = num1+1; instead of num2 = num1++ for postfix.
BTW, I want to sincerely thank you guys for the help and support in my learning of C++, I can't do it without coming here to ask. I am finishing chapter 5 today, that's 110 pages of the book with your help.
 
Last edited:
  • #178
Again, another problem with another exercise. This is about Compound Assignment Operators
C++:
// Using Compound Assignment Operators to perform ADD, SUBTRACT, DIVIDE, MODULUS, SHIDIFT and bitwise OR, AND and COR
#include <iostream>
using namespace std;

int main()
{
    int value = 0; //declare integer variable with initial value of 0.
    cout << "Enter an integer number = "; cin >> value;// give int value a number.
    //Put in 440 as the book suggested.

    cout << " value = value + 8 = " << (value += 8) << endl;// 440 + 8 = 448

    cout << " value = value - 2 = " << (value -= 2) << endl;// 440 - 2 = 446

    cout << " value = value/4 = " << (value /= 4) << endl;// 440 / 4 =110 but result say it's 111 instead.

    cout << " value = value X 4 = " << (value *= 4) << endl; //440 X 4 = 1760 but result is 444.

    return 0;}

I ran the code, the first two answer is correct, the DIVISION and MULTIPLICATION is just WRONG. I wrote the answer in the comment after the line of code. The book even gave the same answer as the program after running which is WRONG.

440/4= 110, and 440 X 4 = 1760, not 111 and 444 respectively. Something is really wrong here. I triple checked my codes, triple checked the compound operators also. Can you take a look?
 
  • #179
yungman said:
Again, another problem with another exercise. This is about Compound Assignment Operators
C++:
// Using Compound Assignment Operators to perform ADD, SUBTRACT, DIVIDE, MODULUS, SHIDIFT and bitwise OR, AND and COR
#include <iostream>
using namespace std;

int main()
{
    int value = 0; //declare integer variable with initial value of 0.
    cout << "Enter an integer number = "; cin >> value;// give int value a number.
    //Put in 440 as the book suggested.

    cout << " value = value + 8 = " << (value += 8) << endl;// 440 + 8 = 448

    cout << " value = value - 2 = " << (value -= 2) << endl;// 440 - 2 = 446

    cout << " value = value/4 = " << (value /= 4) << endl;// 440 / 4 =110 but result say it's 111 instead.

    cout << " value = value X 4 = " << (value *= 4) << endl; //440 X 4 = 1760 but result is 444.

    return 0;}

I ran the code, the first two answer is correct, the DIVISION and MULTIPLICATION is just WRONG. I wrote the answer in the comment after the line of code. The book even gave the same answer as the program after running which is WRONG.

440/4= 110, and 440 X 4 = 1760, not 111 and 444 respectively. Something is really wrong here. I triple checked my codes, triple checked the compound operators also. Can you take a look?
All of your output statements use compound assignment operators, meaning that each one modifies the value variable.
First statement changes value to 448, which you recognize in a comment.
Second statement changes 448 to 446, also recognized, but your comment indicate that you think value is still 440.
Third statement changes 446 to 446/4, which is 111.
Fourth statement changes 111 to 444.
 
  • Like
Likes sysprog, yungman and FactChecker
  • #180
yungman said:
Does anyone actually find this useful? To me it's confusing and hard to remember and easy to make mistakes. I would prefer to write two statements to do the same thing like
num3 = num1 * num2; num1 = num3; instead of num1 *= num2.
A better way with only one statement and no third variable would be to write num1 = num1 * num2;

Compound assignment operators provide a useful shorthand, one that is used extensively in lots of code.
 
  • Like
Likes FactChecker

Similar threads

  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 133 ·
5
Replies
133
Views
10K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
16
Views
3K
  • · Replies 54 ·
2
Replies
54
Views
5K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 58 ·
2
Replies
58
Views
4K