Thread: program wont work, why?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    31

    program wont work, why?

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    int main()
    {
        char name[10];
    
        printf("Please enter your name.\n");
        getch();
        scanf("%s", name);
        printf("Hello %s!", name);
        getch();
    }
    after i put in a name, and press enter, it doesnt pause to let me see the Hello %s!", name the window just closes. ive tried using the void main thing but it wont work on my compiler for some reason. can anyone tell me why this isnt working?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Please spend some time in the FAQ.
    Last edited by Dave_Sinkula; 02-01-2006 at 10:58 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    they pause the program until i press enter. i try to use void main with the program and take out the getch()s but then i cant see the program at all, it just comes up for like a second then goes off..i cant enter anything in. so i usually add getch()s to pause my program(and they usually work) but this one isnt working for some reason.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    India
    Posts
    14
    You can this programme as under:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main()
    {
        char name[10];
        printf("Please enter your name: ");
        scanf("%s", name);
        printf("Hello %s!", name);
        getch();
    }
    Anu

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    ehh....its still doing the same thing : \

  6. #6
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    you haven't read the FAQ?

    go to Start > Run and type cmd.exe. then enter your exe's path in command prompt (surround path with double quotes if there are any spaces).

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > #include <conio.h>
    Get rid of this include, you don't need it.

    > getch();
    > scanf("%s", name);
    Never mix input methods, especially standard with non-standard ones.
    If you need to get a single char, use getchar().
    But as others have said, see the FAQ first.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    36
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char name[10];
    
        printf("Please enter your name.\n");
        scanf("%s", name);
        printf("Hello %s!\n", name);
        printf("Press any key to continue\n");
        while (getchar() != '\n')
        getchar()
        return 0;
    }
    This should make the program do what you want. Dont forget main has to return a value.

  9. #9
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by jlharrison
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char name[10];
    
        printf("Please enter your name.\n");
        scanf("%s", name);
        printf("Hello %s!\n", name);
        printf("Press any key to continue\n");
        while (getchar() != '\n')
        getchar()
        return 0;
    }
    This should make the program do what you want. Dont forget main has to return a value.
    You have a missing semicolon, but further, the
    Code:
    while (getchar() != '\n')
    {
        getchar();
    }
    (reformatted for your reading pleasure) both doesn't do what the prompt ("Press any key to continue") says it should (it only exits on return), it also reads two characters each time through, so if the user types "<space><cr>", it will not recognize that as <cr>.

    I'm not saying that I think that the use of getchar() is good in general, but if you're going to use it, use it correctly.
    Insert obnoxious but pithy remark here

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    36
    You absolutly right. I am a noob and should have kept my mouth shut!

  11. #11
    old man
    Join Date
    Dec 2005
    Posts
    90
    Quote Originally Posted by jlharrison
    You absolutly right. I am a noob and should have kept my mouth shut!
    Not at all. This is a place to learn, and you certainly don't have to be perfect before you post. Just learn from your mistakes and soldier on

  12. #12
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by jlharrison
    You absolutly right. I am a noob and should have kept my mouth shut!
    As eerok said, you should not silence yourself just because you're not an expert yet. I had not intended to impune you as a person -- you'll only learn by doing, and if you're afraid to make a mistake, you learn very slowly.

    I'm sorry if the tone of my post seemed overly critical.
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. threaded program doesn't work?
    By OcTO_VIII in forum Linux Programming
    Replies: 1
    Last Post: 12-11-2003, 12:37 PM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  5. how does this program work
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2002, 01:48 PM