Thread: Need Help... why do some programs close suddenly after entering character ?

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    3

    Need Help... why do some programs close suddenly after entering character ?

    I try to learn C programming but I have a question about it.

    Can you explain why do some programs close after entering number or characters suddenly altough I use conio.h and getch();. For example, I wrote some codes to develop my programming skills. after compiling with Dev C++, my program start but after writing a number between 0-8 it close suddenly.Why? Have I made any mistakes while I was writing codes? but I faced some problems like this when I compiled other codes. What is the general problems?

    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int sayi;
    printf("Please write a number:\n");
    scanf("%d",&sayi);
    switch(sayi){
    case 1: printf("Monday\n");
    break;
    case 2: printf("Tuesday\n");
    break;
    case 3: printf("Wednesday\n");
    break;
    case 4: printf("Thursday\n");
    break;
    case 5: printf("Friday\n");
    break;
    case 6: printf("Saturday\n");
    break;
    case 7: printf("Sunday\n");
    break;
    default: printf("No day\n"); 
    getch();
    }
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    your getch() call is inside your default case. It doesn't get executed for numbers one through 7. Also, if you want it to repeat, you are going to have to include a while loop over the switch statement, something like:

    while (sayi)
    {
    ....
    }

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Yes, put your getch on the last line before you exit the program.

    main()
    {
    Also, your main should have a return type, int main()

    Code:
    int main()
    {
    ...
    getch();
    return 0;
    }
    Spidey out!

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Dont forget to put code tags as well !!!!! It makes reading a lot easier and clean as well.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    3
    Thank you roaan, LBriggs, Spidey I solve problems

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    3
    KBriggs

    How Can I use While Loop in my codes? Can you explain?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. When programs close with no warning
    By Gerread in forum Windows Programming
    Replies: 3
    Last Post: 03-26-2007, 12:55 AM
  2. open() and close()
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 04-08-2005, 01:16 PM
  3. problem to close file and get 1 character of...
    By collinm in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 11:39 AM
  4. My programs close
    By Granger9 in forum C Programming
    Replies: 17
    Last Post: 08-14-2002, 07:02 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM