Thread: problem in code

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    1

    problem in code

    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;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with code
    By Daniel990 in forum C Programming
    Replies: 7
    Last Post: 04-27-2015, 10:25 AM
  2. Code problem or compiler problem....?
    By miloki in forum C Programming
    Replies: 4
    Last Post: 03-05-2015, 12:48 AM
  3. Having a problem with my code :(
    By vivec45 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2011, 06:06 AM
  4. problem with my C code
    By datainjector in forum C Programming
    Replies: 2
    Last Post: 10-04-2009, 07:32 PM
  5. problem with my code
    By stilllearning in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 03:02 PM

Tags for this Thread