Thread: Limit input to 10 numbers

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    3

    Limit input to 10 numbers

    Hello happy people! Can use some help here I am new to programming.
    This code is supposed to accept only ten inputs but it keeps going on forever I don't know what I am doing wrong

    Code:
    #include <stdio.h>
    #include <conio.h>
    int main( void)
    {
        int counter;
        int grade;
        int total;
        int possible;
        int total2;
        int percent;
        
        float average;
        float average2;
        
        total = 0;
        total2 = 0;
        counter = 0;
        
        printf( "Enter grade: ");
        scanf( "%d", &grade );
        printf("Enter total points possible: ");
        scanf("%d", &possible);
        
        while (grade != -1) {
              total = total + grade;
              total2 = total2 + possible;
              counter = counter + 1;
              
              
              printf( "Enter grade: ");
              scanf("%d", &grade );
              
              printf( "Enter total points possible: ");
              scanf( "%d", &possible );
        }
        if (counter <=10 ) {
            average = ( float ) total/counter;
            average2 = (float ) total2/counter;
            
           
            percent = (average/average2)*100;
    }
    
    
     if (percent >= 90) {                  
          printf("The student has  %d perccent Therefore the student has an A ", percent);
             }
             else if (percent >=80){
                  printf("The student has %d percent Therefore the student has a B", percent);}
             else if (percent>=70){
                  printf("The student has %d percent Therefore the student has a C", percent);}
             else if (percent>=60){
                  printf("The student has %d percent Therefore the student has a D", percent);} 
             else if (percent <59){
                  printf("The student has %d percent Therefore the student has an F", percent);}             
           getch();  
          return 0;
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    It accepts inputs until the user enters in a -1 for test grade. Look at the while loop condition

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    3
    How do I change that to end after 10 inputs?

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    What needs to change in order for the loop to go 10 times? Hint: You can use the counter variable in the while loop condition.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    3
    I tried this
    while (counter <=10) { total = total + grade;
    total2 = total2 + possible;
    counter = counter + 1;

    but it don't stop at 10 it keeps asking for more numbers

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Post your new code, you are probably missing something in the while loop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input Numbers
    By bummielove in forum C Programming
    Replies: 2
    Last Post: 07-23-2011, 07:20 PM
  2. How to limit user input to a certain number of digits?
    By NewbGuy in forum C Programming
    Replies: 7
    Last Post: 05-08-2009, 09:57 PM
  3. time limit for user input
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 03:01 PM
  4. How to force user to input a value in the type limit
    By salvadoravi in forum C Programming
    Replies: 11
    Last Post: 01-21-2008, 09:47 AM
  5. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM