Thread: Simple coding help

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    1

    Simple coding help

    hi guys im very new to c coding, really need help to make this program display "you win" when 3 characters are input...less than 3 or more than 3 should display "you lose"..please help correct me.Thanks

    Code:
    #include<stdio.h>
    
    
     int main (){
       int i=0;
       char str[8];
    
    
       
          printf("Enter 3 characters:\n");
          scanf("%s", &str[10]);
       
        if(i==0)
        {
        printf("You Lose\n");
    }
        else
        printf("You Win\n");
    
    
    }
    Last edited by Trri Shen; 05-04-2016 at 06:01 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Welcome. First, be sure your code is neatly formatted and indented - something like this:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int i=0;
        char str[8];
    
        printf("Enter 3 characters:\n");
        scanf("%s", &str[10]);
    
        if(i==0)
        {
            printf("You Lose\n");
        }
        else
            printf("You Win\n");
    }
    This is wrong:

    Code:
    scanf("%s", &str[10]);
    You're passing the address of a non-existent element of your array. You need to pass the address of the first element, &str[0], but since an array name by itself acts as a pointer to its first element in certain contexts, this can be simplified to str.

    Also, that "scanf" call is potentially dangerous since it is capable of reading and attempting to store more characters than will fit in your array. You can limit the input by including the maximum number of characters in the format string thusly: scanf("%7s", ...). Using "fgets" would be an even better way to read a string.

    To determine how many characters were entered, look into the functions in "string.h".

    If you do not actually need to remember the characters that were entered, you can avoid using arrays and string functions altogether.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > hi guys im very new to c coding,
    You have a really awful teacher then.

    > really need help to make this program display "you win" when 3 characters are input
    The code is in cloud cuckoo land.
    To even overwrite the i variable in the prescribed manner relies of detailed specific knowledge of how variables are laid out, and how the compiler compiles the code.

    Seeing no assignment to i, the compiler is perfectly able to just reduce the final condition to
    printf("You Lose\n");
    since there is no legitimate way to the else clause.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-02-2013, 09:21 PM
  2. Simple Half-Life Coding Question
    By bengreenwood in forum Game Programming
    Replies: 1
    Last Post: 11-07-2007, 02:18 PM
  3. Need some help with coding
    By Alphawaves in forum C Programming
    Replies: 27
    Last Post: 03-28-2007, 06:08 PM
  4. Efficiency coding; a simple top-level window
    By bennyandthejets in forum Windows Programming
    Replies: 7
    Last Post: 07-09-2003, 09:15 AM
  5. simple shell coding
    By estranged in forum C Programming
    Replies: 6
    Last Post: 09-30-2002, 05:59 AM

Tags for this Thread