Thread: Working on a program and am stuck. While loop is not working right.

  1. #1
    Registered User
    Join Date
    Feb 2016
    Location
    Middleburg, Florida
    Posts
    3

    Working on a program and am stuck. While loop is not working right.

    I am working on a program of blackjack for class. I was to input an menu with my code to allow user to play another round or exit. My first option works. my second option displays another round and then exits. This is my first semester of programming so I am still quite new but need direction. I have attached my code, and also what the stepped question was. Thanks you for your help.

    -Create a while loop that has a condition of (menuChoice != -1).
    -Inside the while loop setup a menu that allows for players to choose 2 options:
    a. 1- Play Blackjack
    b. 0- Exit Program
    -Ask for a player to input a choice and store it in (menuChoice != -1).
    -Update your while loop condition to now be (menuChoice !=0).
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Please post your code here, using code tags. You should also be careful not to include personal information in the source code you post.

    First, your specifications seem wrong. You can't change the "while()" loop condition from "menuChoice != -1" to "menuChoice != 0" during execution. I would suggest you describe your requirements more clearly, or seek clarification on them if you're uncertain.

    Second, a quick look at the code:

    Code:
    int menuChoice;
    menuChoice != -1;
    This statement has no effect. If you want to assign a value of -1 to "menuChoice", then do so: int menuChoice = -1;

    Code:
    char cSuit = 3;	//prints suit symbol
    What a character with a value of 3 prints depends on the environment - there's no guarantee it will be a "suit" symbol.

    There is a lot of code here, considering the basic menu loop isn't working correctly. You should make sure you get the menu part right before adding other functionality to your code.

    The idea is to break your problem down into pieces, and solve one piece at a time. Write a little bit of code to solve one piece, compile, and test. When you're certain this works, start adding a little bit of code to solve the next piece, compile, and test - and so on. Build your program up gradually, testing along the way. See here: A development process

    I would suggest you start a new project, and work on getting just the menu portion to work correctly. When it does, then start adding additional code. You don't necessarily have to re-write the code from scratch, as you can just copy/paste code segments from the old project to the new project. But be sure each part works before proceeding to the next.
    Last edited by Matticus; 02-25-2016 at 07:53 AM.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    We prefer you to post your code here in code tags so it's easy to view.

    There are NUMEROUS problems in your code. This is just some of them:

    The line "menuChoice != -1;" has no effect.
    If you turn up the compiler warning level you should get a warning about that.
    You presumably mean to initialize menuChoice to -1. You should do that at the point it's declared:
    Code:
    int menuChoice = -1;
    The getchar() is pretty much useless.

    The srand() call should be before the loop (should usually only be called once in a program).

    The for statement for your shuffle routine uses i < ((rand() % 200) + 100) as its test condition. This will be evaluated at the beginning every iteration, yielding a potentially different number each time. It should be precalculated and stored before the loop:
    Code:
    int limit = rand() % 200 + 100;
    for (int i = 0; i < limit; i++)
    Actually, a far better shuffle algorithm exists: https://en.wikipedia.org/wiki/Fisher...dern_algorithm

    To solve your problem of one extra game after choosing "Exit", you might try something like:
    Code:
    int menuChoice = showMenu();
    while (menuChoice != 0) {
      playGame();
      menuChoice = showMenu();
    }

  4. #4
    Registered User
    Join Date
    Feb 2016
    Location
    Middleburg, Florida
    Posts
    3

    Thank you

    I'll go back and start again. This was my first and I do apologize. I will fix next posting of code.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    That's okay.

    If you haven't done so already, I'd strongly recommend you read the "development process" link I posted above. This technique will make your code development go much smoother.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-01-2014, 03:00 PM
  2. If Loop Not Working?
    By lilywest in forum C Programming
    Replies: 4
    Last Post: 11-23-2013, 01:42 PM
  3. Can't get do while loop working
    By FernandoBasso in forum C Programming
    Replies: 8
    Last Post: 10-05-2011, 06:36 PM
  4. my do while loop not working
    By rodrigorules in forum C Programming
    Replies: 12
    Last Post: 09-07-2005, 06:52 PM