Thread: Need help with a loop!

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    7

    Need help with a loop!

    I wrote this code and it all works great, but as you'll notice at the end, I need to prompt the user to enter Y or N. If they enter N, then the program ends, I can do that. But if the user enter Y, the program need to ask for the info to be typed in again. How can I do this?

    I'm not asking you guys to write it for me, just help me get started with that.

    Code:
    #include <stdio.h>
    
    char first, last, GetLetterGrade, x;
    float test_one, test_two, test_three;
    float test_one_score, test_two_score, test_three_score;
    float averageGrade;
    
    int main()
    {
        //Get the maximum scores.
        printf("Please enter the maximum score possible for test one: ");
        scanf("%f", &test_one);
    
        printf("Please enter the maximum score possible for test two: ");
        scanf("%f", &test_two);
    
        printf("Please enter the maximum score possible for test three: ");
        scanf("%f", &test_three);
    
    
        //Get students name.
        printf("\nEnter your first and last name initials: ");
        scanf(" %c%c", &first, &last);
    
    
        //Get student test grades  Checks for proper input.
        printf("\n\nEnter your test one score: ");
        scanf("%f", &test_one_score);
        if(test_one_score < 0 || test_one_score > test_one){
            printf("\nInvalid score, the valid range is : 0-%.2f\nEnter correct score:", test_one);
            scanf("%f", &test_one_score);
        }
    
        printf("Enter your test two score: ");
        scanf("%f", &test_two_score);
        if(test_one_score < 0 || test_two_score > test_two){
            printf("\nInvalid score the valid range is : 0-%.2f\nEnter correct score:", test_two);
            scanf("%f", &test_two_score);
        }
    
        printf("Enter your test three score: ");
        scanf("%f", &test_three_score);
        if(test_one_score < 0 || test_three_score > test_three){
            printf("\nInvalid score, the valid range is : 0-%.2f\nEnter correct score:", test_three);
            scanf("%f", &test_three_score);
        }
    
    
    
        //Calculate average grade.
        averageGrade = (test_one_score + test_two_score + test_three_score)/(test_one + test_two + test_three) * 100;
    
    
        //Output student's info.
        printf("\nStudent's initials: %c%c", first, last);
        printf("\nAverage Score: %.2f", averageGrade);
    
        //Output letter grade.
        if(averageGrade >= 90){
            printf("\nLetter grade: A");
        }
        else if((averageGrade >= 80) && (averageGrade < 90)){
            printf("\nLetter grade: B");
        }
        else if((averageGrade >= 70) && (averageGrade < 80)){
            printf("\nLetter grade: C");
        }
        else if((averageGrade >= 60) && (averageGrade < 70)){
            printf("\nLetter grade: D");
        }
        else if(averageGrade < 60){
            printf("\nLetter grade: F");
        }
    
    
        //Another student.
        printf("\n\nCalculate another student grade (Enter Y for Yes, N for No): ");
        scanf(" %c", &x);
    
    
    
    
        return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    You enclose the part you want to repeat inside a loop and set the loop to check if the user has inputted 'Y'.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    7
    What kind of a loop should I use?

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by antonio.gor View Post
    What kind of a loop should I use?
    A do while loop is generally what is used for this sort of thing. It has the pseudo-code syntax of:

    Code:
        do {
            input = Ask_user_for_input();
        } while (input == invalid_value);
    The do while construct is a bit more useful than a while one in this context, because you don't have to set "input" to be invalid before entering the loop. What happens is that the code "input = Ask_user_for_input()" runs and sets the input variable before the condition of the loop is checked. Otherwise you would need to use something like this:

    Code:
        input = invalid_value; // Set it to be invalid so the loop will begin
    
        while (input == invalid_value) {
            input = Ask_user_for_input();
        }
    You can also set the loop to print information prompting the user that input is expected (the loop will take care of printing it for every invalid value the user enters). Hope this helps, good luck!
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by antonio.gor View Post
    What kind of a loop should I use?
    Here is a novel idea, why don't you read about loops in C and then tell us?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Mar 2015
    Posts
    7
    Thanks, that helped clear some things up for me.

    I ended up using an if else statement:
    Code:
    printf("\n\nCalculate another student grade (Enter Y for Yes, N for No): ");
    scanf(" %c", &x);
    if( x == 'N' || x == 'n'){
       return 0;
    }
    else if(x == 'Y' || x == 'y'){
       return main();
    }
    When they say no, the program ends, but when they say yes, the whole program starts again. Can you suggest how I can make it so that instead of starting the whole program again, it will just go back to a certain part?

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by antonio.gor View Post
    Thanks, that helped clear some things up for me.

    I ended up using an if else statement:
    Code:
    printf("\n\nCalculate another student grade (Enter Y for Yes, N for No): ");
    scanf(" %c", &x);
    if( x == 'N' || x == 'n'){
       return 0;
    }
    else if(x == 'Y' || x == 'y'){
       return main();
    }
    When they say no, the program ends, but when they say yes, the whole program starts again. Can you suggest how I can make it so that instead of starting the whole program again, it will just go back to a certain part?
    Yes, how about you read the link I posted about loops and then implement one. Do not call main recursively, this is a very bad programming practice.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User Sam Jackson's Avatar
    Join Date
    Jan 2015
    Posts
    5
    Try this:
    Code:
    int c;    
    int i = 0;
    // You may need this.
        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), 0); 
    
        
    do{
            printf("Now processing number %u\n", ++i);
            
    //////////////////
            
    // Your code here.
            
    /////////////////////
            puts("Another? y = yes, n = no");
            c = getchar();
        }
    while(toupper(c) != 'N');

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    Sam Jackson: note that SetConsoleMode, GetStdHandle and STD_INPUT_HANDLE are non-standard.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  2. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  3. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM