How to enter a string character by character from the user?

sabbaglearnersabbaglearner pakistan
edited June 2016 in C and C++

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')




  • B00B00
    edited June 2016

    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.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!