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.
  • #211
Just great, Amazon LOST my book! First time happen to me! Still no book!
 
Technology news on Phys.org
  • #212
yungman said:
Just great, Amazon LOST my book! First time happen to me! Still no book!
What book did Amazon lose?
 
  • #213
sysprog said:
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:View attachment 266730
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.
I actually copied this link already yesterday, that's the one that I commented about hard to read. I even copy onto a word doc and modified to make it easier to read. I am still checking, seems like the order of the parameters are switched from what Mark44 suggested. I need to play with the program to verify.
 

Attachments

  • strcpy_s wcscpy_s.docx
    21.3 KB · Views: 134
  • #214
sysprog said:
What book did Amazon lose?
https://www.amazon.com/gp/product/1484233654/?tag=pfamazon01-20

I am not very lucky in C++!

I am still waiting for two other books:
https://www.amazon.com/gp/product/1118823877/?tag=pfamazon01-20

https://www.amazon.com/gp/product/0136022537/?tag=pfamazon01-20

The last one is used in my grandson's C++ class he took. He's going to be Jr. in college. That's actually gave me the idea of learning languages. He is CS major, so grandpa has to try to keep up! I even got his homework assignments and all the answers. But those books are bought used, it won't arrive until the early next month.
 
  • Like
Likes Jarvis323
  • #215
yungman said:
I am still checking, seems like the order of the parameters are switched from what Mark44 suggested. I need to play with the program to verify.
Reiterating @Mark44's code, with 2 comment lines added:
Code:
#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;
    // get a line of input from the user
    getline(cin, userInput); 
    char copyInput[20] = { '\0' };
    if (userInput.length() < 20)// check bound.
    {
        // copy user line to copyInput (to-field, length, from-field)
        strcpy_s(copyInput, 20, userInput.c_str()); 
        cout << " CopyInput contains:   " << copyInput << endl;
    }
    else
        cout << " Bound exceed, won't copy!" << endl;
}
And from the sample at the page to which @Mark44 provided a link (with a comment line added):
Code:
// crt_wcscpy_s.cpp
// Compile by using: cl /EHsc /W4 crt_wcscpy_s.cpp
// This program uses wcscpy_s and wcscat_s
// to build a phrase.

#include <cstring>  // for wcscpy_s, wcscat_s
#include <cstdlib>  // for _countof
#include <iostream> // for cout, includes <cstdlib>, <cstring>
#include <errno.h>  // for return values

int main(void)
{
    wchar_t string[80];
    // using template versions of wcscpy_s and wcscat_s:
    // not strcpy_s  but still (to-field, length, from-field) 
    wcscpy_s(string, L"Hello world from ");
    wcscat_s(string, L"wcscpy_s ");
    wcscat_s(string, L"and ");
    // of course we can supply the size explicitly if we want to:
    wcscat_s(string, _countof(string), L"wcscat_s!");
    std::wcout << L"String = " << string << std::endl;
}
The order of the parameters is the same in the MS sample code as it is in @Mark44's code: to-field, length, from-field.
 
  • #216
  • Like
Likes Jarvis323
  • #217
Mark44 said:
........
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.

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.

I am still a little confused about the syntax of strcpy_s(). In my program that works, line 26 shows:

strcpy_s(copyInput, 20 , userInput.c_str());

where copyInput is the destination char string that the content of userInput is copied into. And userInput contain the source of the characters to be copied into copyInput.

But you said the first parameter is source of the copy and third is the destination of the copy. I am missing something.
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;
}

Also, I am confused when to use userInput.length() or sizeof (copyInput)? I thought both are character strings.

Thanks.
 
  • #219
Mark44 said:
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.
This is backwards ##-## the left-to-right correct order is: destination, length, source ##-## @Mark44's code specifies the parameters in the correct order.
 
  • #220
sysprog said:
This is backwards ##-## the left-to-right correct order is: destination, length, source ##-##
@Mark44's code specifies the parameters in the correct order.

Mark44 said:
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.
Ok, which one is the first parameter? And which is the third parameter? What is the correct direction to read from? Left to right or right to left as the first? This is the point of my confusion.
 
  • #221
I took a quick look, the strcpy() have a different name....it's called strncpy(). I tried substituting strcpy_s with strncpy, it gave me an error!

But the book instruct to go to:
http://www.dummies.com/extras/beginningprogrammingcplusplus.

and download :codeblocks-13.12mingw-setup.exe

Is different IDE uses different spelling of operators or different names all together?

But when I try to go to the website, it doesn't even work!
 
  • #222
yungman said:
I am still checking, seems like the order of the parameters are switched from what Mark44 suggested. I need to play with the program to verify.
yungman said:
But you said the first parameter is source of the copy and third is the destination of the copy. I am missing something.
The code using strcpy_s worked as it should, but my explanation misstated which parameter was the destination and which was the source of the copy. I have edited my earlier post to correct this error.
 
  • Like
Likes yungman
  • #223
yungman said:
Ok, which one is the first parameter? And which is the third parameter? What is the correct direction to read from? Left to right or right to left as the first? This is the point of my confusion.
I specifically included left to right as the order.

It's: strcpy_s(copyInput, 20, userInput.c_str()); ##-## the parameters are, in left-to-right order, (to-field, length, from-field), or (destination, length, source).
 
  • #224
yungman said:
I took a quick look, the strcpy() have a different name....it's called strncpy(). I tried substituting strcpy_s with strncpy, it gave me an error!
strcpy(), strncpy(), and strcpy_s() are three different functions, with different parameters. strcpy_s() is similar to strncpy(), except that the order of the parameters is different and they return different types.

You can't just replace one function with another without checking to see if you are using it correctly. Besides the VS documentation, another site I use for its C and C++ library function documenation is http://cplusplus.com.
yungman said:
But the book instruct to go to:
http://www.dummies.com/extras/beginningprogrammingcplusplus.

and download :codeblocks-13.12mingw-setup.exe

Is different IDE uses different spelling of operators or different names all together?

But when I try to go to the website, it doesn't even work!
You don't need Codeblocks -- you already have VS. You should be able to type in the programs in the "Dummies" book and run them. The book is for C++, so I don't understand what you're talking about with regard to different spelling of operators and all that.
 
  • Like
Likes sysprog
  • #225
yungman said:
I took a quick look, the strcpy() have a different name....it's called strncpy(). I tried substituting strcpy_s with strncpy, it gave me an error!

But the book instruct to go to:
http://www.dummies.com/extras/beginningprogrammingcplusplus.

and download :codeblocks-13.12mingw-setup.exe

Is different IDE uses different spelling of operators or different names all together?

But when I try to go to the website, it doesn't even work!
In C, strncpy() is like strcpy(), but strncpy() copies only the specified length. I recommend against your using it, for the reasons stated by @Mark44. I also think that you shouldn't use a multitude of different C++ compilers this early in your learning of the language. Despite the Visual Studio IDE and C++ compiler not being the most streamlined offering out there, it is among the very best, it's free, and it's the de facto industry standard.
 
  • Like
Likes Mark44
  • #226
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
#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
sysprog said:
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
yungman said:
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
sysprog said:
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
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
yungman said:
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
Mark44 said:
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
yungman said:
I am still confused when to use userInput.length() or sizeof (copyInput)? I thought both are character strings in this program.
Mark44 said:
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
yungman said:
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
wle said:
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
Mark44 said:
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
Mark44 said:
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
yungman said:
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
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];).
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

  • Mark44 array string.docx
    13.7 KB · Views: 103
  • #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
  • #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.
 
Back
Top