Thread: Some help to understand.

  1. #1
    Registered User Sietor's Avatar
    Join Date
    Aug 2016
    Location
    Sweden
    Posts
    13

    Some help to understand.

    So I am making this basic game in C, And when I type in more than 1 character in the scanf part it read as example:

    Where would you like to go:
    ERASF <---- This is what the user types.
    You are in your house
    You are in your house
    You are in your house
    You are in your house
    You are in your house

    This is the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    
    int main()
    {
    
        char Move;
        float LocationA = 0;
        float LocationB = 0;
    
        printf("\n\n\nWhere would you like to go?\n");
        do{
    
            scanf(" %s", &Move);
    
            if(Move == 'E'){
                LocationB++;
            }
            if(Move == 'W'){
                LocationB--;
            }
            if(Move == 'S'){
                LocationA--;
            }
            if(Move == 'N'){
                LocationA++;
            }
            if(LocationA == 0 && LocationB == 0){
                printf("You are in your house\n");
            }
            if(LocationA == 0 && LocationB == 1){
                printf("You are on a road\n");
                continue;
            }
            if(LocationA == 1 && LocationB == 0){
                printf("You are in a forest");
                continue;
            }
            if(LocationA == 0 && LocationB == -1){
                printf("You are in a forest\n");
                continue;
            }
            if(LocationA == -1 && LocationB == 0){
                printf("You are in a forest\n");
                continue;
            }
    
        }while(Move != 'M');
    
        return 0;
    }
    Can I make the program Only read what the user types, if there is only 1 character?
    And how?
    And why does it reply on every character I write in the scanf instead of just the 1 time?

    Ask if you dont understand the questions and i will try to explain more.
    Last edited by Sietor; 08-17-2016 at 02:23 PM.

  2. #2
    Registered User Sietor's Avatar
    Join Date
    Aug 2016
    Location
    Sweden
    Posts
    13
    I have edited the post

  3. #3
    Registered User Sietor's Avatar
    Join Date
    Aug 2016
    Location
    Sweden
    Posts
    13
    Where would you like to go?
    Home <---- User
    You are in your house
    Really? <----User
    You are in your house
    Why am i home <--- User
    You are in a forest
    You are in a forest
    You are in a forest
    You are in a forest

    Efter the edited code this are the responses i get after typing in what the User point at.
    and why is it so?
    Last edited by Sietor; 08-17-2016 at 02:28 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    One major problem I see is that in your scanf call, you're asking for a string, but only giving it a single char to read into. This will cause undefined behavior. You really need to use %c instead of %s.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    What did you not understand from this topic?

    You're still not using the correct format specifier for your scanf() call, which produces undefined behavior.

    And in future please don't open a new topic when you already have a topic open for the problem.


    Jim

  6. #6
    Registered User Sietor's Avatar
    Join Date
    Aug 2016
    Location
    Sweden
    Posts
    13
    Ok wont happen again Jim!
    Elkvis even if I change the %s to a %c i get the same problem... as fast as i put a space between two characters, it gives me multiple anwsers.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Sietor View Post
    Elkvis even if I change the %s to a %c i get the same problem...
    My response wasn't intended to solve the problem that you're asking about. Only to make your program more semantically correct.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    Registered User Sietor's Avatar
    Join Date
    Aug 2016
    Location
    Sweden
    Posts
    13
    OK!
    Thanks Elkvis

  9. #9
    Registered User
    Join Date
    Aug 2016
    Posts
    1
    I've prepared this little routine for you...
    be aware that
    Code:
    getchar()
    instead of
    Code:
    scanf(...)
    is used in combination with
    Code:
    fflush()
    .
    This enables a little more robust program behavior as user input is limited to the first character on a line and no dangling input remaining, which may cause undefined behavior. No guarantees though.

    Code:
    void Getchar_Test( void )
    {
        const char    s[3][3][16] =    { 
                                        { { "Road" }, { "Road" }, {"Forest"} } , 
                                        { { "Shed" }, { "House" }, {"Road"} } , 
                                        { { "Road" }, { "Jeep" }, {"Corpse!"} } , 
                                    }, 
                    s_blocked[] = "Your way is blocked...",
                    c_exit = 'q';
        const int    ox = 1, oy = 1;
        int            x = 0, y = 0, 
                    c, moves;
        
        
        printf("Possible commands...\n\t\t(n)orth, (w)est, (s)outh, (e)ast, (i)nventory, (q)uit.\n\n");
        printf( "(n, w, s, e, i or %c)? ", c_exit );
        c = getchar();
        c = toupper( c );
        for ( moves = 0; toupper(c_exit) != c; moves++ )
        {
            switch( c )
            {
                case 'N': 
                    if (-1 < y) { y--; } else { printf("\t%s\n", s_blocked); }
                    break;
                case 'W':
                    if (-1 < x) { x--; } else { printf("\t%s\n", s_blocked); }
                    break;
                case 'S':
                    if (1 > y) { y++; } else { printf("\t%s\n", s_blocked); }
                    break;
                case 'E':
                    if (1 > x) { x++; } else { printf("\t%s\n", s_blocked); }
                    break;
                case 'I':
                    printf( "\tYou carry... %s.\n", "nothing" );
                    break;
                default:
                    printf("Unknown command...\n");
            }
            
            printf("\n[Move %d]\n\tYou are at... %s\n\n", moves + 1, s[oy+y][ox+x] );
            
            printf( "(n, w, s, e, i or %c)? ", c_exit );
            fflush( stdin );    /* remove newline ...and other possible characters not needed */
            c = getchar();
            c = toupper( c );
        }
        
        printf("\n\n\nAfter %4i move(s)... your adventure is over.\n", moves );
    }

  10. #10

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    fflush( stdin ); /* remove newline ...and other possible characters not needed */
    Don't you mean, produces undefined behavior? The fflush() function is only defined by the standard to work with output streams, using it on an input stream produces undefined results.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Don't understand this
    By .C-Man. in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2011, 07:01 AM
  2. NOT, AND, OR- I do not understand at all.
    By Phanntom in forum C++ Programming
    Replies: 7
    Last Post: 08-21-2006, 06:28 PM
  3. Don't understand
    By jaylc185 in forum Game Programming
    Replies: 2
    Last Post: 05-24-2005, 07:29 AM
  4. Help me understand.
    By blindman858 in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2005, 04:45 AM
  5. Help me understand why
    By byteme101 in forum C Programming
    Replies: 7
    Last Post: 12-08-2004, 09:39 AM

Tags for this Thread