Thread: Confused about Loops (Beginner Question)

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    5

    Confused about Loops (Beginner Question)

    I believe, I need two loops for this..but I just can't make it out no matter how hard I try. So I guess I need help!


    Here is the code...:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int counterH = 0;
        int counterN = 0;
        int mark;
        int confirmExit;
    
    
        printf("Please input student mark: ");
        scanf("%d", &mark );
    
    
           while ( mark <= 100 )
            {
                if ( mark >= 85 )
                    {
                    puts ("H");
                    counterH++;
                    }
    
    
                else if ( mark >= 75 )
                    puts ("D");
    
    
                else if ( mark >= 65 )
                    puts ("P");
    
    
                else if ( mark >= 50 )
                    puts ("C");
    
    
                else if ( mark >=  0 )
                    {
                    puts ("N");
                    counterN++;
                    }
                
                printf("Please input student mark: ");
                scanf("%d", &mark );
            }
    }/* End Main*/


    So the problem here, is that I don't know how to insert the other condition which is to exit if the user wants to.


    Here's the full question:


    1) get one student’s mark (keep asking for input if the input is more than 100)
    2) decide and display which grade the mark is in ( make a series of if / else decisions)
    3) add 1 (one) to the corresponding counter for grade HD and grade N
    4) ask if this is the last student
    5) if not the last student, return to the start to get the next student’s mark


    I got (1),(2) and (3) right. I don't know how to deal with (4) and (5) =(


    I appreciate all the help!


    Regards,
    Sam

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What about putting a BIG
    Code:
    do {
    //all your other code goes in here
    }while(confirmExit==0);
    around all the other code you have? This is for #4.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Do you know how to use functions? the code would be easier to split into parts relevant to different tasks

    Code:
    int answer;
    do
    {
       int mark = getOneStudentMark(); /* do 1) and 2) inside that funtion */
    
       /* do 3) here */
    
      answer = IsThisLastStudent() /* do 4) inside this function and return 1 is yes and 0 is no */
    } while (answer);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. System programming - Absolute beginner confused
    By std10093 in forum C Programming
    Replies: 21
    Last Post: 11-24-2012, 03:21 PM
  2. C++ help needed - beginner and so confused
    By staceyktaylor in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2010, 11:28 PM
  3. beginner help loops
    By aadil7 in forum C Programming
    Replies: 9
    Last Post: 03-15-2010, 01:41 AM
  4. Confused Beginner - hangman game
    By goodfornothing in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2009, 11:06 PM
  5. :confused: Using loops..
    By snapshooter in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 09:48 AM