Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

if statments help (C program)

Options
  • 05-12-2011 4:00pm
    #1
    Registered Users Posts: 258 ✭✭


    hello,
    i am a learning the c programing language and currently have a problem.
    i am writing a code so that if the string .-- is entered that it will print out the letter H instead.

    here is my code so far:

    char input;
    printf("enter the string");
    scanf("%s", &input);
    if (input == "..-") {
    printf("%s", "H");
    }

    basically my question is how do i define H to be printed out when ..- is entered?
    any help would be great as i am stuck at this point :o


Comments

  • Registered Users Posts: 26,574 ✭✭✭✭Creamy Goodness


    the char datatype will only hold one character.

    you'll need to look into implementing a c-string which is basically an array of char datatypes.


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    flying11 wrote: »
    hello,
    i am a learning the c programing language and currently have a problem.
    i am writing a code so that if the string .-- is entered that it will print out the letter H instead.

    here is my code so far:

    char input;
    printf("enter the string");
    scanf("%s", &input);
    if (input == "..-") {
    printf("%s", "H");
    }

    basically my question is how do i define H to be printed out when ..- is entered?
    any help would be great as i am stuck at this point :o

    Can you further into what problems you are encountering. One thing I see, is in plain English you say "[...] if the string .-- is entered [...]", but in your source code you say: if (input == "..-")

    Is it .-- or ..- (of if is morse code, should it H really be ....)


  • Registered Users Posts: 258 ✭✭flying11


    Can you further into what problems you are encountering. One thing I see, is in plain English you say "[...] if the string .-- is entered [...]", but in your source code you say: if (input == "..-")

    Is it .-- or ..- (of if is morse code, should it H really be ....)


    typo, its ment to be ..- = H
    its similar to morse code, but it is my own "made up" version of it with my own series of dots and dashes.

    i.e my code is not equal to morse code.


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    Look into char arrays and using fgets rather than scanf.


  • Registered Users Posts: 1,994 ✭✭✭lynchie


    And also read up on string comparisons and why == will not work


  • Advertisement
  • Registered Users Posts: 34 mairtin.l


    #include<stdio.h>

    int main()
    {
    char pstring[21]="";
    char *pother ="..-";
    printf("Enter a string\n");
    scanf("%20s",pstring);
    if(!strcmp(pstring,pother))//compare two strings, This returns a 1 if false and 0 if true so ! is used
    {
    printf("Corrent !!!\n");
    }
    else
    {
    printf("Incorrent \n");
    }
    return 0;
    }


    Hope this helps


  • Registered Users Posts: 258 ✭✭flying11


    mairtin.l wrote: »
    #include<stdio.h>

    int main()
    {
    char pstring[21]="";
    char *pother ="..-";
    printf("Enter a string\n");
    scanf("%20s",pstring);
    if(!strcmp(pstring,pother))//compare two strings, This returns a 1 if false and 0 if true so ! is used
    {
    printf("Corrent !!!\n");
    }
    else
    {
    printf("Incorrent \n");
    }
    return 0;
    }


    Hope this helps

    thank you! :D


Advertisement