I'm a self-taught programmer and I've been going over this part for 4 days now and I just can't get the hang of it. It doesn't seem useful and it doesn't make sense. I've provided everything the author described about this code so you can picture it clearer. P.S. - I obviously understand what it does, but I don't understand the code itself.
--------------------------------------------------------------------------------
// Concatenate - concatenate two strings
// with a " - " in the middle
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
// read first string...
char szString1[256];
cout << "Enter string #1:";
cin.getline(szString1, 128);
// ...now the second string...
char szString2[128];
cout << "Enter string #2:";
cin.getline(szString2, 128);
// ...concatenate a " - " onto the first...
concatString(szString1, " - ");
// ...now add the second string...
concatString(szString1, szString2);
// ...and display the result
cout << "\n" << szString1 << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}
// concatString - concatenate the szSource string
// onto the end of the szTarget string
void concatString(char szTarget[], const char szSource[])
{
// find the end of the first string
int targetIndex = 0;
while(szTarget[targetIndex])
{
targetIndex++;
}
// tack the second onto the end of the first
int sourceIndex = 0;
while(szSource[sourceIndex])
{
szTarget[targetIndex] =
szSource[sourceIndex];
targetIndex++;
sourceIndex++;
}
// tack on the terminating null
szTarget[targetIndex] = '\0';
}
The program begins by reading a string from the keyboard. The program does not use the normal cin >> szString1for two reasons. First, the cin >> operation stops reading when any type of whitespace is encountered. Characters up to the first whitespace are read, the whitespace is tossed and the remaining characters are left in the input hopper for the next cin >> statement. Thus, if I were to enter "the Dog", szString2would be filled with "the" and the word "Dog", would be left in the input buffer.
The second reason is that the getline() allows the programmer to specify the size of the buffer. The call to getline(szString2, 128) will not read more than 128 bytes no matter how many are input.
Instead, the call to getline() inputs an entire line up but not including the newline at the end. We'll review this function with other file I/O functions in detail in Chapter 23.
After reading the first string into szString1[], the program appends " - " onto the end by calling concatString(). It concatenates the second string by calling concatString() with szString2[].
The concatString() function accepts a target string, szTarget, and a source string, szSource. The function begins by scanning szTarget for the terminating null character, which it stores in targetIndex. The function then enters a second loop in which it copies characters from the szSource into szTarget starting at the terminating null. The final statement in concatString() slaps a terminating null on the completed string.
An example output from the program appears as follows:
Enter string #1: This is a string
Enter string #2: THIS IS A STRING
This is a string - THIS IS A STRING
Press any key to continue...
--------------------------------------------------------------------------------
I'm not really getting any of this. If someone can please explain the code in simpler text. I'm just a beginner and this is a huge jump for me. :(
The text looks pretty clear to me. If I just explained the code, it'd probably look a lot like what the author wrote, so are you having trouble with some part in particular?
Like I don't understand the cin.getline(szString1, 128) and the concatString(szString1, " - ") I dont understand , is this a function? Or like a specific call to C++. I'm just really confused. It's the whole thing in particular.
The former is a member function of std::istream, of which std::cin is an instance. Don't worry too much about the terms "member" and "instance" for now.
The latter is a user-defined ("user" as in user of the language, not user of the program) function.
Anything in the form x(y) is a function call to x passing y as a parameter.
I dont understand , is this a function? Or like a specific call to C++.
Yeah that's what I meant, by call I meant like a function in another library. But I'm starting to get it now. By the way is this particular 'thing' even important? Like will I build upon it in the future?
Just the point of this code. Like, when would you ever use this in a real life situation. It makes no sense and C++ seems to be going way to far into this. However, I may be wrong, will this become important in the future?
Like jsmith told you before, this is just an introduction to array handling, which is important to understand pointers, which are probably the most important part of C/++. So no, the code itself is not important, what's important is that you understand how arrays work.