MidnightSky00
Technical User
Hi there,
I seem to be having a very simple problem here, but yet I can't figure it out. When I run the following, if someone enters a string that is larger than 20 the program will kick out. I am pretty sure I know why, but yet I don't know how to fix it without just increasing the buffer size, which is not what I want to do, if possible.
It's very simplistic code, and it is embarrassing that I can't resolve this.
Thanks for the help in advance.
#include <iostream.h>
void main()
{
char word1[3][20];
int x;
for (x=0;x<3;x++)
{
cout << "\nEnter a word: ";
cin.getline(word1[x],20);
}
//cin.ignore();
for (x=0;x<3;x++)
{
cout << "\nYour word was " << word1[x] << "\n\n";
}
}
I seem to be having a very simple problem here, but yet I can't figure it out. When I run the following, if someone enters a string that is larger than 20 the program will kick out. I am pretty sure I know why, but yet I don't know how to fix it without just increasing the buffer size, which is not what I want to do, if possible.
It's very simplistic code, and it is embarrassing that I can't resolve this.
Thanks for the help in advance.
#include <iostream.h>
void main()
{
char word1[3][20];
int x;
for (x=0;x<3;x++)
{
cout << "\nEnter a word: ";
cin.getline(word1[x],20);
}
//cin.ignore();
for (x=0;x<3;x++)
{
cout << "\nYour word was " << word1[x] << "\n\n";
}
}