Thread: Need help with a loop!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    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...

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

  3. #3
    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;

  4. #4
    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?

  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
    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.

  6. #6
    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');

  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
    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.

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