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.
  • #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
  • #181
I have no clue what you want your statements to print. Before the arithmetic operation, after the operation, a success flag from the operation, something else? If you want to learn C++ KEEP IT SIMPLE AND AS CLEAR AS POSSIBLE. People might lose interest in helping you if your goal is to experiment with every strange, ambiguous thing you can think of.
 
  • #182
FactChecker said:
I have no clue what you want your statements to print. Before the arithmetic operation, after the operation, a success flag from the operation, something else? If you want to learn C++ KEEP IT SIMPLE AND AS CLEAR AS POSSIBLE. People might lose interest in helping you if your goal is to experiment with every strange, ambiguous thing you can think of.
No, this is not what I made up, both strange behavior are straight from the book.

The problem in post 179 gave the answer exactly like in the book which is wrong:

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.

You can see 440 /= 4 should be 110. But both the program and the book's answer is 111.

440 *= 4 should be 1760, but but the program and the book answer is 444.

I did not experiment on this.

Unless I miss the whole thing about this, the answer is wrong.
 
  • #183
yungman said:
The problem in post 179 gave the answer exactly like in the book which is wrong:
yungman said:
You can see 440 /= 4 should be 110. But both the program and the book's answer is 111.
No, 110 is not correct. I explained all of this in post #179. Please take the time to read what others and I have written.

yungman said:
Unless I miss the whole thing about this, the answer is wrong.
You're missing the point that these compound assignment operators modify the variables they're applied to. You seem to be thinking that value is 440 all the way through -- you are mistaken. The compound assignment expressions in each output statement change the variable (value) each time.
 
  • Like
Likes FactChecker and sysprog
  • #184
Mark44 said:
No, 110 is not correct. I explained all of this in post #179. Please take the time to read what others and I have written.

You're missing the point that these compound assignment operators modify the variables they're applied to. You seem to be thinking that value is 440 all the way through -- you are mistaken. The compound assignment expressions in each output statement change the variable (value) each time.
Sorry, I did not see your reply. Now I know, thanks.
 
  • Like
Likes sysprog
  • #185
finally finish chapter 5, all the exercise and all. 😄😄
 
  • #186
I just finished the notes for chapter 4 and chapter 5. It's been a long day.
 

Attachments

  • #187
Comments on your notes.
Ch 1
*std is a namespace. It is inside isostream.
The header is iostream, not isostream. The std namespace in not "inside" iostream. The identifiers that make up the std namespace are spread across many headers.
Ch 2
*using namespace std to avoid repeating std:: on every line.
Many C++ textbooks do this, but it is considered bad practice, that can cause problems.

*Program can declare a function of int abcd() to be used in main(). But it cannot be inside main().
Any function can be called from any other function, not just main().

Ch 3
-enum Directions { North, East, South, West};// Directions() that has only 4 valid values.
Directions is an enumeration, not a function. Directions() is a syntax error.
The enumeration values can be used by Directions.North, Directions.East, and so on, using the dot operator. The dot operator is also used to access the members of a struct or class, which you haven't studied yet.
Ch 4
Eg. Int myNumbers [5] { 123, 234, 345, 456, 567} is 5 elements with values.
Don't write Int -- it should be int.
Also, if you provide a list of initializers, as you did, you don't have to include the array size.
C++:
int myNumbers[] = {123, 234, 345, 456, 567};
-std::cout << “Hi” is the same as char sayHello[] = { ‘H’,’I’,’\0’}.
No. The first is an output statement that displays the string "Hi". The second is an array of type char with the characters 'H' and 'I' and the null character. The two statements are not the same because the first displays a string and the second merely initializes an array. Also the two arrays are different -- the first has the character 'i' and the second has the character 'I'.
Ch 5
Cout< “Number of days in a year: “ << daysInYear << endl;
It's cout, not Cout (probably an autocapitalization in Word).
The stream insertion operator is <<, not <.
*L-values and R-values: daysInYear = 365. L-value is dayInYear’s address location in memory.
No. dayInYear is an l-value. An l-value is an addressable memory location. It's called an l-value because it can appear on the left side of an assignment statement.
Not all variables are l-values. For example, if you have this definition
C++:
char name[] = "Fred";
you can't do this: name = "Mary";
The variable name is not an l-value -- it can't appear on the left side of an assignment statement.
 
  • Like
Likes FactChecker, yungman and sysprog
  • #188
Thanks Mark44. This is very helpful.
 
  • #189
Thanks Mark44. This is very helpful.
yungman said:
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:

View attachment 266629

I don't see anything wrong. Please help

Thanks
Update:
This is really funny. I had problem with this that did not make sense two days ago. I loaded and ran this program today. Everything ran and gave me the correct display! I did not change anything, I just gave up two days ago.

This is NOT the first time I run into strange problem with VS. I had issue of another that I ran ( Ctrl+F5) and got the same result in debugger no matter I change the code. I exit VS and re-ran the program, it's the same. I even restarted my laptop, still the same. But somehow, it ran fine later on. VS doesn't seems to be that reliable.

It almost seems the debugger got stuck with the last run and keep displaying the same without even running the debugger. Seems like when this happen, the cmd window comes up right away as soon as I hit Ctrl+F5. In normal running, it usually takes a few seconds running through, display something before the control panel screen appears. It's almost it just keep displaying the stuck page.
 
Last edited:
  • #190
I am kind of taking it easy today, I am just think about what I want to do learning languages. One of the goal is to be better with computer. I have been asking a lot of question here how to deal with different things I encountered, like lost of password and other things. I do want to understand the Windows more so I can deal with it better. I know going on google, come here are good ways, but I am thinking, what language is the most useful in dealing with Windows, that if I learn it, I would have more experience with Windows.

I know if I keep coming here and google, I'd resolve most of the problems. But since I decided to learn some languages, I might as well look into something closer to what I want to learn of Windows. I don't like the idea of keep blindly asking for solution from you guys, I might want to learn how to do something with Windows to get insight of Windows.

Any particular language is closer to what I want? Is C++ the language?

BTW, I actually scan through chapter 6 and 7 that are on Condition statements and functions resp. They are long, but don't seems to be as hard as the first 5 chapters. It's the names, the new terms that I have a very hard time, the classes, headers...all the names. But now that it is getting into the details, it's like I scan through the example, it's not that scary. I hope I am right.
 
  • #191
yungman said:
I might want to learn how to do something with Windows to get insight of Windows.

One suggestion might be to start exploring what you can do with Windows PowerShell. Many things that used to require building a program in what is normally thought of as a programming language can now be done with PowerShell scripts. (Windows has always had batch files inherited from DOS and cmd.exe scripts, but those are very limited in capability compared to what PowerShell scripts can do.) Exploring what you can do with PowerShell can probably tell you quite a lot about how Windows works.

As far as other programming languages, the core of Windows programming is not really a particular language but the .NET runtime and API. I'm not sure what the current Microsoft-preferred language is for .NET programs; it started out being C# (when .NET took over from the old Win32 API, which had its own rather checkered history of preferred languages, including C, C++, and Visual Basic), but I'm not sure if that's still the case.
 
  • #192
yungman said:
This is NOT the first time I run into strange problem with VS. I had issue of another that I ran ( Ctrl+F5) and got the same result in debugger no matter I change the code. I exit VS and re-ran the program, it's the same. I even restarted my laptop, still the same. But somehow, it ran fine later on. VS doesn't seems to be that reliable.
I've been using VS for 22 years, and can't remember ever seeing the problem you describe. If you change the code, you need to rebuild your program. Just hitting CTRL+F5 might be just running the code you had before you made the change.
yungman said:
It almost seems the debugger got stuck with the last run and keep displaying the same without even running the debugger. Seems like when this happen, the control panel comes up right away as soon as I hit Ctrl+F5. In normal running, it usually takes a few seconds running through, display something before the control panel screen appears. It's almost it just keep displaying the stuck page.
I don't know what you're describing here, especially about the control panel coming up and the debugger getting stuck. The Control Panel is a feature of Windows, not VS. It allows you to make changes to the computer.

Again, I think what you might be seeing is that you aren't rebuilding (i.e., compiling and linking) your edited program to produce a new executable.
 
  • #193
Mark44 said:
I've been using VS for 22 years, and can't remember ever seeing the problem you describe. If you change the code, you need to rebuild your program. Just hitting CTRL+F5 might be just running the code you had before you made the change.
I don't know what you're describing here, especially about the control panel coming up and the debugger getting stuck. The Control Panel is a feature of Windows, not VS. It allows you to make changes to the computer.

Again, I think what you might be seeing is that you aren't rebuilding (i.e., compiling and linking) your edited program to produce a new executable.
I meant cmd window. I change that already.
Mark44 said:
I've been using VS for 22 years, and can't remember ever seeing the problem you describe. If you change the code, you need to rebuild your program. Just hitting CTRL+F5 might be just running the code you had before you made the change.
I don't know what you're describing here, especially about the control panel coming up and the debugger getting stuck. The Control Panel is a feature of Windows, not VS. It allows you to make changes to the computer.

Again, I think what you might be seeing is that you aren't rebuilding (i.e., compiling and linking) your edited program to produce a new executable.
That sounds like my problem, I only hit Ctrl+F5.

What do I have to do to rebuild and link the program after I modify the code? I thought Ctrl+F5 is doing that already.
 
  • #194
yungman said:
What do I have to do to rebuild and link the program after I modify the code? I thought Ctrl+F5 is doing that already.
In the menu bar near the top of the VS window, click the Build menu item. The drop-down menu that opens has Build Solution (F7), Rebuild Solution (Ctrl+Alt+F7), and Clean Solution, and other commands. When I make a change to my source code, I usually hit Rebuild Solution.

Clean Solution gets rid of all of the object files and the executable, letting you start the build process from scratch.
 
  • Like
Likes yungman
  • #195
yungman said:
num1 += num2 that gives the result store in num1 where num1 = num1 + num2

Does anyone actually find this useful?
Yes I use this almost every day for incrementing pointers in loops e.g. x_ptr += 6 to move to the next pixel in an 8 bit colour display frame.

yungman said:
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

Does anyone actually find this useful?
These not so much, but &= is useful for applying bitmasks. Can't remember using |= (which is the one that caught you out earlier in the thread) or ^= although I probably have sometime: word ^= 0xFFFF will usefully flip every bit of a 16 bit unsigned integer.
yungman said:
The prefix and postfix is even more confusing. It's so easy to use
It's OK if you stick to i++ and i-- and never assign the result, although some people prefer i += 1 and i -= 1 which a modern compiler should optimise to the same thing.

I find it helpful to use a linter to ensure consistency in coding style choices like these an others, there is one built into VS but I am not sure how configurable it is (I use Visual Studio Code which despite the similar name is a totally different product).
 
  • Like
Likes yungman
  • #196
I have a question about bitset<>
What is bitset<>, a class, a function or an operator?

C++:
//Shifting  num1 << num2 where num1 = num1 << num2
#include <iostream>
#include <bitset>
using namespace std;

int main()
{
    int num1 = 0, num2 = 0;
    cout << " Enter num1 = "; cin >> num1;
    cout << endl;
    cout << " Enter num2 = "; cin >> num2;
    cout << endl;

    int num3 = num1 & num2;
    cout << "num3 = " << num3 << " = " << bitset<4>(num3) << endl;
    cout << endl;   

    cout << "num1 AND num2 = " << (num1 & num2) << bitset<4>(num1 & num2) << endl;
    cout << endl;

    return 0;}

This is just a short program I play with bitset<>. Line 15 obviously give the correct answer. on line 18, I tried

cout << bitset<4>(num1 & num2) << endl;

It gave me the 5th bit as "2". It cannot have 2, only 1 and 0. The last 4 bits is correct. Is it wrong to write like this?

Thanks
 
  • #197
yungman said:
I have a question about bitset<>
What is bitset<>, a class, a function or an operator?
The <bitset> header "defines the class template bitset and two supporting template functions for representing and manipulating fixed-size sequences of bits. " -- straight from the documentation.

A template class is a class that you can use to define a number of specific class instances, based on the information you supply in the angle brackets when you create an instance. For bitset, the information is the number of bits in your bitset class instance. Your code creates two bitset instances on the fly, each containing four bits.

When you create the two bitset instances, as in bitset<4>(num3), your code is calling the bitset constructor, and initializing the instance with the value in num3.

Other template classes can be used to create class instances based on the underlying type. The vector template class can be used to create a vector of type char (vector<char>), or of type int (vector<int>), or whatever other type.

You're getting pretty heavily into C++ arcana here, which I'm not sure is a good idea, especially since the book hasn't presented decision structures (if statements) or functions yet.
yungman said:
C++:
//Shifting  num1 << num2 where num1 = num1 << num2
#include <iostream>
#include <bitset>
using namespace std;

int main()
{
    int num1 = 0, num2 = 0;
    cout << " Enter num1 = "; cin >> num1;
    cout << endl;
    cout << " Enter num2 = "; cin >> num2;
    cout << endl;

    int num3 = num1 & num2;
    cout << "num3 = " << num3 << " = " << bitset<4>(num3) << endl;
    cout << endl;

    cout << "num1 AND num2 = " << (num1 & num2) << bitset<4>(num1 & num2) << endl;
    cout << endl;
    return 0;
}

This is just a short program I play with bitset<>. Line 15 obviously give the correct answer. on line 18, I tried

cout << bitset<4>(num1 & num2) << endl;

It gave me the 5th bit as "2". It cannot have 2, only 1 and 0. The last 4 bits is correct. Is it wrong to write like this?
There is no 5th bit. The 2 that you see is the value of the expression num1 & num2. The other four digits are the value of bitset<4>(num1 & num2).
Things would have been clearer if you had added a space in your output line, like this:
C++:
cout << "num1 AND num2 = " << (num1 & num2) << ' ' << bitset<4>(num1 & num2) << endl;
 
  • Like
Likes pbuk, sysprog and yungman
  • #198
Mark44 said:
The <bitset> header "defines the class template bitset and two supporting template functions for representing and manipulating fixed-size sequences of bits. " -- straight from the documentation.

A template class is a class that you can use to define a number of specific class instances, based on the information you supply in the angle brackets when you create an instance. For bitset, the information is the number of bits in your bitset class instance. Your code creates two bitset instances on the fly, each containing four bits.

When you create the two bitset instances, as in bitset<4>(num3), your code is calling the bitset constructor, and initializing the instance with the value in num3.

Other template classes can be used to create class instances based on the underlying type. The vector template class can be used to create a vector of type char (vector<char>), or of type int (vector<int>), or whatever other type.

You're getting pretty heavily into C++ arcana here, which I'm not sure is a good idea, especially since the book hasn't presented decision structures (if statements) or functions yet.
There is no 5th bit. The 2 that you see is the value of the expression num1 & num2. The other four digits are the value of bitset<4>(num1 & num2).
Things would have been clearer if you had added a space in your output line, like this:
C++:
cout << "num1 AND num2 = " << (num1 & num2) << ' ' << bitset<4>(num1 & num2) << endl;
My God, I forgot I output the number 2 also which is exactly what I want! Believe me, it is very very confusing to me. It's been only less than 2 weeks learning C++, everything is still foreign to me at this point, those names, it's just a blur to me still. Never even know to rebuild solution, just kept hitting Ctrl-F5. No wonder I kept getting the same result.

I have no idea what is deep and what's entry level. Today, I decided not to learn new things and review the programs I created, starting to have more questions, then experiment with them and go from there. I am still waiting for the other books to arrive, I just don't think the book I have is good. Keep throwing things out without explaining, I have to guess all the time. Well, can't exactly complaining I don't have enough brain exercise lately.

Thanks for your patience and help out.
 
  • #199
Mark44 said:
You're getting pretty heavily into C++ arcana here, which I'm not sure is a good idea, especially since the book hasn't presented decision structures (if statements) or functions yet.
This.

I had never even heard of std::bitset until this thread, for bit operatons I would use the techniques outlined in the code below. Note that in a timing-critical environment like a microcontroller you need to be careful about using anything from STL (the Standard Template Library).

C:
#include <iostream>
using namespace std;
 
#define BIT_2 0b00000100
#define BIT_4 0b00010000
#define EOL "\n"

int main() {
    // unsigned char is an 8 bit type implemented in all versions of C.
    unsigned char input_byte = 0;
    // uint8_t is supported in most versions of C++ and is more explicit.
    uint8_t output_byte = 0;
    
    // Set bits 2 and 4.
    output_byte = input_byte | (BIT_2 | BIT_4);
    // Need to typecast to int otherwise cout will treat the byte as an ASCII value.
    cout << (int) output_byte << EOL;
    
    // Reset bits 2 and 4.
    output_byte = input_byte & ~(BIT_2 | BIT_4);
    cout << (int) output_byte << EOL;
    
    // Toggle bits 2 and 4.
    output_byte = input_byte ^ (BIT_2 | BIT_4);
    cout << (int) output_byte << EOL;
    
    // Test bit 4.
    cout << (output_byte & BIT_4 ? "is set" : "is not set") << EOL;
}
 
  • #200
Hi, I run into build problem. This is EXACTLY to program given in the book. It won't even build. There are other question that the book doesn't even talk about also.
C++:
#include <iostream>
#include <string>
#include <string.h>
using namespace std;

int main()
{
    cout << " Enter a line of text:  "; 
    string userInput;
    getline(cin, userInput);
    char copyInput[20] = { '/0' };
    if (userInput.length() < 20)// check bound.
    {
        strcpy(copyInput, userInput.c_str());
        cout << " CopyInput contains:   " << copyInput << endl;
    }
    else
            cout << " Bound exceed, won't copy!" << endl;

    return 0;
}

The error message is:
Compile error Listing 7.2.jpg


Also I have questions:

1) Line 11 is: char copyInput[20] = { '/0' }. Does this mean it fills all 20 elements with {'/0'} null character?

2) Line 14 is: strcpy(copyInput, userInput.c_str()). Why having .c_str()? what does this mean?

As usual, this book throw out stuffs with no explanations. I did search through the index at the back of the book. No information at all. I can't wait for the new books to arrive so I can look up all these.

Thanks
 
Back
Top