//Read characters into a char Array until the character 'p' is encountered,
//up to a limit of 10 characters (including the terminating null character).
//Extract the delimiter 'p' from the input stream and discard it.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
char characterArray[10];
char delimiter;
int ArrayPos = 0;
//read a string into character array 10 characters
cout << "Enter a string: ";
while(ArrayPos <= 10){
//if the character is a p, we need to get rid of it.
if(cin.peek() == 'p')
{
//extract the delimiter from the stream
ArrayPos = 11; //exit loop
}
else
{
characterArray[ArrayPos] = cin.get();
ArrayPos++;
}
}
cout << characterArray << endl;
system("pause");
return 0;
}
You use <= on line 24, so you try to write to the 11th element of your array (that is characterArray[10]). This element doesn't exist.
The reason you get garbage characters is because your array of characters does not have a terminating null character to tell when the end of the string is, so it just keeps going and reading random memory until it happens to hit a 0.