Program Loops Forver! HELP!

This is a discussion on Program Loops Forver! HELP! within the C Programming forums, part of the General Programming Boards category; hi guys need some help, my program seems to loop forever when any non-integer value is entered in an integer ...

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

    Program Loops Forver! HELP!

    hi guys need some help, my program seems to loop forever when any non-integer value is entered in an integer input

    heres my code

    Code:
    /********************************
    *                               *
    *	Grade Calculator	*
    *                               *
    *********************************
    *                               *
    *      	James Daniel Archer	*
    *                               *
    *	15th December 2004      *
    *                               *
    *********************************
    *                               *
    *Input number of students, input*
    *each students mark, calculate  *
    *each students grade, output    *
    *number of students, the average*
    *grade for all students, & the  *
    *number of students achieving   *
    *each grade.                    *
    *                               *
    *********************************
    *                               *
    *Running the program:-          *
    *   -Double click on the EXE    *
    *    file to run the program.   *
    *                               *
    *********************************
    *                               *
    *References:-                   *
    *   -N/A                        *
    *                               *
    *********************************
    *                               *
    *Restrictions:-                 *
    *   -The program will not output*
    *    a list of all students and *
    *    their achieving grade.     *
    *                               *
    *********************************
    *				*
    *Error handling:-		*
    *   -The program will ask the   *
    *    user to re-input data if an*
    *    error is detected. The     *
    *    system will continue to ask*
    *    the user to re-input the   *
    *    data until an acceptable   *
    *    value is entered.          *
    *				*
    *********************************
    *				*
    *See structure chart for more   *
    *information.                   *
    *				*
    ********************************/
    
    
    /* Header Files */
    
    #include <stdio.h>
    #include <dos.h>
    
    /* Functions */
    
    void initialise(void);
    void input(void);
    void process(void);
    void output(void);
    
    /* Functions NOT Included On Stucture Chart
       These Functions Are Used For Program Presentation */
    
    void intro(void);
    void clear(void);
    void load(void);
    
    /* Global Variables */
    
    int students, mark, count, failcount, passcount, meritcount, distinctioncount, total;
    float average;
    char fname[16], sname[16];
    
    main()
    {	/* Program Start */
       system("cls");	/* Clear Screen To Remove Data On Screen From Previous Programs */
       initialise();	/* Function Calls */
       intro();
       input();
       load();
       clear();
       process();
       load();
       clear();
       output();
       clear();
    }	/* Program End */
    
    void initialise(void)
    {	/* Initialise Function Start */
       students=0;		/* Global Numeric Variables Initialised To 0 */
       mark=0;
       count=0;
       failcount=0;
       passcount=0;
       meritcount=0;
       distinctioncount=0;
       average=0;
    }	/* Initialise Function End */
    
    void input(void)
    {	/* Input Function Start */
       printf("Please Input The Number of Students You Wish To Grade");
       printf(" \n");
       printf(" \n");
       scanf("%d",&students);	/* Data Input 1 (Number of Students) */
       printf(" \n");
       printf(" \n");
       while((students <1) || (students >60))	/* Validation Loop 1 (Number of Students Within Rage 1 - 60) */
       {	/* Validation Loop 1 Start */
          printf("Error 001 - Number Out of Range! \a");	/* Error Message With System Beep */
          printf(" \n");
          printf(" \n");
          printf("Please Input The Number of Students You Wish To Grade");
          printf(" \n");
          printf(" \n");
          scanf("%d",&students);	/* Data Input 1 (Number of Students) */
          printf(" \n");
          printf(" \n");
       }	/* Validation Loop 2 End */
    }	/* Input Function End */
    
    void process(void)
    {	/* Process Function Start */
       for(count=1; count<=students; count++)	/* Loop 1 (Repeat The Following Code For Each Student) */
       {	/* Loop 1 Start */
          printf("Please Input Students First Name");
          printf(" \n");
          printf(" \n");
          scanf("%s",fname);	/* Data Input 2 (Students First Name) */
          printf(" \n");
          printf(" \n");
          printf("Please Input Students Surname");
          printf(" \n");
          printf(" \n");
          scanf("%s",sname);	/* Data Input 3 (Students Surname) */
          printf(" \n");
          printf(" \n");
          printf("Please Input Students Mark");
          printf(" \n");
          printf(" \n");
          scanf("%d",&mark);	/* Data Input 4 (Students Mark) */
          printf(" \n");
          printf(" \n");
          while((mark <0)||(mark >100))	/* Validation Loop 2 (Students Mark Within Range 0 - 100) */
          { /* Validation Loop 2 Start */
    	 printf("Error 001 - Number Out of Range! \a");	/* Error Message With System Beep */
    	 printf(" \n");
    	 printf(" \n");
    	 printf("Please Input Students Mark");
    	 printf(" \n");
    	 printf(" \n");
    	 scanf("%d",&mark);	/* Data Input 4 (Students Mark) */
    	 printf(" \n");
    	 printf(" \n");
          }	/* Validation Loop 2 End */
          printf("Clearing Screen, One Moment Please...");
          sleep(1);	/* Program Pause (1 Second) */
          system("cls");	/* Clear Screen */
          printf("Students Results");
          printf(" \n");
          printf(" \n");
          printf("%s",fname);	/* Data Output 1 (Students First Name) */
          printf(" %s",sname);      /* Data Output 2 (Students Surname) */
          printf(" \n");
          printf(" \n");
          if(mark<40)	/* IF Statement 1 (Fail Grade) */
          { /* IF Statement 1 Start */
    	 printf("GRADE: FAIL");
    	 failcount++;	/* Increment The Fail Count */
          }	/* IF Statement 1 End */
          else
    	 if(mark<65)	/* IF Statement 2 (Pass Grade) */
    	 {	/* IF Statement 2 Start */
    	    printf("GRADE: PASS");
    	    passcount++;	/* Increment The Pass Count */
    	 }	/* IF Statement 2 End */
    	 else
    	    if(mark<75)	/* IF Statement 3 (Merit Grade) */
    	    {	/* IF Statement 3 Start */
    	       printf("GRADE: MERIT");
    	       meritcount++;	/* Increment The Merit Count */
    	    }	/* IF Statement 3 End */
    	    else	/* Final Else (Distinction Grade) */
    	    {	/* Final Else Start */
    	       printf("GRADE: DISTINCTION");
    	       distinctioncount++;	/* Increment The Distinction Count */
    	    }	/* Final Else End */
          total=total+mark;	/* Calculation To Determine The Total Value of All Marks */
          printf(" \n");
          printf(" \n");
          printf("Press Any Key To Continue");
          getch();	/* Wait For User To Press A Key */
          system("cls");
       }	/* Loop 1 End */
    }	/* Process Function End */
    
    void output(void)
    {	/* Output Function Start */
       average=total/students;	/* Calculation To Determine The Average of All Marks */
       printf("Number of Students:	%d",students);	/* Data Output 3 (Number of Students) */
       printf(" \n");
       printf(" \n");
       printf("Average Mark:	   	   %.2f",average);	/* Data Output 4 (Average Grade) */
       printf(" \n");
       printf(" \n");
       printf("Number of Students Achieving The Following Grades");
       printf(" \n");
       printf(" \n");
       printf("Fail:		%d",failcount);	/* Data Output 5 (Students Achieving Fail) */
       printf(" \n");
       printf("Pass:		%d",passcount);	/* Data Output 6 (Students Achieving Pass) */
       printf(" \n");
       printf("Merit:		%d",meritcount);	/* Data Output 7 (Students Achieving Merit) */
       printf(" \n");
       printf("Distinction:	%d",distinctioncount);	/* Data Output 8 (Students Achieving Distinction) */
       printf(" \n");
       printf(" \n");
       printf(" \n");
       printf("Press Any Key To Exit");
       getch();	/* Wait For User To Press A Key */
    }	/* Output Function End */
    
    void intro(void)
    {	/* Intro Function Start */
       printf("Archer Software Presents\n");
       printf("Grade Calculator\n");
       printf(" \n");
       printf("One Moment Please...");
       sleep(2);	/* Program Pause (2 Seconds) */
       printf(" \n");
       printf(" \n");
    }	/* Intro Function End */
    
    void clear(void)
    {	/* Clear Function Start */
       printf(" \n");
       printf(" \n");
       printf("Clearing Screen, One Moment Please...");
       sleep(1);	/* Program Pause (1 Second) */
       system("cls");
    }	/* Clear Function End */
    
    void load(void)
    {	/* Load Function Start */
       printf("Loading.");
       sleep(1);	/* Program Pause (1 Second) */
       printf(".");
       sleep(1);	/* Program Pause (1 Second) */
       printf(".");
       sleep(1);	/* Program Pause (1 Second) */
       printf(".");
       printf(" \n");
       printf(" \n");
    }	/* Load Function End */
    thnx

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Do someting like:

    Example
    Code:
    char line[256];
    
    do {
      fgets(line, sizeof(line), stdin);
    } while(sscanf(line, "%d", &students) != 1);
    Next time cut your code down a bit for us bud. Oh and the above could use improvements such as telling the user that their input was invalid.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Also, I would refactor that code, its hard/annoying to read.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    By the way, just to clarify part of my code, remember that the scanf() functions return how many requested items were successfully found. In the case of the string "%d" you are looking for a single integer. Which is why the do while loop ends as soon as only 1 item is found by sscanf().

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by Hulag
    Also, I would refactor that code, its hard/annoying to read.
    i agree...i would do something like this

    char formatString = "Grade: %d\n\nName: %s\n\n etc. ";
    printf(formatString, grade, name, average, etc...);
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    I didn't read your code but i noticed you using waaay too many 'printf's in it. You don't need to use a seperate 'printf' function for every new line. You can skip three , four or as many lines as you want in one printf like this :
    Code:
    printf("\n\n\n\n\n\n\n\n");
    using too many 'printf' functions makes the code longer and a bit annoying.

    You can shorten this part for example :
    Code:
    printf("Error 001 - Number Out of Range! \a");
    	 printf(" \n");
    	 printf(" \n");
    	 printf("Please Input Students Mark");
    	 printf(" \n");
    	 printf(" \n");
    this way :
    Code:
    printf("Error 001 - Number Out of Range! \a\n\nPlease input students mark\n\n");
    Or another way , you can use one printf on multiple lines if you still insist on writing every \n on a seperate line :
    Code:
    printf("Error 001 - Number Out of Range! \a"
             "\n"
             "\n"
             "Please input students mark"
             "\n"
             "\n");
    but it would still be confusing for whoever attempts to read your code.


    Quote Originally Posted by misplaced
    char formatString [] = "Grade: %d\n\nName: %s\n\n etc. ";
    printf(formatString, grade, name, average, etc...);
    you forgot the braces


    *EDIT*

    Quote Originally Posted by master5001
    remember that the scanf() functions return how many requested items were successfully found.
    Im afraid it returns the number of fields that has been successfully stored.


    glossopjames :
    read this FAQ entry to see how you can restrict the user's input.
    Last edited by Brain Cell; 12-16-2004 at 08:11 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  7. #7
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Code:
    void intro(void)
    {	/* Intro Function Start */
       printf("Archer Software Presents\n");
       printf("Grade Calculator\n");
       printf(" \n");
       printf("One Moment Please...");
       sleep(2);	/* Program Pause (2 Seconds) */
       printf(" \n");
       printf(" \n");
    }	/* Intro Function End */
    
    void clear(void)
    {	/* Clear Function Start */
       printf(" \n");
       printf(" \n");
       printf("Clearing Screen, One Moment Please...");
       sleep(1);	/* Program Pause (1 Second) */
       system("cls");
    }	/* Clear Function End */
    
    void load(void)
    {	/* Load Function Start */
       printf("Loading.");
       sleep(1);	/* Program Pause (1 Second) */
       printf(".");
       sleep(1);	/* Program Pause (1 Second) */
       printf(".");
       sleep(1);	/* Program Pause (1 Second) */
       printf(".");
       printf(" \n");
       printf(" \n");
    }	/* Load Function End */
    Whats the point of forcing the user to wait when you can let your program run much faster? remove those 'sleep()' function , they are useless. And letting the user wait for the screen to be "cleared" isn't a good idea.

    Besides , Sleep(1) should be Sleep(1000) since it deals with Milliseconds. I'm not sure if this is compiler/system specific but thats how sleep work as far as i know .


    *EDIT*
    Code:
    main()
    {	/* Program Start */
       system("cls");	/* Clear Screen To Remove Data On Screen From Previous Programs */
       initialise();	/* Function Calls */
       intro();
       input();
       load();
       clear();
       process();
       load();
       clear();
       output();
       clear();
    }	/* Program End */
    Function 'main' should return an 'int'. You should use :
    Code:
    int main(void)
    {
     /* code here */
    
    return 0;
    }
    'return 0' tells the system that the program has terminated successfuly. And get rid of the load() and clear() functions since they only do one simple task that can be done in 1 line in function main.


    - You are using 'getch()' delcared in conio.h wich isn't a standard header file therefore your code won't work in many compilers. I suggest replacing it with 'getchar()' although it requires hitting enter.

    And finally your function here :

    Code:
    void initialise(void)
    {	/* Initialise Function Start */
       students=0;		/* Global Numeric Variables Initialised To 0 */
       mark=0;
       count=0;
       failcount=0;
       passcount=0;
       meritcount=0;
       distinctioncount=0;
       average=0;
    }	/* Initialise Function End */
    It's useless , too. Why don't you initialize your variables once you declare them? like this :
    Code:
    int students=0, mark=0, count=0, failcount=0, passcount=0, meritcount=0, distinctioncount=0, total=0;
    What you did was time consuming and lets your code go even bigger.


    Your program could've been much shorter .
    Last edited by Brain Cell; 12-16-2004 at 08:47 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

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, 11:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 08:51 AM
  4. simple program on loops
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-11-2002, 11:24 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21