Description
The purpose of this challenge is to use the WHILE loop to control program flow. This challenge will use the WHILE loop in various ways.
Requirements
Write all your code for the following steps in one file as one program. This program doesn’t really do anything except illustrates various ways to use a while loop. Each of the items below are independent of each other – they are not steps to accomplishing one program.
- Ask the user to enter a single string. Ask the user to enter an integer value for a variable called repetitions. Use a while loop to display this string (of your choice) repeated repetitions times, one string per line. Hint: declare an integer called counter, initialized to zero.
// the goal is to count, one at a time, until we get to repetitions // because that's how many times we need to display the text // when counting in programming, zero is usually a // good place to start int counter = 0; string text; int repetitions; // ask the user to enter to enter a value for text // this is the word that gets displayed repeatedly code goes here // ask the user to enter a enter a value for repetitions // this is the number of times that we will display text code goes here // now, write a while loop that counts, using counter to keep // track. The loop will continue until we have repeated // as many times as repetitions // Think about walking 20 steps in one direction // after each step, you say coffee. You stop when you've // walked 20 steps. Except, the user will tell you how many // steps to take while (what condition goes here that means keep going?) { // display text on a line by itself code goes here // update counter to the next value // Remember, it's a simple counter code goes here }
- Use a while loop to count and display all the integers from 1 to 20. Separate numbers with a space
// this one is set up very similarly to #1 above. // Except, instead of asking the user to enter the value // you're trying to get to, you already know your goal is to // get to 20 // Just like the walking steps analogy. You're supposed to // take 20 steps, one at a time. Instead of saying coffee, you say // how many steps you've taken after each step // You don't have to ask the user to enter any text // that will be displayed. Instead, simply display the number // of steps you've taken // You can either re-use some of the variables you declared in #1 // If you choose to reuse them, make sure to reset their starting // values. counter = 1; // this is resetting the value // of counter declared previously // No need to create a new variable while (what condition goes here that means keep going?) { // display counter. Don't display on separate lines // display the value using a space to separate // successive values code goes here // update counter to the next value // Remember, it's a simple counter code goes here } // this forces the continuation of the next output to // go the next line cout << endl;
- Ask the user to enter a number larger than 100. Then, use a while loop to display all numbers divisible by 10 starting from the number entered by the user all the way down to 0. Only show numbers divisible by 10. Separate numbers with a tab. TAB character is displayed using the \t escape sequence. (Remember that \n is a newline; use similarly).
int number; // ask the user to enter a number. Store user's input in number // the goal is to count down from the user's number // all the way down to 0 while (what condition goes here that means keep going?) { // display the current number here // BUT only display the number if it is divisible // by 10 if (what condition means current value is divisible?) { // display the number code goes here } // Don't forget to update the value that updates // your while's boolean expression // Remember that you are counting down code goes here }
- Use a while loop to keep asking the user for an integer as long as positive numbers are being entered. For every positive number entered, show its value multiplied by 2. Once a negative number is entered stop asking and quit the loop
int input; // ask the user to enter a value code goes here while (what condition here means keep going?) { // show the calculated value // show the arithmetic expression as well // as the result of doubling the user's input // see sample interaction below code goes here // ask the user for another value for input // it is this value from the user that can // affect the behavior of the while loop // if the user enters a negative here, the // while's boolean expression will turn false code goes here }
- Use a while loop to keep asking for a string until a particular string is entered (for example, “bye”). Hint: declare at least two string variables.
// declare string variables string exit_word = "bye"; // use any word you want string user_word; // ask the user to enter a word code goes here while (what condition means the user hasn't typed the exit word?) { // ask the user to enter a word // it is this word that will determine if // the while loop keeps going or stops code goes here }
Do Not Use
You must use the WHILE statement. You may not use any other looping mechanism.
Sample Interaction / Output
Enter a string to be repeated: coffee How many times to show? 5 coffee coffee coffee coffee coffee 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Enter a number: 119 110 100 90 80 70 60 50 40 30 20 10 Enter a number: 3 3 x 2 = 6 Enter a number: 7 7 x 2 = 14 Enter a number: -2 Enter a string: yo Enter a string: hi Enter a string: hola Enter a string: sup Enter a string: bye
LEGEND
PROGRAM OUTPUT
USER ENTRY
FROM INPUT
CATALOG ID: CPP-LOOP0001-GUIDED
Print Requirements