Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

if statments help (C program)

  • 05-12-2011 04:00PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 26,449 ✭✭✭✭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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    Look into char arrays and using fgets rather than scanf.


  • Registered Users, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭lynchie


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


  • Advertisement
  • Registered Users, Registered Users 2 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, Registered Users 2 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