Pat Mulvihill
Pat Mulvihill

Reputation: 53

C++ Debug Assertation Failed: "String is not null terminated"

Me and a friend are trying to solve this problem to practice programming. We've solved it, but it only actually works on his computer, which is running ubuntu. He's using gedit to compile. The solution we came up with is:

#include <iostream>
#include <string>
using namespace std;

int main()
{
char input[999], sentence[999];

cout << "Enter a sentence in all lower case with no punctuation." << endl;

cin.getline(input, 999);
strcat(sentence, input);

for(int x=0; input[x] != 0; x++)
{
        if (!(input[x] == 'a' || input[x] == 'e' || input[x] == 'i' || input[x] == 'o' || input[x] == 'u' || input[x] == ' '))
        {
            cout << input [x];
        }
}
    cout << endl;

    for(int x=0; input[x] != 0; x++)
    {
            if (input[x] == 'a' || input[x] == 'e' || input[x] == 'i' || input[x] == 'o' || input[x]         == 'u')
            {
            cout << input[x];
            }
    }       
    cout << endl;

    return 0;
}

It runs successfully on his computer, but I'm using visual studio 12 and I get a debug assertion error. It states that

Expression: (L"String is not null terminated"&&0).  

Any reason why I'm getting this error and he is not?

Upvotes: 1

Views: 3715

Answers (1)

Icemanind
Icemanind

Reputation: 48726

In C, strings are simply several characters joined together, terminated with a null terminator (\0)(ASCII code 0).

You can declare strings one of two ways. One way is to use a pointer declaration:

char *input;

Pointer declarations tell C to expect a pointer of char type. Later, you'd go on and dynamically allocate space for the string, using malloc.

The second way to declare a string is to create an array or characters. This is the method you are using:

char input[999];

You are telling the compiler that you need 999 bytes of type char to store your data. The compiler allocates 999 bytes, but it does NOT initialize it. The simplest way to initialize a string array is like this:

input[0] = 0;

By doing this, the first element in the array is set to the null terminator character. If you want to guarantee every element is set to 0, you can use memset:

memset(input, 0, 999);

C has many string functions. One of them is strcat. strcat concatenates two strings together. The problem, in your case, was that since the string wasn't initialized, "garbage data" was in your string. When you don't initialize it, any kind of random data could be in your string and that was why you were getting the error. Your friend didn't get the error just out of random luck. He just as easily could have gotten the error too. The string was probably allocated in an area of memory that just happen to be filled with 0's (the null terminator).

strcpy works because strcpy duplicates the contents of one string and places it into another string. It doesn't matter if that other string was initialized. It will simply overwrite it with the source string.

Upvotes: 3

Related Questions