How do I ...

This is a discussion on How do I ... within the C Programming forums, part of the General Programming Boards category; If u want the user to imput a specific char you can do smthg like: Code: int letter; printf ("Please ...

  1. #16
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32

    Post

    If u want the user to imput a specific char you can do smthg like:
    Code:
    int letter;
    printf ("Please input letter [A or B]:\n");
    do {
    letter = getch();
    letter = toupper(letter);
    }
    while ((letter != 'A') && (letter != 'B'));
    
    if (letter == 'A')
    printf("The letter you have inputted was: A\n");
    else
    printf("The letter you have inputted was: B");
    You could try to do this also by using scanf function and a switch.

  2. #17
    Registered User
    Join Date
    Jan 2009
    Posts
    35
    Quote Originally Posted by ralu. View Post
    If u want the user to imput a specific char you can do smthg like:
    Code:
    int letter;
    printf ("Please input letter [A or B]:\n");
    do {
    letter = getch();
    letter = toupper(letter);
    }
    while ((letter != 'A') && (letter != 'B'));
    
    if (letter == 'A')
    printf("The letter you have inputted was: A\n");
    else
    printf("The letter you have inputted was: B");
    You could try to do this also by using scanf function and a switch.
    thanks again

    is it possiable to have multiple else statements:
    Code:
    int letter;
    printf ("Please input letter [A,  B, C]:\n");
    do {
    letter = getch();
    letter = toupper(letter);
    }
    while ((letter != 'A') && (letter != 'B') && (letter != 'C'));
    
    if (letter == 'A')
    printf("The letter you have inputted was: A\n");
    else
    printf("The letter you have inputted was: B");
    else
    printf("The letter you have inputted was: C");
    Last edited by geekrockergal; 02-07-2009 at 06:06 AM.
    Im new to C so I make a lot of beginners mistakes !!!
    Thanks to everyone for helping, Im must be really annoying you *bangs head on desk*

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Yes, it is. But again, you need a condition.
    The if statement is like
    "if x do this, if y do this, if z do this, else do this."
    This is the essence of the if.

    if (condition)
    else if (condition)
    else if (condition)
    else
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #19
    Registered User
    Join Date
    Jan 2009
    Posts
    35
    Quote Originally Posted by Elysia View Post
    Yes, it is. But again, you need a condition.
    The if statement is like
    "if x do this, if y do this, if z do this, else do this."
    This is the essence of the if.

    if (condition)
    else if (condition)
    else if (condition)
    else
    again thank you Im at last understanding !!!!!!!

    I did what you said and Im getting two errors and two warnings
    Code:
    /*********************
    Program Name:Program 1
    Author:geekrockergal
    *********************/
    /*********PREPREOCESSOR DIRECTIVES*************/
    /****************HEADER FILES******************/
    #include <stdio.h>/*Standard I/O Header*/
    #include <conio.h>/*Console  I/O Header*/
    /************NAMED CONSTANTS*******************/
    /****USER-DEFINES AND ENUMERATED DATA TYPES****/
    /************FUNCTION PROTOTYPES***************/
    /*************GLOBAL VARIABLES*****************/
    /***************MAIN FUNCTION******************/
    int main (void)
    {
    int letter;
    printf ("Please input letter[A, B, C or D]: \n");
    do {
    letter = getch();
    letter = toupper(letter);
    }
    while (letter != 'A') && (letter != 'B') && letter != 'C') && (letter != 'D'));
    if(letter == 'A')
    printf("The letter inputted was A\n");
    else if(letter == 'B')
    printf("The letter inputted was B\n");
    else if(letter == 'C')
    printf("The letter inputted was C\n");
    else(letter == 'D')
    printf("The letter inputted was D\n");
    }
    /**************OTHER FUNCTIONS*****************/
    error 22: do - while state missing ;
    error 30: statement missing ;
    warning 30: code has no effect
    warning 31: function should return vaule

    Im bet you lot are thinking just quit while your ahead
    =( =( =( =(
    Last edited by geekrockergal; 02-07-2009 at 06:46 AM.
    Im new to C so I make a lot of beginners mistakes !!!
    Thanks to everyone for helping, Im must be really annoying you *bangs head on desk*

  5. #20
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    error 22: do - while state missing ;
    you missed the ( after while

    else(letter == 'D')

    or use else if (codition)

    or just else

    there is no

    else (condition)

    possibility in C
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Code:
    /*
    Program Name:Program 1
    Author:geekrockergal
    */
    /* PREPREOCESSOR DIRECTIVES */
    /* HEADER FILES */
    #include <stdio.h>
    #include <conio.h>
    /* NAMED CONSTANTS */
    /* USER-DEFINES AND ENUMERATED DATA TYPES */
    /* FUNCTION PROTOTYPES */
    /* GLOBAL VARIABLES */
    /* MAIN FUNCTION */
    int main()
    {
        int letter;
        printf("Please input letter[A, B, C or D]: \n");
        do
        {
            letter = getch();
            letter = toupper(letter);
        }
        while ( (letter != 'A') && (letter != 'B') && (letter != 'C') && (letter != 'D') );
        if (letter == 'A')
            printf("The letter inputted was A\n");
        else if (letter == 'B')
            printf("The letter inputted was B\n");
        else if (letter == 'C')
            printf("The letter inputted was C\n");
        else /*(letter == 'D')*/
            printf("The letter inputted was D\n");
    }
    /* OTHER FUNCTIONS */
    I had to add the corrections in the syntax you must make in red.
    The last, I just removed everything from the else by putting it inside comments.
    "else" means just that - everything else. Everything that does not fit the conditions listed above.
    And don't forget indentation while you're at it.
    I suspect this might be an exercise on if, but you can also write it as:
    Code:
    /*
    Program Name:Program 1
    Author:geekrockergal
    */
    /* PREPREOCESSOR DIRECTIVES */
    /* HEADER FILES */
    #include <stdio.h>
    #include <conio.h>
    /* NAMED CONSTANTS */
    /* USER-DEFINES AND ENUMERATED DATA TYPES */
    /* FUNCTION PROTOTYPES */
    /* GLOBAL VARIABLES */
    /* MAIN FUNCTION */
    int main()
    {
        int letter;
        printf("Please input letter[A, B, C or D]: \n");
        do
        {
            letter = getch();
            letter = toupper(letter);
        }
        while ( (letter != 'A') && (letter != 'B') && (letter != 'C') && (letter != 'D') );
        printf("The letter inputted was %c\n", letter);
    }
    /* OTHER FUNCTIONS */
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #22
    Registered User
    Join Date
    Jan 2009
    Posts
    35
    Quote Originally Posted by Elysia View Post
    Code:
    /*
    Program Name:Program 1
    Author:geekrockergal
    */
    /* PREPREOCESSOR DIRECTIVES */
    /* HEADER FILES */
    #include <stdio.h>
    #include <conio.h>
    /* NAMED CONSTANTS */
    /* USER-DEFINES AND ENUMERATED DATA TYPES */
    /* FUNCTION PROTOTYPES */
    /* GLOBAL VARIABLES */
    /* MAIN FUNCTION */
    int main()
    {
        int letter;
        printf("Please input letter[A, B, C or D]: \n");
        do
        {
            letter = getch();
            letter = toupper(letter);
        }
        while ( (letter != 'A') && (letter != 'B') && (letter != 'C') && (letter != 'D') );
        if (letter == 'A')
            printf("The letter inputted was A\n");
        else if (letter == 'B')
            printf("The letter inputted was B\n");
        else if (letter == 'C')
            printf("The letter inputted was C\n");
        else /*(letter == 'D')*/
            printf("The letter inputted was D\n");
    }
    /* OTHER FUNCTIONS */
    I had to add the corrections in the syntax you must make in red.
    The last, I just removed everything from the else by putting it inside comments.
    "else" means just that - everything else. Everything that does not fit the conditions listed above.
    And don't forget indentation while you're at it.
    I suspect this might be an exercise on if, but you can also write it as:
    Code:
    /*
    Program Name:Program 1
    Author:geekrockergal
    */
    /* PREPREOCESSOR DIRECTIVES */
    /* HEADER FILES */
    #include <stdio.h>
    #include <conio.h>
    /* NAMED CONSTANTS */
    /* USER-DEFINES AND ENUMERATED DATA TYPES */
    /* FUNCTION PROTOTYPES */
    /* GLOBAL VARIABLES */
    /* MAIN FUNCTION */
    int main()
    {
        int letter;
        printf("Please input letter[A, B, C or D]: \n");
        do
        {
            letter = getch();
            letter = toupper(letter);
        }
        while ( (letter != 'A') && (letter != 'B') && (letter != 'C') && (letter != 'D') );
        printf("The letter inputted was %c\n", letter);
    }
    /* OTHER FUNCTIONS */
    thanks again *hits head on desk*
    Im new to C so I make a lot of beginners mistakes !!!
    Thanks to everyone for helping, Im must be really annoying you *bangs head on desk*

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

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