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

  • Context: MHB 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Loop
Click For Summary
SUMMARY

The forum discussion focuses on creating a bidding program in C++ that continues until the user inputs 'n'. The sample program provided uses a while loop to repeatedly generate and display bids, but the user input variable is incorrectly referenced. The correct approach is to check the variable keepGoing instead of nextBid to determine when to stop the loop. The solution emphasizes the need for proper variable usage in control flow statements.

PREREQUISITES
  • Basic understanding of C++ programming
  • Familiarity with control flow structures, specifically while loops
  • Knowledge of random number generation in C++ using srand and rand
  • Experience with user input handling in C++ using cin
NEXT STEPS
  • Review C++ while loop syntax and best practices
  • Explore random number generation techniques in C++
  • Learn about input validation methods in C++ to handle user input more effectively
  • Investigate debugging techniques for identifying variable scope issues in C++
USEFUL FOR

C++ programmers, students learning control flow in programming, and developers looking to improve their understanding of user input handling and random number generation in C++ applications.

ineedhelpnow
Messages
649
Reaction score
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
You are checking the wrong variable...it is [m]keepGoing[/m] that stores the user input...:D
 

Similar threads

Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K