Continuously Bid with While Loop - Stop at 'n' | Sample Program

  • MHB
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    Loop
In summary, a while loop is a programming construct used to repeatedly execute code as long as a specified condition is true. It works by first checking the condition, executing the code, and then checking the condition again until it becomes false. The main difference between a while loop and a for loop is that a while loop continues until the condition is false, while a for loop has a predetermined number of iterations. To prevent an infinite loop, a counter variable or a break statement can be used. While loops are useful for tasks such as reading user input, iterating through data structures, and implementing game logic.
  • #1
ineedhelpnow
651
0
Write an expression that continues to bid until the user enters 'n'.

Sample program:
Code:
#include <iostream>
using namespace std;

int main() {
   char keepGoing = '-';
   int nextBid = 0;

   srand(5);
   while (<STUDENT CODE>) {
      nextBid = nextBid + (rand()%10 + 1);
      cout << "I'll bid $" << nextBid << "!" << endl;
      cout << "Continue bidding? ";
      cin >> keepGoing;
   }
   cout << endl;

   return 0;
}

help me (Headbang) i tried nextBid<'n' nextBid != 'n' !(nextBid='n'). nothing works.
 
Technology news on Phys.org
  • #2
You are checking the wrong variable...it is [m]keepGoing[/m] that stores the user input...:D
 

1. What is a while loop?

A while loop is a programming construct that allows you to repeatedly execute a block of code as long as a specified condition is true. It is used to automate tasks and make programs more efficient.

2. How does a while loop work?

A while loop first checks the specified condition. If the condition is true, the code inside the loop is executed. After the code is executed, the condition is checked again. This process repeats until the condition becomes false, at which point the loop terminates and the program continues to the next line of code.

3. What is the difference between a while loop and a for loop?

A while loop and a for loop are both programming constructs used to repeat code. The main difference is that a while loop continues to execute the code as long as the condition is true, while a for loop has a predetermined number of iterations and will terminate once that number is reached.

4. How do you prevent an infinite loop in a while loop?

An infinite loop occurs when the condition in a while loop is always true, causing the loop to never terminate. To prevent this, you can use a counter variable or a break statement to ensure that the loop eventually ends.

5. When should I use a while loop?

A while loop is useful when you need to repeat a block of code until a specific condition is met. It is commonly used for tasks such as reading user input, iterating through data structures, and implementing game logic.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
15
Views
2K
Replies
10
Views
961
  • Programming and Computer Science
Replies
2
Views
881
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
3
Views
734
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top