I've written the code but it's not working.
Code is as follows:
int main()
{
int size=25;
char c,huma[size];
int i;
while(i<size && c!='\n')
{
cin>>c;
huma[i]=c;
i++;
}
huma[i]='\0';
cout<<huma;
}
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
Your
i
has radom value at the start. ALWAYS set initial values for variables to avoid problems.Variable without a value does not mean it will be 0 or anything else that you expect.
Ok ! But still it's not working. Actually the loop is not terminating whether I press 'enter' or the size limit exceeds. And Hence the entered loop is not displayed at the end.
cin
is whitespace delimited, so any whitespace (including\n
) will be discarded.c
will never be\n
.You can use the getline method to get the line, then istringstream to get formatted input from the line. Or alternatively use C function
fgets
.