loop needed also how to make input use letters

This is a discussion on loop needed also how to make input use letters within the C Programming forums, part of the General Programming Boards category; heres my code ill point out what i need looped isnt done yet but has errors that i cant figure ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    loop needed also how to make input use letters

    heres my code
    ill point out what i need looped

    isnt done yet but has errors that i cant figure out how to fix
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    
    {
    int a,name,main,starta,outa;
           printf("Please enter your name: ");
           scanf("%user",&name);
           printf("Hello, %user\n\n",name);
           printf("Welcome to the quest for users brain\n"
           "Sponsored By DUR\n"
           "for more info on DUR go to www.freewebs.com/lordhsv1991\n\n"
           "Name: DURxLoRdHSV\n"
           "Copyright: 2006\n");
    system("PAUSE");
    system("CLS");
    
        printf("   Main Menu \n"); 
        printf("-> 1 - start\n");
        printf("-> 2 - quit\n");
        printf("-> ");
        scanf("%d", &main);
    switch (main) {
           case 1:
                system("CLS");
                printf("Hayden was at the store when he realised he had no brain\n"
                "so after he tryed to realse it and ........ his pants he went home to try to think\n"
                "big success that was\n"
                "well back to the story\n"
                "what do u want to do know?\n");
                  printf("   go to \n"); 
        printf("-> 1 - shops\n");
        printf("-> 2 - park\n");/*done but needs loop back to here (loop a)*/
        printf("-> ");
        scanf("%d", &starta);
    if(starta == 1) {
                system("CLS");
                printf("you look around the shops and find some canned brains\n"
                "unforunalty, even u know that wont work LOL\n"
                "after hours of lookin high and low\n"
                "you go home dissapointed\n"
                "your sad so you indulge on a cake\n "
                "then you go out/n");
                    printf(" goin out \n"); 
        printf("-> 1 - movies\n");
        printf("-> 2 - walk\n");/*needs a loop back to here (loop b)*/
        printf("-> ");
        scanf("%d", &outa);
        if(outa == 1){
                printf("You go and watch your favourite movie\n"
                "this cheers you up\n"
                "untill.......\n");
                systemf("PAUSE");
                printf("the power dies...\n"
                "this angers you...\n"
                "so you throw a fit in the movies and end up trying to sue\n"
                "but....\n"
                "you dont have the mental capacity to do so...\n"
                "the movies gives u a store credit\n"
                "you go home...\n");
                
                if(outa == 2) /* needs loop back to outa menu (loop b)*/
                 printf("You get to the door and colapse of exhaustion\n"
                 "so.... you decide to sleep on the lounge for abit\n"
                 "several hours later...\n"
                 "you are now ready to go out again\n"):
                /*loop need to start here*/
                }
                if(starta == 2){
                system("CLS");/*needs aloop upot 1st menu (loop a)*/ 
                printf("dont you remember.....\n"
                "after u releived your self on that old person\n"
                "you were banned for life\n"); /*loop need to start here*/
                }        
                case 2:
                     system("CLS");
                     printf("thanks for using DUR sofetware");
                     system("PASUE");
                     break;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You my good man, need a finite state machine! (Among other things.)
    Code:
    #define AT_STORE 1
    #define AT_HOME 2
    ...
    #define AT_SOMEWHERE n
    
    
    while( state != AT_DIEPROGRAMDIE )
    {
        switch( state )
        {
            case AT_HOME:
                ...display at home text and prompt for choices
                if( choice == foo )
                    state = AT_MCMEATIES;
                else
                if( choice == bar )
                    state = AT_THEMALL;
                /* else the state doesn't change, because they didn't enter the right thing... */
            break;
    
            case AT_FOO:
                ... foo stuff ...
            break;
    
            case AT_BAR:
                ... bar stuff ...
            break;
    
            ...
        }
    }
    printf("Goodbye...\n");

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

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    finite state machine!?????
    wat the lol

    like i said else where
    i dont get half the stuff that is posted here
    lol

    but that doesnt explain the answer to my question either

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's simple really. You make one loop, which wraps everything up in it. You use 1 variable to control where you go while you're inside your loop. Here I've elected to use a switch to jump around to various states, or conditions. All those places where you keep saying "need loop here", you just move into one big loop, and when the condition's right, you go to the right place. Here we use a very simple version of one.

    There are only a few states. IE: We can be at home. We can be at the store. We can be at foo, and at bar. However, that's all we can do. Thus, we have a finite number of choices. You update the state of where you're at allowing for only valid moves from your current state. For example, if we had we were sleeping, we might limit the current choices to: 'awake', or 'dead'. Otherwise you remain asleep. You can die in your sleep. You can also wake up from being asleep. Otherwise, you remain asleep.

    Now you just do the same thing with your code above. It's pretty simple, and should be adequately illustrated if you look at the 'AT_HOME' state above. You read their choice into a temproary variable, and if it matches any of the states you allow them to change to, then you update the state.
    Code:
    while( state != STATE_DEAD )
    {
        switch( state ) /* see what the state is... */
        {
            case STATE_ASLEEP: /* ...and go there. */
                printf("You're asleep. You can: \n"
                    "1 - Wake up.\n"
                    "2 - Die.\n"
                    "> " );
                scanf( "%d", &choice ); /* Do your own error checking. */
                if( choice == 1 )
                {
                     /* They've chosen to wake up, so change state to be STATE_AWAKE */
                     state = STATE_AWAKE;
                }
                if( choice == 2 )
                {
                    /* They've chosen to die. Kill'm. */
                    state = STATE_DEAD;
                }
                /* Otherwise, it's an invalid choice, so don't do anything, and it will loop back here again. */
            break;
            ... put other cases here ...
        }
    }
    /* If we're out of the loop, we're at state == DEAD */
    printf("You're dead.\n");
    If you don't know what a switch is, I'd suggest grabbing your C book of choice and doing some reading.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  3. Replies: 4
    Last Post: 04-03-2008, 10:07 PM
  4. Infinite Loop when entering invalid input
    By acwheat in forum C Programming
    Replies: 5
    Last Post: 04-18-2006, 05:17 PM
  5. Input help needed
    By blindleaf in forum C Programming
    Replies: 5
    Last Post: 04-08-2003, 07:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21