Trying to decide which programming language I want to learn

  • Thread starter yungman
  • Start date
  • #226
4,951
110
I flip through the new book and compare the names, there's a lot of difference. Here is an example I scan on String Manipulation Functions. I cannot find most of them in the other book. I tried strncpy and it did not work. I put a red mark in front of all the ones that I cannot find in the other book.

Dummie char.jpg


I also notice this C++ for Dummies use different #include header files also.
#include <cstdio>
#include <cstdlib>

Both are not used in my first book.

If the C++ for Dummies using names that won't work with VS, then it's not very useful.

Too bad, the kind of table I attached is EXACTLY what I hope to find in the book. Condense, to the point and clear. Too bad, it's on stuffs that is not in the other book and already tried strncpy and it doesn't work.

Well, I have two more books coming. If anyone have suggestion of a good book, I would buy another one.

I hope you guys can put up with me asking questions if I cannot find a good book.

Thanks
 
  • #227
1,655
911
#include <cstdio>
#include <cstdlib>
Those are the C++ versions of the C stdio.h and stdlib.h libraries.

I think that you should avoid using C library functions in C++.

Did you terminate your input strings with /0 ? ##-## that's null termination, and it's important to use it when you use ASCIIZ functions without a length parameter.
 
Last edited:
  • #228
4,951
110
Those are the C++ versions of the C stdio.h and stdlib.h libraries.

I think that you should avoid using C library functions in C++.

Did you terminate your input strings with /0 ? ##-## that's null termination, and it's important to use it when you use ASCIIZ functions without a length parameter.
Those are C library functions? That explains it. I am pretty much committed to VS, so if this doesn't work, then wait for the other books. This book is used, I only paid like $8 total including shipping. I am going to read some of the stuff anyway as there's still a lot of stuffs that look right. This book might explain stuffs better than the first book.
 
  • Like
Likes sysprog
  • #229
1,655
911
Those are C library functions? That explains it. I am pretty much committed to VS, so if this doesn't work, then wait for the other books. This book is used, I only paid like $8 total including shipping. I am going to read some of the stuff anyway as there's still a lot of stuffs that look right. This book might explain stuffs better than the first book.
That makes sense to me.
 
  • Like
Likes yungman
  • #230
4,951
110
That makes sense to me.
It is actually a much better book, a little too simple, but much better so far. I even experimented with one or two programs already. Since I went through 5 chapters and part of the 6th with the other book, I went through the C++ for Dummies very fast, scanned through over 100 pages last night already.

Just when I see some terms that is different, I write a short program to check whether it work with VS, if it works, it's all good, new things to learn.
 
Last edited:
  • #231
4,951
110
I am still confused when to use userInput.length() or sizeof (copyInput)? I thought both are character strings in this program.


C++:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <iostream>
#include <string>
using std::cout; using std::cin; using std::endl;
using std::string;

int main()
{
    string userInput;
    cout << " Enter a line of text:  ";
    getline(cin, userInput);
    cout << " The userInput is:  " << userInput << endl;
    cout << endl;
       
    cout << "length of userInput =  " << userInput.length() << endl;
    cout << endl;

    char copyInput[20] = { '\0' }; cout << " copyInput = " << copyInput  << endl;
    cout << endl;
    cout << " length of copyInput = " << sizeof(copyInput) << endl;
    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;

    return 0;
}
 
  • #232
34,155
5,773
I am still confused when to use userInput.length() or sizeof (copyInput)? I thought both are character strings in this program.
It's no wonder. Your program is using two different types of strings. userInput is an instance of the C++ template class, while copyInput is a plain old C-style array of type char. The C array is very simple -- it consists of 20 bytes in memory, and nothing else. The C++ class instance is much more complex -- it has class member functions (such as length() and getline() and quite a few more), and operators (such as + for concatenating two string objects).

Unless your book, the first one you've been working in, talks about the differences between C-type strings and C++ string objects, mixing the two types in a program is bound to lead to confusion. So far, I'm not very impressed at the quality of that book.
 
  • Like
Likes yungman and sysprog
  • #233
4,951
110
It's no wonder. Your program is using two different types of strings. userInput is an instance of the C++ template class, while copyInput is a plain old C-style array of type char. The C array is very simple -- it consists of 20 bytes in memory, and nothing else. The C++ class instance is much more complex -- it has class member functions (such as length() and getline() and quite a few more), and operators (such as + for concatenating two string objects).

Unless your book, the first one you've been working in, talks about the differences between C-type strings and C++ string objects, mixing the two types in a program is bound to lead to confusion. So far, I'm not very impressed at the quality of that book.
If you go on Amazon and look at the first book page 79. That's all the explanation it gives, nothing like what you said. I am going to go back and read the second book on array and strings!!

1) userInput is defined as std::string; Is everything defined as std::string all have class member functions (such as length() and getline() etc.)?

2) copyInput is defined as char copyInput[20];. Is the [20] telling this is a 20 characters array of characters? The first book mostly talk about int array, how many type of array is there ( I mean like int, char type)?


The second book C++ For Dummies is a whole lot better, might be simpler, but it really explain things a lot better. You should really read p79 on the first book on std::string. You'll see why I have so many questions. I am going to read the second book on this.
 
  • #234
wle
316
139
I am still confused when to use userInput.length() or sizeof (copyInput)? I thought both are character strings in this program.
It's no wonder. Your program is using two different types of strings.
C++ has a lot of feature duplication. It often provides two or more different ways of achieving substantially the same thing. This is something you need to accept up front and be ready to deal with if you've made the decision to learn C++.

Much of the feature duplication comes from C++ defining a new way of doing something while also retaining whatever solution the C programming language already had for the same kind of thing; some of it also just comes as a result of C++ accreting many new features since it started as "C with classes" forty or so years ago. Some examples I know, off the top of my head (and this is just what I remember from learning some of C++98 many years ago):
  • C-style strings (null-terminated character arrays) vs. C++ std::string objects.
  • More generally, C-style arrays vs. C++ template containers (std::vector<T> and such).
  • IO with C-style FILE * streams (printf() and co. work with these) vs. C++ stream objects (std::cout and family).
  • C-style pointers vs. C++ references and smart pointers.
  • The C malloc() and free() standard library functions vs. C++ new[] and delete[] keywords.
  • C functions and function pointers vs. C++ functionals (objects with an operator() method), including those created by lambda expressions since C++11.
  • C-style macros and C++ templates.
  • C and new C++ syntax for casts.
  • struct and class (in C++ these are identical except for different defaults).
  • C's setjmp()/longjmp() and C++'s try/catch keywords.
  • One specific to C++: function overloading, default arguments, and template specialisation.
To make things more confusing, sometimes the duplication is exact or nearly so (e.g. the different kinds of strings) and sometimes it's just a significant but partial overlap (e.g., there are important things you can do with macros that you can't with templates, and vice versa).

This trend looks like it's likely to continue. For example, C++ designers are apparently considering adding multi-methods or open-methods to support multiple (runtime) dispatch (as opposed to single dispatch, which is what virtual methods let you do in C++) so a future version of C++ could very well have two different kinds of class methods.
 
  • #235
34,155
5,773
If you go on Amazon and look at the first book page 79. That's all the explanation it gives, nothing like what you said. I am going to go back and read the second book on array and strings!!

1) userInput is defined as std::string; Is everything defined as std::string all have class member functions (such as length() and getline() etc.)?
Yes.
yungman said:
2) copyInput is defined as char copyInput[20];. Is the [20] telling this is a 20 characters array of characters? The first book mostly talk about int array, how many type of array is there ( I mean like int, char type)?
An array can have any base type, not just the primitive types such as char, short, int, long, float, double, etc. When an array is declared or defined, the number in the brackets indicates how many elements of the given type will be in the array.
yungman said:
The second book C++ For Dummies is a whole lot better, might be simpler, but it really explain things a lot better. You should really read p79 on the first book on std::string. You'll see why I have so many questions. I am going to read the second book on this.
 
  • Like
Likes yungman
  • #236
4,951
110
C++ has a lot of feature duplication. It often provides two or more different ways of achieving substantially the same thing. This is something you need to accept up front and be ready to deal with if you've made the decision to learn C++.

Much of the feature duplication comes from C++ defining a new way of doing something while also retaining whatever solution the C programming language already had for the same kind of thing; some of it also just comes as a result of C++ accreting many new features since it started as "C with classes" forty or so years ago. Some examples I know, off the top of my head (and this is just what I remember from learning some of C++98 many years ago):
  • C-style strings (null-terminated character arrays) vs. C++ std::string objects.
  • More generally, C-style arrays vs. C++ template containers (std::vector<T> and such).
  • IO with C-style FILE * streams (printf() and co. work with these) vs. C++ stream objects (std::cout and family).
  • C-style pointers vs. C++ references and smart pointers.
  • The C malloc() and free() standard library functions vs. C++ new[] and delete[] keywords.
  • C functions and function pointers vs. C++ functionals (objects with an operator() method), including those created by lambda expressions since C++11.
  • C-style macros and C++ templates.
  • C and new C++ syntax for casts.
  • struct and class (in C++ these are identical except for different defaults).
  • C's setjmp()/longjmp() and C++'s try/catch keywords.
  • One specific to C++: function overloading, default arguments, and template specialisation.
To make things more confusing, sometimes the duplication is exact or nearly so (e.g. the different kinds of strings) and sometimes it's just a significant but partial overlap (e.g., there are important things you can do with macros that you can't with templates, and vice versa).

This trend looks like it's likely to continue. For example, C++ designers are apparently considering adding multi-methods or open-methods to support multiple (runtime) dispatch (as opposed to single dispatch, which is what virtual methods let you do in C++) so a future version of C++ could very well have two different kinds of class methods.
That's exactly what I hate about all the new electronics in cars, printers, computers etc. Now even smart TV is the same. There are so many ways to do the same thing in the name of " finding your preference" and "easy to understand", it makes things so complicated and confusing. What's wrong with making it simple, one way doing one particular task, people just need to learn. Yes, some people are not up to this, but don't drag the people that can do it down with them in the name of equality and no body left behind!!!

How many times in your career you see people that just don't have it and drag everybody down in a project? Then everyone has to jump in and put out the fire. Nobody say we are all equal, some have it and some just don't. Why try to make them think they can do it by making it easier? Programming, electronics, hightech are NOT easy, it is HARD. Now, just because someone has a master degree imply the person have it, I've seen engineers with MSEE couldn't design if their lives depend on it.

Then the reliability issue. The fancier the program is, the more bugs you will have and reliability hurts. I cannot stress enough my 2018 car was in the shop over a month in the first 8 months. ALL computer problems, nothing really got fix, now we just come to live with it. Same brand 2014 we have, never been in the shop for problem yet ( knock on wood). You need few step to get to the menu you want, all the mouse pads, it's dangerous when you are driving.

Then the stupid Canon printers, my wife want to throw it down the street so many times. Keep asking you questions, then does it wrong anyway. Guess who get to try to fix everything........Me!!! then when everything work right for a while, stupid thing gets a software update, things change again, wife threaten to throw it out the window again!! We must have over 5 Canon printers, and those are NOT the cheapest model as we have a business that needs more than just a $59.99 printer......................AND the newer the electronics, the slower they are......I am serious. Stupid car, smart TV, printers. You name it we have problem with them.

Sorry about the long ranting.
 
  • #237
4,951
110
Yes.

An array can have any base type, not just the primitive types such as char, short, int, long, float, double, etc. When an array is declared or defined, the number in the brackets indicates how many elements of the given type will be in the array.
You won't find simple straight forward answers like this in the first book.

thanks
 
  • #238
4,951
110
Yes.
An array can have any base type, not just the primitive types such as char, short, int, long, float, double, etc. When an array is declared or defined, the number in the brackets indicates how many elements of the given type will be in the array.
I have more questions, seems like both strings and array look the same: myThing[20] and otherThing[20]. The only thing that tells the different a string and array is the declaration

1) std::string myThing[20] that tell people myThing[20] is a 20 element string. Where char otherThing[20] declare it's a 20 element array of characters. In another word, you have to declare it's a string. Am I right?

2) If the above is true, how come in the first book page 77, when it talk about character string, it just write:
char sayHello[] = { 'H', 'e', 'l','l','o', '\0'} as character string with 6 characters.

3) I see array can be declared as int, float, char etc. How about strings? Can it be int, float etc.? How do you declare the string ( syntax).

Thanks
 
  • #239
34,155
5,773
I have more questions, seems like both strings and array look the same: myThing[20] and otherThing[20]. The only thing that tells the different a string and array is the declaration
The fact that they are declared differently should be a clue that they aren't the same.
yungman said:
1) std::string myThing[20] that tell people myThing[20] is a 20 element string. Where char otherThing[20] declare it's a 20 element array of characters. In another word, you have to declare it's a string. Am I right?
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];).
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/
yungman said:
2) If the above is true, how come in the first book page 77, when it talk about character string, it just write:
char sayHello[] = { 'H', 'e', 'l','l','o', '\0'} as character string with 6 characters.
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.
yungman said:
3) I see array can be declared as int, float, char etc. How about strings? Can it be int, float etc.? How do you declare the string ( syntax).
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]
 
Last edited:
  • Like
Likes yungman
  • #240
4,951
110
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];).
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/
yungman said:
2) If the above is true, how come in the first book page 77, when it talk about character string, it just write:
char sayHello[] = { 'H', 'e', 'l','l','o', '\0'} as character string with 6 characters.
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.
yungman said:
3) I see array can be declared as int, float, char etc. How about strings? Can it be int, float etc.? How do you declare the string ( syntax).
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. I have to take the time to read first. I have to fix the stuffs in your response as it's hard to read. Attached is what I fix to make it easier for me to read. I am sure I will have more question later as I read both books and none of them are really that good.
 

Attachments

  • #241
34,155
5,773
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
  • #242
4,951
110
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.

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.

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
34,155
5,773
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.
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.
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
4,951
110
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
34,155
5,773
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
FactChecker
Science Advisor
Gold Member
5,787
2,153
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
4,951
110
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
jtbell
Mentor
15,635
3,682
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
4,951
110
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
4,951
110
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.
 

Related Threads on Trying to decide which programming language I want to learn

Replies
7
Views
4K
  • Last Post
Replies
14
Views
3K
  • Last Post
2
Replies
43
Views
3K
  • Last Post
Replies
9
Views
909
Replies
17
Views
2K
  • Last Post
2
Replies
29
Views
2K
Replies
9
Views
871
Replies
15
Views
3K
Top