String Crossing Over to Another and Program Not Completing.

Hello! I wrote this small program that concatenates two strings with a space in the middle. It appeared to work fine at first and resembled something like the following:
This program takes two strings and displays them together with a space between them.
Please enter the first string: Hi!
Please enter the second string: Hello!
Hi! Hello!
Press any key to continue...
(exits).

Then I decided to test the 128 byte limit. I probably even passed the 260 character Array that I set up. I don't think it would matter because the cin only reads the first 128 bytes (char is usually worth about 1 byte), correct me if I am wrong and it does matter. When I tested the cin limit I did something that resembled this:
This program takes two strings and displays them together with a space between them.
Please enter the first string:nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn... // and so on.
//But then something strange happened. The second string, without me entering anything showed something such as:
Please enter the second string:nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
// Then the program wouldn't perform what it was designed to do (display two strings with a space in the middle) and immediately displayed.
Press any key to continue...
(exits).
Can anbody explain what happened?
Thank you all for helping me in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

  #include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    cout << "This program takes two strings that you enter in and displays them together with a space in between them.\n";
    cout << "Please enter the first string:";
    char szString1[260];
    cin.getline(szString1, 128);

    cout << "Please enter the second string:";
    char szString2[260];
    cin.getline(szString2, 128);

    strncat(szString1," ", 260);
    strncat(szString1, szString2, 260);

    char * pszString1 = szString1;

    while(*pszString1 != '\0')
    {
        cout << *pszString1;
        pszString1++;
    }




    system("PAUSE");
    return 0;



}
Last edited on
You did fill the buffer with characters. The first getline took 127 of them. The next getline took up to 127 of the characters that were still on the buffer.

Print some newlines before line 25 to proof this.
Btw, your output loop could be replaced with: cout << szString1;

While testing is educational, would std::string be a more useful subject for study?
Interesting, so the second getline just picked up where the first one left off. Thank you for this information.
I also realize my loop could be replaced with "cout << szString1;," but I am trying to practice coding with pointer variables.
What do you mean by print some new lines?
so the second getline just picked up where the first one left off.

Actually I believe that your first entry will put cin into an error state because it didn't find the delimiter. Since the stream is in as error state no processing will occur until the error is cleared.


From the documentation of getline() from this site:

The failbit flag is set if the function extracts no characters, or if the delimiting character is not found once (n-1) characters have already been written to s.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
    cout << "This program takes two strings that you enter in and displays them together with a space in between them.\n";
    cout << "Please enter the first string:";
    char szString1[260];
    cin.getline(szString1, 12);

    if(!cin)
    {
        cout << "cin is in an error state" << std::endl;
    }

    int test = 1000;
    cin >> test;

    cout << test << endl;

    system("PAUSE");
    return 0;
}


If you enter and run this program you should see that the input stream is put into an error state if you enter 12 or more characters for the first string. The input of test will be skipped and it's value will remain at 1000.


Last edited on
I see. So I need to figure out a way to limit the amount of characters entered by the user to 128?
You first need to decide what to do if the user entered more characters than what you desired. For example you may decide that it's okay to just discard the extra characters, in which case you would clear the stream error then call the ignore() function to discard the characters. Or you may decide that these extra characters need to be retrieved and appended onto the C-string, until the size of the string is reached. In this case you would clear the stream error state and retrieve the characters into a temporary buffer that you append to the target C-string. Or you may decide that the program can't continue and exit the program.

But whatever you decide you will need to clear the stream error state. Unless you clear the error state no further operations can occur on that stream.

If you're learning C++ I really suggest you learn C++, which means using std::strings instead of the error prone C-strings.

Lastly what does the sz prefix mean, if anything?
The sz prefix means zero-terminated string.
Yes, I will learn std::strings.
Do you know that using prefixes or suffixes like that went out of style in the late 90's. Even Microsoft saw the errors of their ways and came into the light.



I am not totally sure what is wrong with it. It lets me know the type of variable, therefore making it easier to read.
Last edited on
That is a matter of style and maintenance.

Lets assume that a function computes something. It uses variable in many places. Variable was declared float fNumber;

At some point some code update requires a type change: double fNumber;
The rest of the code would not require any changes, but now the name lies.


You can usually declare your variables close to where they are used and therefore you can check the type from the declaration easily. (IDE could show it for you too.) If, however, the code spans several pages, then ask whether it should by refactored to simpler units.
Thank you for explaining that to me!
Registered users can post here. Sign in or register to post.