Thread: loops, menu loops

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    41

    Unhappy loops, menu loops

    so this is my
    Code:
    }
    int c;
    {printf("To play blackjack press 1:");scanf("%d", &c);}
    if(c==1)
    do{
    printf("Here we go\n");
    break;
    }while(c==1);
    if(c!=1)
    do{
    printf("Please only press 1.\n");
    }while(c==1);
    
    return 0;
    }
    //after the choice for rules has been made what string do i need to use in order to have this snippet loop back to the top and have it run the scanf all over again? like if the user presses 2 i want it to loop back up so that they can keep having the chance to enter it again instead of just ending the program and having to run "a.out" again...help
    Last edited by gloworm; 04-11-2010 at 10:29 AM. Reason: had to put in my actual code!!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Post your whole code. This looks bad for various reasons. One is that c==2 implies c!=1 so your conditions are wrong.

    As for the loop, I think what you want is a single do while loop with a switch inside that let's you select what you want to do based on the input.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by claudiu View Post
    Post your whole code. This looks bad for various reasons. One is that c==2 implies c!=1 so your conditions are wrong.

    As for the loop, I think what you want is a single do while loop with a switch inside that let's you select what you want to do based on the input.
    can you give me a small example of a switch statement, and i know that code is way wrong i just wanted to get the just of it, if i knew how to copy from "putty, onyx" and paste i would for sure cause my whole code needs help

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You've mentioned this before, so we'll jump on it this time: do you know what putty is? Putty is just a way to log in to a remote machine. Almost certainly, you don't have to write your code remotely over putty; you can write (and check!) your code on your machine (if you don't have a compiler on your machine, why not? Go get code::blocks) and then upload it to the remote machine when it's finished (since if you've got ssh, you've probably got sftp too).

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There are sooo many WTFs in just this small snippet of code:
    Code:
    if(c==3)
    do{break;}
    while(c==3 && c!=1 && c!=2);
    It's the most convoluted no-op I've ever seen!

    I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a code snippet!
    Start re-learning the basics of loops, if-statements, statement blocks, and general structured programming techniques.
    Last edited by iMalc; 04-10-2010 at 06:18 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    41

    hey

    Quote Originally Posted by claudiu View Post
    Post your whole code. This looks bad for various reasons. One is that c==2 implies c!=1 so your conditions are wrong.

    As for the loop, I think what you want is a single do while loop with a switch inside that let's you select what you want to do based on the input.
    so i put in my actual code instead so maybe now without all of the make shift stuff you could give me your opinion again please.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by gloworm View Post
    so i put in my actual code instead so maybe now without all of the make shift stuff you could give me your opinion again please.
    You're joking right?

    Code:
    if (c==)
    if c equals what?

    Code:
    do{
    printf("Here we go\n");
    break;
    }while(c==1);
    I really hope you intend this to be your main loop of "the game is played in here", otherwise writing a loop structure for something you only want to have happen once is inane.

    Code:
    if(c!=1)
    do{
    printf("Please only press 1.\n");
    }while(c==1);
    And again. Since c will never equal one, and for that matter there's no way to change c inside the loop, this won't ever loop. Extra fun: if the only thing the program does is play blackjack, why are you giving them the choice? Just go.

    IMO this entire swatch of code could go up in flames and it would only make your program better. There doesn't seem to be anything here worth saving. You should write down what you want to have happen, in an organized way (like a flowchart or pseudocode algorithm) before you start typing.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by tabstop View Post
    You're joking right?

    Code:
    if (c==)
    if c equals what?

    Code:
    do{
    printf("Here we go\n");
    break;
    }while(c==1);
    I really hope you intend this to be your main loop of "the game is played in here", otherwise writing a loop structure for something you only want to have happen once is inane.

    Code:
    if(c!=1)
    do{
    printf("Please only press 1.\n");
    }while(c==1);
    And again. Since c will never equal one, and for that matter there's no way to change c inside the loop, this won't ever loop. Extra fun: if the only thing the program does is play blackjack, why are you giving them the choice? Just go.

    IMO this entire swatch of code could go up in flames and it would only make your program better. There doesn't seem to be anything here worth saving. You should write down what you want to have happen, in an organized way (like a flowchart or pseudocode algorithm) before you start typing.
    good point and sorry about my bad typing i guess i need to learn to review my posts to avoid getting sarcastic answer and no answers at all, ill work on that. But your right i really dont need to give them the option. thanks

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    glowworm, i still don't see your code. Post your entire code no matter how wrong and let's work from there.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by claudiu View Post
    glowworm, i still don't see your code. Post your entire code no matter how wrong and let's work from there.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    #define DECKSIZE 52
    #define VALUE 9
    #define FACE 4
    
    typedef struct {
            int value;
            char* suit;
            char* name;
    }Card;
    
    Card cards[DECKSIZE];
    char *faceName[]={"two","three","four","five","six","seven","eight","nine",
                      "ten","jack","queen","king","ace"};
    char *suitName[]={"spades","diamonds","hearts","clubs"};
    void printDeck(){
    int i;
    for (i=0;i<6000;i++){
    int j = i+ rand()%(6000-i);
            this=rand()%DECKSIZE;
            that=rand()%DECKSIZE;
    while(this==that)that=rand()%(52+1);
    //printf("shuffle  card%d with card %d\n", this, that);
    temp=cards[this];
    cards[this]=cards[that];
    cards[that]=temp;
    }
    }
    int main(){
            int suitCount=0;
            int faceCount=0;
            int i;
            for(i=0;i<DECKSIZE;i++){
                    if(faceCount<9){
                            cards[i].value=faceCount+2;
                    }else{
                            cards[i].value=10;
                    }
                    cards[i].suit=suitName[suitCount];
                    cards[i].name=faceName[faceCount++];
                    if(faceCount==13){
                            cards[i].value=11;
                            suitCount++;
                            faceCount=0;
                    }
            }
    {
    
    //printDeck();
    //shuffleDeck();
    //printDeck();
            }
    int P_hand, D_hand;
    P_hand=rand() %52+1;
    D_hand=rand() %52+1;
    shuffleDeck();
    printf("%s of %s,----%s of %s\n ",cards[1].name,cards[3].suit,cards[4].name,cards[2].suit );
    shuffleDeck();
    printf("%s of %s,----%s of %s\n",cards[1].name,cards[3].suit,cards[4].name,cards[2].suit  );
    shuffleDeck();
            return 0;
    }

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by claudiu View Post
    glowworm, i still don't see your code. Post your entire code no matter how wrong and let's work from there.
    i posted my code so far but im really having questions on how i can assign the value of the cards printed and use that total to determine whether or not to hit or stand and to identify bust, blackjack and to assign the printf functions at the end of the code with the P_hand and the D_hand. hopefully you can understand my giberish
    Last edited by gloworm; 04-12-2010 at 04:38 PM.

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by tabstop View Post
    You're joking right?

    Code:
    if (c==)
    if c equals what?

    Code:
    do{
    printf("Here we go\n");
    break;
    }while(c==1);
    I really hope you intend this to be your main loop of "the game is played in here", otherwise writing a loop structure for something you only want to have happen once is inane.

    Code:
    if(c!=1)
    do{
    printf("Please only press 1.\n");
    }while(c==1);
    And again. Since c will never equal one, and for that matter there's no way to change c inside the loop, this won't ever loop. Extra fun: if the only thing the program does is play blackjack, why are you giving them the choice? Just go.

    IMO this entire swatch of code could go up in flames and it would only make your program better. There doesn't seem to be anything here worth saving. You should write down what you want to have happen, in an organized way (like a flowchart or pseudocode algorithm) before you start typing.
    my code is posted so let me know where i need to go from there. you can see where im stuck cause all its doing now is printing out the random cards but i still need it to total the value of the posted cards and determine if its a blackjack, and then if the user decides to hit if they bust or not. Youll see

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Apparently this forum has become gloworm's Blackjack forum. Nice.

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by rags_to_riches View Post
    Apparently this forum has become gloworm's Blackjack forum. Nice.
    sorry im just lost and have no one to help. I have the books but im trying to learn on my own so i thought that people in these forums would be helpful. Sorry for all the questions but im just trying to learn

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No one cares that you ask multiple questions. They care that you have FOUR threads going now on the SAME topic.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM