Thread: Help on while loop

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    10

    Help on while loop

    Hi so I'm new to programming and learning C programming. Im currently working on a while loop to print statements based on an input from the user. I got the input but stuck on an infinite loop of the printed material. How can i limit the loop to the number that was acquired from the user?

    Code:
    
    
    
    
    #define_CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    
    
    
    
    int main() {
    	int Input;
    	printf("Type in a number from 1-20\n");
    	scanf(" %d", &Input);
    
    	while (Input) {
    
    
    		printf("\nGo Spartans\n");
    		printf("Time to start winning\n"); 
    
    	}
    
    
    return 0;
    
    
    }
    

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Subract 1 each time through the loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    look at what are you using for your condition to keep that loop running. -- try going against it until it matches using, "while not what I want, until it reaches what I want" logic.

    hint on what you're missing:
    in order to go against something what condition needs to be met?

    you can use Salem's suggestion of Subtract 1 each time through, or add 1 each time through, depending how you apply the logic.
    Code:
    userx@slackwhere:~/bin
    $ ./a.out
    Type in a number from 1-20
    3
    
    Go Spartans
    Time to start winning
    
    Go Spartans
    Time to start winning
    
    Go Spartans
    Time to start winning
    Last edited by userxbw; 10-14-2017 at 12:33 PM.

  4. #4
    Registered User
    Join Date
    Oct 2017
    Posts
    10
    so would i apply the addition or subtraction to the condition?

    Code:
    while (Input-1){
    }
    Or how would I apply the logic in terms of the code?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Normally the condition is a Boolean expression like Input > 0 and you would do something to Input between the curly braces.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    You have to change Input so it gets down to zero.

    Input = Input - 1
    or
    Input -= 1
    or
    Input--

    Take your pick.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2017
    Posts
    1
    Quote Originally Posted by Salem View Post
    You have to change Input so it gets down to zero.

    Input = Input - 1
    or
    Input -= 1
    or
    Input--

    Take your pick.
    Code:
    #include<stdio.h>
    
    
    
    
    int main() {
            int Input;
            printf("Type in a number from 1-20\n");
            scanf(" %d", &Input);
    
    
            while (Input >= 1) {
                    Input--;
                    printf("\nGo Spartans\n");
                    printf("Time to start winning\n");
            }
    
    
    
    
    return 0;
    
    
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2016, 09:08 AM
  2. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  3. 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
  4. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM

Tags for this Thread