Trying to decide which programming language I want to learn

In summary: C++ is considered a lower-level language, meaning that it is closer to the hardware and gives you more control over how your program runs. However, C# is a high-level language, which means it is easier to learn and use and has a lot of built-in libraries that make coding faster and more efficient. Both languages have their advantages and disadvantages, and it ultimately depends on what type of project you are working on. For gaming, C++ is often preferred because of its speed and control, but C# has become increasingly popular in game development as well. It's worth trying both and seeing which one you prefer working with.
  • #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
 
Technology news on Phys.org
  • #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

  • C++ notes.docx
    24.8 KB · Views: 91
  • #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
 
  • #201
Out of curiosity, what's the title of the book you're working out of, and who is the author?I hope it's not Herb Schildt.
yungman said:
Also I have questions:

1) Line 11 is: char copyInput[20] = { '/0' }. Does this mean it fills all 20 elements with {'/0'} null character?
The null character (ASCII code 0) should be '\0', not '/0'. All of the special control characters are represented with a backslash; e.g, '\n' (new line), '\t' (tab), and so on.

Assuming that you use '\0' as the initializer, all the other 19 array elements also get this value.
yungman said:
2) Line 14 is: strcpy(copyInput, userInput.c_str()). Why having .c_str()? what does this mean?
First off, since you're using VS, you shouldn't use strcpy(), since that has been deprecated for the past two or three versions of VS. Instead, use strcpy_s(), the secure version of this function.
I changed your code to the following:
C++:
strcpy_s(copyInput, 20, userInput.c_str());
The first parameter is the source of the copy, the second is the max. number of characters to copy, and the third is the destination of the copy.

Edit: My code did what it was supposed to, but my explanation was wrong. The first parameter is the destination string, second is the max. number of characters, and the third is the source of the copy.

c_str() is a function that converts a C++-style string to a C-style null-terminated array of type char. One of the very confusing things about C++ and its standard template library is that both types of string are still present -- the C-type array of char, and the C++ string class. The program you showed mixes both types, plus it uses a C function (strcpy) that Microsoft doesn't support any longer.

Here's my version of the program, one that builds with no warnings or errors.
C++:
#include <iostream>
#include <string>
#include <string.h>
using std::cout; using std::cin; using std::endl;
using std::string;

int main()
{
    cout << " Enter a line of text:  ";
    string userInput;
    getline(cin, userInput);
    char copyInput[20] = { '\0' };
    if (userInput.length() < 20)// check bound.
    {
        strcpy_s(copyInput, 20, userInput.c_str());
        cout << " CopyInput contains:   " << copyInput << endl;
    }
    else
        cout << " Bound exceed, won't copy!" << endl;
}
Notice that I don't have using namespace std;
The strcpy_s() function is documented here: https://docs.microsoft.com/en-us/cp...rence/strcpy-s-wcscpy-s-mbscpy-s?view=vs-2019
 
Last edited:
  • Like
Likes yungman
  • #202
Great, that's exactly what I need, a book like this leading the blind(me). I can't wait for the other books to arrive. One of them is late. Seldom happens to Amazon.
 
  • #203
yungman said:
Great, that's exactly what I need, a book like this leading the blind(me).
Who's the author?
 
  • #204
Mark44 said:
Who's the author?
Google search on a line posted here from one of the exercises shows that the text is Sam's Teach Yourself C++ in One Hour a Day by Jesse Liberty and Siddhartha Rao
 
  • #205
Mark44 said:
Who's the author?
https://www.amazon.com/gp/product/0789757745/?tag=pfamazon01-20

I am no stranger of self study. I studied all the electronics on my own and worked as EE and manager of EE for almost 30 years before I retired. Electronics is not even my major in college. Even after I retired, I studied calculus to Partial Differential Equations on my own and RF electromagnetics. I never ran across so many issues like with this book. In all fairness, I always have 3 books for one subject. So far, this C++ is not hard, just give me the information and I am all good.

Even a lot of calculus books have all the essential formulas condensed and put it on the first page and last page of the book for people to have quick reference. Isn't that hard to put something like my notes at the back of the book so people can just have quick reference. that's all it takes and I am sure I won't have half the questions here. I never see a book throwing out so much stuffs and have absolutely no explanation in the rest of the book.

Thank you and the others to have to patience to help me. I hope when I have the other books, I can find information easier.
 
Last edited:
  • Like
Likes sysprog
  • #206
Among his many accomplishments is that @Mark44 is a senior-level subject matter expert in the C++ language. I'm rather confident that if I buy both the Sam's C++ in 21 days book and the 1 hour a day book, I can get to his level in 21 hours ##-## NOT. :rolleyes: :confused: :wink:
 
  • Love
Likes Mark44
  • #207
sysprog said:
Among his many accomplishments is that @Mark44 is a senior-level subject matter expert in the C++ language. I'm rather confident that if I buy both the Sam's C++ in 21 days book and the 1 hour a day book, I can get to his level in 21 hours ##-## NOT. :rolleyes: :confused: :wink:
I think you guys have been spending over an hour a day helping me so far! Maybe some that is already expert in programing and don't know C++ might be able to learn in 21 days with one hour a day( not with this book). Not what the book said you don't need experience like me. I don't think I am a slow learner, It's been like 11 days so far, I put in minimum 3 to 4 hours a day, last Monday I put in like 10 hours just to finish chapter 5. It's all these problem with the book. I hope the second book arrive today as promised after missing the Monday delivery date.
 
  • #208
yungman said:
I think you guys have been spending over an hour a day helping me so far! Maybe some that is already expert in programing and don't know C++ might be able to learn in 21 days with one hour a day( not with this book). Not what the book said you don't need experience like me. I don't think I am a slow learner, It's been like 11 days so far, I put in minimum 3 to 4 hours a day, last Monday I put in like 10 hours just to finish chapter 5. It's all these problem with the book. I hope the second book arrive today as promised after missing the Monday delivery date.
Really, @yungman, it's just a sales-pitch book title.

When the Saturday Night Fever movie came out, millions of guys went to their local Arthur Murray's Dance Studio franchise and said they'd pay anything to learn to dance exactly like John Travolta did in that movie, and they had to be told that they would have needed to start decades earlier, and have had enormous talent, and that John Travolta wishes he could dance like Fred Astaire, and that everyone can learn to dance in his own way.

I think that you can learn to be proficient in C++.
 
  • Like
Likes yungman
  • #209
Ye
sysprog said:
Really, @yungman, it's just a sales-pitch book title.

When the Saturday Night Fever movie came out, millions of guys went to their local Arthur Murray's Dance Studio franchise and said they'd pay anything to learn to dance exactly like John Travolta did in that movie, and they had to be told that they would have needed to start decades earlier, and have had enormous talent, and that John Travolta wishes he could dance like Fred Astaire, and that everyone can learn to dance in his own way.

I think that you can learn to be proficient in C++.
Even for Travolta, you don't know how many times they tape each move to pick the best one, how many short segments linked together to make it like a sequence of dance. I was a photographer, believe me, what you see is an illusion. There are so many camera angle and tricks you can do to make people look better than they really are.

Like if you watch the UFC fights vs kung-fu movie, the movie make the star looks so good and amazing speed and movement where the UFC fight scene is not that amazing. Believe me, you put those movie star in the Octagon, they won't last 10 seconds!
 
  • #210
I just highlighted "definition of strcpy_s" from your post, right-clicked, selected "Search Google for "definition of strcpy_s"" from the context menu, and got this:
1595464502682.png

Remarks. The strcpy_s function copies the contents in the address of src, including the terminating null character, to the location that's specified by dest. The destination string must be large enough to hold the source string and its terminating null character.​

The link leads to a full explanation. It's true that, as you put it , "It is NOT easy to google and learn C++", but being able to do searches on things is a great aid in learning things, and of course, not everything that is worth learning is easy to learn, by any means.
 
Back
Top