Okay, so I have said code:

#include <iostream>
#include <cstdlib>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    char input[8];
    int counter = 0;
    int newletter;

    do
    {
    cout << "Please enter a 7 letter word to encrypt:\n>";
    cin >> input;

    if(input[8] != '\0')
    {
        input[8] = '\0';
    }

    }while(input[8] != NULL);



    while(counter <= 6)
    {
        newletter = input[counter];
        input[counter] = ++newletter;
        counter++;
    }

    cout << "your encrypted word is:\n" << input << endl;

    system("PAUSE");
    return 0;

}

and I would like to know about the NULL terminator. The above program is a homework assignment where we need to encrypt a string. great, that part works perfectly, and thats not what I'm here about, my homework is done and turned in.

What I would like to know is:
if the user inputs a string that is more than 7 characters long, it can mess up the program. I tried to do something above where it basically says "If the last character that the array can store is not the character that ends the array, make it so;" but yet, it doesn't do so...

Am I wrong in thinking that the below statement should work?

do
    {
    cout << "Please enter a 7 letter word to encrypt:\n>";
    cin >> input;

    if(input[8] != '\0')
    {
        input[8] = '\0';
    }

    }while(input[8] != NULL);

((pardon that if statement in there... I was trying a few things, but it should still run the loop just fine, if my logic is right..))

If someoen could set me on the straight and narrow, that would be great... thanks :)

Derek Elensar

input[8] is not the last member of your array, input[7] is.

When you declare the array you put the size, when you use the array, you put the index.

After taking in the input, you could say

if(input[7] != '\0')
   input[7] = '\0'

What I think you were doing is trying to march down the string, which you can do by

int i = 0;
while(input[i] != '\0')
{ 
   cout<<input[i]<<" ";
   i++;
}
(it could be a for loop too, I was just using while because you did)

input[8] is not the last member of your array, input[7] is.

When you declare the array you put the size, when you use the array, you put the index.

After taking in the input, you could say

if(input[7] != '\0')
   input[7] = '\0'

What I think you were doing is trying to march down the string, which you can do by

int i = 0;
while(input[i] != '\0')
{ 
   cout<<input[i]<<" ";
   i++;
}
(it could be a for loop too, I was just using while because you did)

Shouldn't your code initialize input to a value that is not '\0' first.

I omitted the cin portion, is that what you mean?

I omitted the cin portion, is that what you mean?

Yeah I guess you could do it that way... something like below.

#include <iostream>

#define ARR_SIZE 8

int main()
{
  int i = 0;
  char input[ARR_SIZE];
  
  while(input[i] = std::cin.get() , input[i]  != '\n' && i < (ARR_SIZE - 1))
  { 
	i++;
  }
  
  input[i] = '\0';

  std::cout<<input<< std::endl;;
  return 0;
}

Oh, no, miscue on my part then. I meant that you can use >> with char arrays. It's conscious of the need for '\0' at the end. You can put in more characters than your buffer will hold EDIT: and it plows through to the adjacent memory. It will only put the '\0' if there's room.

Wow, thanks for the help!

input[8] is not the last member of your array, input[7] is.

You're right. it's 8 big, but it starts at 0 ;)

also:

After taking in the input, you could say

if(input[7] != '\0')   
input[7] = '\0';

If someone inputted a string that was 15 letters long, would the leftover characters after the null terminator be discarded? or would they remian in the input stream?

And on one last note: is there a way to expand my character array size once it's defined?

i.e. I declare char input[7]. Am I able to, down the road, change [7] to, say, [12]?

If someone inputted a string that was 15 letters long, would the leftover characters after the null terminator be discarded? or would they remian in the input stream?

And on one last note: is there a way to expand my character array size once it's defined?

Your first question. Yes the characters remain in the stream.

Your second question. No you can't change the size of a static array but you can create a dynamic array with new and that array can be changed(or recreated).

If someone inputted a string that was 15 letters long, would the leftover characters after the null terminator be discarded? or would they remian in the input stream?

What I was saying in the post above is that in this case, they actually end up clobbering the memory adjacent to the array, so however many characters you have beyond the limit writes over whatever is next to it by that many bytes.

Say we have char arr[4], and there's a second array of size 4 next to it with "abc"
|   |   |   |   |'a'|'b'|'c'|'\0'|
cin >> arr; //we enter "123456"
|'1'|'2'|'3'|'4'|'5'|'6'|'c'|'\0'|
Not only does cin not put the null terminus on (as there's no room), but we've clobbered the other array (as a side effect, any function relying on the position of the null terminus will think this string is "123456c".

And on one last note: is there a way to expand my character array size once it's defined?

i.e. I declare char input[7]. Am I able to, down the road, change [7] to, say, [12]?

Not with a fixed-size array. There's a way to do it (realloc - http://en.wikipedia.org/wiki/Malloc#realloc) if you dynamically allocate the array at runtime with malloc (a C function that is,in some ways, a predecessor to the "new" keyword in C++, but also, I believe, still used in a lot of the underlying machinery of C++ memory management). Look up "new" and "delete" if you want to know how to do things in C++.

Yes the characters remain in the stream

Try it with 2 char arrays that you declare one right after the other, which don't have to adjoin in memory but with such a small program they often do, then write more into the second one than it can hold. If you print it out it's clobbered the first one.

Try it with 2 char arrays that you declare one right after the other, which don't have to adjoin in memory but with such a small program they often do, then write more into the second one than it can hold. If you print it out it's clobbered the first one.

I was assumed, he was only taking seven characters from the stream and placing them in the array and the terminating it(the array) with '\0'. In this instance, the remaining characters would be in the stream.

In the scenario your describing, you would indeed have a buffer overflow and possible data corruption.

Mmkay, thanks for helping me colve this. I'll look into that link that you posted jonsca. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.