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.
  • #241
yungman said:
I have to fix the stuffs in your response as it's hard to read.
I already fixed my reply and the text you quoted.
 
  • Like
Likes yungman
Technology news on Phys.org
  • #242
Mark44 said:
The fact that they are declared differently should be a clue that they aren't the same.
First off, myThing[20] is not a string -- it's the element one past the end of the string.
Second, you need to distinguish between (1) an array of char (a C-string), which is a contiguous block of memory containing characters, and (2) a C++ Standard Template Library string instance. string is a keyword in C++ but not in C.
Using your examples, otherThing is the name of the array. There are no methods provided for any type of C-style array. The only things you can do are: set an element of the array (otherThing[3] = 'b';) or get an element of the array (char val = otherThing[8];).
To verify, unless it is specifically defined as std::string myThing[20] that defines it is a 20 elements string. char otherThing[20] is just defining a 20 elements char array.

Mark44 said:
In contrast, myThing is an instance of the string class. As such, there are lots of member methods and operators such as size(), clear(), capacity(), append(), push_back(), and many more. A C-style array has none of these. See this documentation page: http://www.cplusplus.com/reference/string/string/
This is a C-style array of type char. It has nothing to do with a C++ string object. This array could also be defined in another way:
C++:
char sayHello2 = "Hello";
Both definitions store the letters in the first 5 bytes of memory, followed by an ASCII null character. Both arrays have a length of 5, the number of characters up to the null.
I can still use sizeof(array) to find the length of the array. This is useful for dynamic array where the length change.

Mark44 said:
No, you can't have a string object with a base type of int, float, and so on. A string object has a base type of char. However, it's possible to form other types of strings with different base types, using the basic_string template class.
There are four specializations:
basic_string<char> str_ch; // same as string
basic_string<u16string> str_16bit; // a string of 16-bit characters
basic_string<u32string> str_32bit; // a string of 32-bit characters
basic_string<wstring> str_wchar; // a string of wide characters

If you want an array of floats, use the vector template class, like so:
vector<float> vec = {3.2, 2.7, 5.1};[/icode]

Thanks for the detail reply. The first book put char array[] in the string section. That doesn't help.

You should write a book on C++.
 
  • #243
yungman said:
To verify, unless it is specifically defined as std::string myThing[20] that defines it is a 20 elements string. char otherThing[20] is just defining a 20 elements char array.
Yes.
yungman said:
I can still use sizeof(array) to find the length of the array. This is useful for dynamic array where the length change.
The sizeof operator can be used to find the size, in bytes, of any type or any variable. It won't tell you how many elements are in the array. This operator is from C that was carried over to C++.

If you're doing something with Standard Template Library containers, such as the <array> or <vector> template classes, any instance of one of these classes has a number of member functions that can tell you the size (size() returns the number of elements), the capacity (capacity() returns the number of elements the vector could contain without reallocating more space, plus lots more capabilities.

Be careful with the terminology. The term array in C has no special meaning other than a block of contiguous memory locations that can contain a sequence of values all of the same type. Likewise the term string, which can refer to a variable of type array of char, or a string literal. In C++ however, array and vector are template classes, meaning that you can create instances of a specified type with either of them provided that you include their corresponding header.
yungman said:
You should write a book on C++.
Too much work, plus there are lots of good books out there (and a fair number of mediocre books).
 
  • Like
Likes yungman
  • #244
I have issue building the solution. VS gives me error. This is from the C++ for Dummies. What's wrong with this?

C++:
#include <iostream>

using namespace std;void displayString(char szString)
{
    for (int index = 0; szString[index] != '\0'; index++)
    {
        cout << szString[index];
    }
}
int main(int nNumberofArg, char* pszArgs[])
{
    char szName2[] = "Stephen";// declare char szName[8] = "stephen"
    cout << "Output szName2: ";
    displayString(szName2);
    cout << endl;

    return 0;
}

The error messages are these:

Error message.jpg


I also have other questions

1) Why I have to declare int main()? What is int for?

2) Why in the first book, it just declare int main(). But in this book it has parameter like int main(int nNumberofArg, char* pszArgs[])?
Thanks
 
Last edited:
  • #245
yungman said:
I have issue building the solution. VS gives me error. This is from the C++ for Dummies. What's wrong with this?
It's always more helpful if you tell us which error VS is reporting.
yungman said:
C++:
#include <iostream>

using namespace std;void displayString(char szString)
{
    for (int index = 0; szString[index] != '\0'; index++)
    {
        cout << szString[index];
    }
}
int main(int nNumberofArg, char* pszArgs[])
{
    char szName2[] = "Stephen";// declare char szName[8] = "stephen"
    cout << "Output szName2: ";
    displayString(szName2);
    cout << endl;

    return 0;
}
The displayString function is defined as having a char parameter, not a char array parameter. That's the reason for the error.
Change the function header to either of these:
void displayString(char szString[])
or
void displayString(char * szString)
BTW, this book is using what is called Hungarian notation, which has somewhat fallen ouot of favor. The "sz" and "psz" prefixes (sometimes called "warts") signify string, zero-terminated, and pointer to string, zero-terminated.
Since you haven't done anything with pointers yet, I won't go into any detail about them.
yungman said:
I also have other questions

1) Why I have to declare int main()? What is int for?
I think you asked this before. The main() function is called from the operating system. The OS can use this value, particularly if you program is run from a batch file.
You can omit the "return 0;" statement if you like.
yungman said:
2) Why in the first book, it just declare int main(). But in this book it has parameter like int main(int nNumberofArg, char* pszArgs[])?

Thanks
No good reason for this particular program, since the program doesn't do anything with the command line arguments. Perhaps in subsequent lessons the book will have an example where you run the program from the command line, and pass arguments that the program does something with.
 
  • #246
yungman said:
I have issue building the solution. VS gives me error.
Please don't ever ask this without telling us what the complete error statement, including the line number, if given.
1) Why I have to declare int main()? What is int for?
Because main often returns a completion code. 0 = normal satisfactory completion. Anything else indicates a different kind of termination.
2) Why in the first book, it just declare int main(). But in this book it has parameter like int main(int nNumberofArg, char* pszArgs[])?
This is the standard way that the main program can be passed information when it is called on the command line or in scripts. Like if you want to tell main the name of an input file and an output file. nNumberofArg tells main how many arguments were on the command line. All the arguments are stored in the array pszArgs. The first one is special (if it is not null). It gives the name of the executable that is running and contains main. (See https://en.cppreference.com/w/cpp/language/main_function )
 
  • #247
FactChecker said:
Please don't ever ask this without telling us what the complete error statement, including the line number, if given.Because main often returns a completion code. 0 = normal satisfactory completion. Anything else indicates a different kind of termination.This is the standard way that the main program can be passed information when it is called on the command line or in scripts. Like if you want to tell main the name of an input file and an output file. nNumberofArg tells main how many arguments were on the command line. All the arguments are stored in the array pszArgs. The first one is special (if it is not null). It gives the name of the executable that is running and contains main. (See https://en.cppreference.com/w/cpp/language/main_function )
Sorry, I added into the other post already.
 
  • #248
yungman said:
std::string myThing[20] that tell people myThing[20] is a 20 element string.
No.

std::string myThing; gives you a single C++-style string which is initially "empty". It's a dynamic object which can contain any number of characters depending on what you do with it later. For example, if you later write myThing = "automobile";, it now contains ten characters.

std::string myThing[20]; gives you a 20-element C-style array of std::string objects. You could then write

Code:
myThing[0] = "Hello";
myThing[1] = "my";
myThing[2] = "name";
myThing[3] = "is";
myThing[4] = "yungman";

However, I consider such a beast to be a Frankenstein's monster, mixing a C++-style construct with a C-style construct. When I taught C++, I always used std::vector instead of C-style arrays:

Code:
std::vector<std::string> myThing(20);
myThing[0] = "Hello";
myThing[1] = "my";
// etc.

After I stopped teaching C++, the C++11 standard (I think that was when it was) introduced C++-style fixed length arrays: std::array<std::string, 20> myThing;.
 
  • #249
jtbell said:
No.

std::string myThing; gives you a single C++-style string which is initially "empty". It's a dynamic object which can contain any number of characters depending on what you do with it later. For example, if you later write myThing = "automobile";, it now contains ten characters.

std::string myThing[20]; gives you a 20-element C-style array of std::string objects. You could then write

Code:
myThing[0] = "Hello";
myThing[1] = "my";
myThing[2] = "name";
myThing[3] = "is";
myThing[4] = "yungman";

However, I consider such a beast to be a Frankenstein's monster, mixing a C++-style construct with a C-style construct. When I taught C++, I always used std::vector instead of C-style arrays:

Code:
std::vector<std::string> myThing(20);
myThing[0] = "Hello";
myThing[1] = "my";
// etc.

After I stopped teaching C++, the C++11 standard (I think that was when it was) introduced C++-style fixed length arrays: std::array<std::string, 20> myThing;.
I thought myThing is a 20 character string, that is each element is only ONE charater. like if myThing [] = "Hello"
myThing[0] = 'H'; myThing[1] = 'e'; myThing[2] = 'l'; etc. But you are saying myThing[0] = "Hello" a whole word. Which one is correct?
 
  • #250
I am learning loops, the goto, while, do-while all doing the same thing. Do I have to learn all or them or I just stick with one and be done with it? too many options here.
 
  • #251
There are reasons that goto is frowned on. Some standards forbid it. Bad programmers have put so many gotos in their code that they become spider webs. If you use it, make sure there is no other way and keep it as simple as possible. Avoid it if possible.
 
  • Like
Likes yungman
  • #252
FactChecker said:
There are reasons that goto is frowned on. Some standards forbid it. Bad programmers have put so many gotos in their code that they become spider webs. If you use it, make sure there is no other way and keep it as simple as possible. Avoid it if possible.
So "while" it is! too many options in C++.
 
  • #253
yungman said:
I thought myThing is a 20 character string, that is each element is only ONE charater. like if myThing [] = "Hello"
No. In the declaration std::string myThing[20];, myThing is a container with 20 elements, each of which is an std::string. Each element is not necessarily a single character.
yungman said:
myThing[0] = 'H'; myThing[1] = 'e'; myThing[2] = 'l'; etc. But you are saying myThing[0] = "Hello" a whole word. Which one is correct?
See above.
yungman said:
I am learning loops, the goto, while, do-while all doing the same thing. Do I have to learn all or them or I just stick with one and be done with it? too many options here.
The goto statement is not a loop -- it's a branch instruction.
There are three types of loops: for (which you didn't mention), while, and do ... while. They don't all do the same thing.
A for loop is usually used when you want the loop to execute a specific number of times.
A while loop is usually used when you don't know how many times the loop should execute. It checks the condition expression first, so the loop body might not execute at all.
A do...while loop is also used when you don't know how many times the loop should execute, but it checks the condition expression after each loop iteration. It will always execute at least once.
FactChecker said:
There are reasons that goto is frowned on. Some standards forbid it. Bad programmers have put so many gotos in their code that they become spider webs.
Granted, goto is usually frowned on, but there are legitimate reasons to use it, such as to bail out under extraordinary conditions. Some arguments pro, per Steve McConnell's "Code Complete," pp. 348,349.
A well-placed goto can eliminate the need for duplicate code.
The goto is useful in a routine that allocates resources, performs operations on those resource, and then deallocates the resources. With a goto, you can clean up in one section of code.
In some cases, the goto can result in faster and smaller code. Knuth's 1974 article cited a few cases in which the goto produces a legitimate gain.
Two decades' worth of research with gotos has failed to demonstrate their harmfulness. In a survey of the literature, B.A. Sheil concluded that unrealistic test conditions, poor data analysis, and inconclusive result failed to suppor the claim of Shneiderman and others that the number of bugs in code was proportional to the number of gotos (1981).
Finally, the goto was incorporated into the Ada language, the most carefully engineered programming language in history. Ada was developed long after the arguments on both sides of the goto debate had been fully developed.
yungman said:
So "while" it is! too many options in C++.
Suit yourself, but really, you should learn all three loop constructs: for, while, do...while.
 
  • Like
Likes sysprog
  • #255
yungman said:
I thought myThing is a 20 character string, that is each element is only ONE charater. like if myThing [] = "Hello"
myThing[0] = 'H'; myThing[1] = 'e'; myThing[2] = 'l'; etc. But you are saying myThing[0] = "Hello" a whole word. Which one is correct?
With any of these declarations:
Code:
std::string myThing[20];
std::vector <std::string> myThing(20);
std::array <std::string, 20> myThing;

myThing is a collection of 20 strings. Each string is a collection of characters. If you want to access individual characters, you need to specify two indexes: which string and which character. Example:

Code:
std::string myThing[20];
myThing[0] = "Hello";
myThing[1] = "my";
myThing[2] = "name";
std::cout << "First word is " << myThing[0] << std::endl;
std::cout << "Second letter of third word is " << myThing[2][1] << std::endl;
myThing[0][0] = 'J';
std::cout << "First word is now " << myThing[0] << std::endl;  // guess it, then try it!
 
Last edited:
  • Like
Likes FactChecker and sysprog
  • #256
Mark44 said:
No. In the declaration std::string myThing[20];, myThing is a container with 20 elements, each of which is an std::string. Each element is not necessarily a single character.
See above.
The goto statement is not a loop -- it's a branch instruction.
There are three types of loops: for (which you didn't mention), while, and do ... while. They don't all do the same thing.
A for loop is usually used when you want the loop to execute a specific number of times.
A while loop is usually used when you don't know how many times the loop should execute. It checks the condition expression first, so the loop body might not execute at all.
A do...while loop is also used when you don't know how many times the loop should execute, but it checks the condition expression after each loop iteration. It will always execute at least once.
Granted, goto is usually frowned on, but there are legitimate reasons to use it, such as to bail out under extraordinary conditions. Some arguments pro, per Steve McConnell's "Code Complete," pp. 348,349.

Suit yourself, but really, you should learn all three loop constructs: for, while, do...while.
To me, for is different from the others as you program how many times. The other ones just loop until jumping out. Just use an if statement to to goto start. They all do the same thing at the end.
 
  • #257
  • Haha
Likes sysprog
  • #258
yungman said:
To me, for is different from the others as you program how many times.
All three loop constructs are different, as I explained in post #253.
yungman said:
The other ones just loop until jumping out.
No. The body of a while loop might not execute at all.
yungman said:
Just use an if statement to to goto start. They all do the same thing at the end.
Well, sure, the assembly code that the compiler generates boils down to compare instructions and jump instructions.
yungman said:
too many options in C++.
All procedural programming languages at a higher level than assembly provide the same sorts of loop statements.
 
  • #259
I have been reviewing int array and char array, and then strings. A lot of my confusion are in this area. I feel I have been asking stupid questions. Also, I am reading back this thread again, memory is failing!

I'll be back.
 
  • #260
Mark44 said:
......
C++:
char sayHello2 = "Hello";
Both definitions store the letters in the first 5 bytes of memory, followed by an ASCII null character. Both arrays have a length of 5, the number of characters up to the null.
........
This is about post #239
I though this is a 6 elements char array as the last one is '\0'.
I want to confirm these

1) You can declare: char myChar[] = " This" to create a 5 elements char array. But after this, you can ONLY write one element at a time. There's no myChar = "ABCD" one step filling 4 characters.

2) After declaring int myNumbers[] = {1,2,3,4,5}, I cannot read all 5 numbers in one code line, has to be one element at a time eg. cout << myNumbers[1] << endl; to read out "2".

3) There is no command to write all 5 numbers into myNumber[] in one line of code, has to be one number at a time.

Thanks
 
  • #261
C++ and C can be very frustrating for scientific and engineering use. It lacks several features that were in FORTRAN specifically for that use (like reading and writing arrays).
 
  • #262
yungman said:
1) You can declare: char myChar[] = " This" to create a 5 elements char array. But after this, you can ONLY write one element at a time. There's no myChar = "ABCD" one step filling 4 characters.
Correct. The reason that you can't assign a new string to it is that myChar is a constant -- it can't be modified. In particular it is a pointer constant, which I don't think you've gotten to yet.
yungman said:
2) After declaring int myNumbers[] = {1,2,3,4,5}, I cannot read all 5 numbers in one code line, has to be one element at a time eg. cout << myNumbers[1] << endl; to read out "2".
Sort of correct, although your use of "read out" is a bit confusing. I would rephrase this to say that you can't output all of the numbers at the same time -- you have to access them one at a time.
I said "sort of correct" because the following is one code line.
Code:
cout << myNums[0] << myNums[1] << myNums[2] << myNums[3] << myNums[4] << endl
yungman said:
3) There is no command to write all 5 numbers into myNumber[] in one line of code, has to be one number at a time.
Correct. After the array has been initialized, there's no way to reassign a set of new values other than one at a time.
 
  • Like
Likes yungman
  • #263
Mark44 said:
......
C++:
char sayHello2[] = "Hello";
Both definitions store the letters in the first 5 bytes of memory, followed by an ASCII null character. Both arrays have a length of 5, the number of characters up to the null.
........
I though this is a 6 elements char array as the last one is '\0'.
Mark44 said:
Correct. The reason that you can't assign a new string to it is that myChar is a constant -- it can't be modified. In particular it is a pointer constant, which I don't think you've gotten to yet.
I can change the characters in myChar, it is not defined as constant. just has to be one by one.

char myChar[] = "Test";
myChar[0] = 'F'; myChar[1] = 'G'; etc.

If I do cout << myChar; I will get "FGst" as I changed the first two characters. I actually verified with the program.

ThanksI don't mean to challenge you, I just feel this is very important for me to understand this for once. I am NOT learning anything more new until I get through array and strings. I want it to be very clear on this.
 
Last edited:
  • #264
From post #239:
Mark44 said:
......
C++:
char sayHello2[] = "Hello";
Both definitions store the letters in the first 5 bytes of memory, followed by an ASCII null character. Both arrays have a length of 5, the number of characters up to the null.
........
yungman said:
I though this is a 6 elements char array as the last one is '\0'.
The array has 6 elements, but the length of the string is 5, as reported by strlen(), is the number of characters up to, but not including the terminating null.
yungman said:
I can change the characters in myChar, it is not defined as constant. just has to be one by one.

char myChar[] = "Test";
myChar[0] = 'F'; myChar[1] = 'G'; etc.

If I do cout << myChar; I will get "FGst" as I changed the first two characters. I actually verified with the program.
myChar IS a constant, but myChar[0], myChar[1], etc. are not constants. You can change the individual characters in the string, provided that you include the index of the character (in brackets).
In short, you can't do this: myChar = "FGst";, but you can change individual characters as you did in your code example.
The constant that myChar represents is the address of the first byte in the array. Because myChar is a constant, you are not allowed to assign an new value (i.e., an address) to it.
 
  • #265
Mark44 said:
From post #239:
The array has 6 elements, but the length of the string is 5, as reported by strlen(), is the number of characters up to, but not including the terminating null.
myChar IS a constant, but myChar[0], myChar[1], etc. are not constants. You can change the individual characters in the string, provided that you include the index of the character (in brackets).
In short, you can't do this: myChar = "FGst";, but you can change individual characters as you did in your code example.
The constant that myChar represents is the address of the first byte in the array. Because myChar is a constant, you are not allowed to assign an new value (i.e., an address) to it.
Thanks

So myChar is actually an address pointing to the first character of the array? I thought I asked and you said it's not somewhere. Is that true for int array, strings also?

Also, if I declare myChar[] = "Test", I forever locked myself that strlen() is going to be 4 for the rest of the program?

Thanks
 
  • #266
yungman said:
Is that true for int array, strings also?
With C-style arrays, whether of chars, ints or anything else, the name of an array by itself with no index (subscript) always "decays" to a pointer to the first item in the array.

This is not true for C++ std::string or std::array. The name of a string variable always represents the entire string, which is an object that contains more than just the chars. You can assign literal strings to them, and they automatically resize themselves accordingly.

Code:
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int main ()
{
    string name = "Huey";
    cout << name << " has " << name.length() << " characters." << endl;
    name = "Dewey";
    cout << name << " has " << name.length() << " characters." << endl;
    return 0;
}
Output:
Code:
Huey has 4 characters.
Dewey has 5 characters.

(the first C++ program on my new iMac, after installing the compiler today! :woot: )
 
Last edited:
  • Like
Likes sysprog and yungman
  • #267
jtbell said:
With C-style arrays, whether of chars, ints or anything else, the name of an array by itself with no index (subscript) always "decays" to a pointer to the first item in the array.

.......
What is the meaning of "decays"? Do you mean the array name is actually a POINTER ( address) to the first member of the array ( char, int and others)?

Thanks

I don't even look at strings and std::array. I just concentrate on array and C-String and resolve that, hopefully tomorrow I can get into strings. That's another can of worms. I just get the feeling that this is very very important to just go through the program and move on.
 
  • #268
yungman said:
What is the meaning of "decays"? Do you mean the array name is actually a POINTER ( address) to the first member of the array ( char, int and others)?
In a declaration like int numArray[5];, this is an array of five int values. However, if the name appears by itself, with no brackets, the name is a constant - the address of the first int value in the array -- a pointer. In this case, the address is of the first byte of the four bytes that hold an int value.
This business of array names being addresses will become more significant when you study functions and their parameters.
 
  • Like
Likes yungman
  • #269
Mark44 said:
In a declaration like int numArray[5];, this is an array of five int values. However, if the name appears by itself, with no brackets, the name is a constant - the address of the first int value in the array -- a pointer. In this case, the address is of the first byte of the four bytes that hold an int value.
This business of array names being addresses will become more significant when you study functions and their parameters.
Sorry, I need to absolutely confirm:
numArray[5] is an array with 5 elements.
numArray is the address that point to the first element of array numArray[5].

Are std::strings like this also? Without brackets is the address pointing to the first element?
 
  • #270
I just received this book today by Tony Gaddis
https://www.amazon.com/gp/product/0136022537/?tag=pfamazon01-20

Seems to be a really good book, I read the part on std::string section, it's very good. I should have waited for this book instead of wasting time on the first book. Now I am thinking whether I should stop and start over again following this new book.

I bought this used for cheap, it's in really bad shape. First time I bought a used book that is like this, pages wrinkled, pen marked all over the place, all worn out. I am going to read more, if it is that good, I might buy a better condition one.
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 107 ·
4
Replies
107
Views
9K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
16
Views
3K
  • · Replies 54 ·
2
Replies
54
Views
5K
  • · Replies 11 ·
Replies
11
Views
2K
  • · 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