Code:int main() { char x[10];int i=0; printf("please enter x:\n"); do{ scanf(" %c",&x[i]); i++;} while(x[i]!=13); printf("%s",x); return 0; }
Code:int main() { char x[10];int i=0; printf("please enter x:\n"); do{ scanf(" %c",&x[i]); i++;} while(x[i]!=13); printf("%s",x); return 0; }
You should:
- Explain what your program is about.
- Tell us how does your program not work, e.g., provide test input, expected output, and actual output.
- #include the relevant headers
- Indent your code properly
- Avoid magic numbers like 13 in favour of named constants or literals like '\r'
Anyway, instead of that loop, why not use fgets? You need to remember that fgets will save the newline ('\n') read if there is space for it, but that is easily dealt with.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
> while(x[i]!=13);
You're reading from a FILE* stream, so the standard end of line is '\n', even if your underlying OS uses '\r'
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.