Like Tree1Likes
  • 1 Post By grumpy

Stumped... Whats wrong here?

This is a discussion on Stumped... Whats wrong here? within the C Programming forums, part of the General Programming Boards category; Hey Guys, This is my first post ever, I can't figure what is wrong with this code. any help is ...

  1. #1
    H8c
    H8c is offline
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    Stumped... Whats wrong here?

    Hey Guys,

    This is my first post ever, I can't figure what is wrong with this code. any help is greatly appreciated.

    insert
    Code:
    #include<stdio.h>
    
    int main() {                                                                       
    
        int x=0;                                                                       
        int y=0;                                                                       
        char input;
    
    printf( "Andrew Fowler, Assingment5 part2\n\n" );
    printf( "Use G, J, Y, N, T, U, B, M as arrow inputs. Type q to quit\n");
    
        while( input != 'q');
            {
            scanf(" %c", &input );
        
            if (input = 'g'){                                                                /*g = left move*/
                x= x-1;
                printf("Cordinates (X,Y) = (%d,%d)\n", x, y );}
    
            if (input = 'j'){                                                                /*j = right move*/
                x= x+1;
                printf("Cordinates (X,Y) = (%d,%d)\n", x, y );}
    
            if (input = 'y'){                                                                /*y = up*/
                y= y+1;
                printf("Cordinates (X,Y) = (%d,%d)\n", x, y );}
    
            if (input = 'n'){                                                                /*n = down*/
                y= y-1;
                printf("Cordinates (X,Y) = (%d,%d)\n", x, y );}
    
            if (input = 't'){                                                                /*t = up and left*/
                x= x-1;
                y= y+1;
                printf("Cordinates (X,Y) = (%d,%d)\n", x, y );}
    
            if (input = 'u'){                                                                /*u = up and right*/
                x= x+1;
                y= y+1;
                printf("Cordinates (X,Y) = (%d,%d)\n", x, y );}
    
            if (input = 'b'){                                                                /*b = down and left*/
                x= x-1;
                y= y-1;
                printf("Cordinates (X,Y) = (%d,%d)\n", x, y );}
    
            if (input = 'm'){                                                                /*m = down and right*/
                x= x+1;
                y= y-1;
                printf("Cordinates (X,Y) = (%d,%d)\n", x, y );}
    
            }
                                                                                        
        scanf(" %c", &input );                                                            /*Stops the Program from Ending Abruptly*/
        return 0;                                                                            /*Ends Programs*/
    
    }

  2. #2
    H8c
    H8c is offline
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Sorry I forgot to post the error I am getting, here it is;

    "Run-Time Check Failure #3 - The variable 'input' is being used without being initialized."

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Um, the problem is exactly what the error message says. You check the value of "input" in the while statement, but the scanf that assigns things to it happens after the first check.

    Try this as line 7:
    Code:
    char input='\0';
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    > if (input = 'g')
    The use of = when you should be using == is a biggie as well.

    > while( input != 'q');
    The trailing ; isn't helping either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,536
    Quote Originally Posted by TheBigH View Post
    Try this as line 7:
    Code:
    char input='\0';
    I'd actually structure the loop as a do/while, since the loop body needs to be executed at least once.

    The points mentioned by Salem are also critical to getting the code working as (presumably) intended.

    Less critical, there is a lot of repeated code within the loop body. You've fallen into the trap of unnecessary copy/paste. Plenty of opportunities for simplifying that code, and removing repetition, which would make the code easier to understand.

    And the word "coordinate" has two 'o's, not one.
    iMalc likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  6. #6
    H8c
    H8c is offline
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Hey Guys,

    The tips are much appreciated, I have changed the code to a do - while orientation. However I don't see why trailing ; isn't helping?

    > while( input != 'q');
    The trailing ; isn't helping either

    Thanks again,

    AF

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    while (input != 'q');
    is an infinite loop. The body of the while loop is empty thus "input" will never change.

    The syntax of a while loop is
    Code:
    while ( condition ) 
    {
        some statements
    }
    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help not sure whats wrong
    By iamnewtothis in forum C++ Programming
    Replies: 4
    Last Post: 03-04-2011, 03:55 AM
  2. Whats wrong?
    By jturner38 in forum C Programming
    Replies: 24
    Last Post: 04-16-2009, 04:51 AM
  3. Whats wrong?
    By D3V1LD0G in forum C Programming
    Replies: 3
    Last Post: 08-19-2008, 09:29 PM
  4. whats wrong with this
    By shabbirhussain in forum C Programming
    Replies: 3
    Last Post: 08-18-2008, 09:32 AM
  5. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM

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