Thread: terminate the program....

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    5

    terminate the program....

    Anyone please help me to find out ...where is the error because this program will continue untill the user enter 0 the program will terminate ...but i stuck at here please help...
    Code:
    #include<stdio.h>
    
    struct student{
    int id_no;
    char name;
    int average;
    
    int main()
    {
    struct student stud1;
    
    do{
    
    printf("Enter Index No : ");
    scanf("%d",&stud1.id_no);
    printf("Enter name : ");
    scanf("%s",&stud1.name);
    printf("Enter average : ");
    scanf("%d",&stud1.average);
    
    printf("\n");
    
    }while(stud1.id_no!=0);
    
    return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It will finish if the user enters zero. There is nothing wrong in your code in that respect.

    However, I think the question you are REALLY asking is "Why is it continuing to ask for name and average" when I enter zero. That is because "while" is not like our natural language while ("Use an umbrella while it's raining" - if it stops raining, a human will stop using the umbrella. In computers, a while-statement (or "do while") is cecked when you reach that line of the code. So in your case, it happens after you've completed the input of ALL the data input.

    The solution to your problem would be to add an if-statement to check if the id is not zero, and only then do the input of name and average.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Need Delimiter Program helpful hints.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 02-16-2002, 06:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM