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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

if statments help (C program)

  • 05-12-2011 3: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,584 ✭✭✭✭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 Posts: 2,013 ✭✭✭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