Thread: how to display name and integer

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    Quezon City, Philippines, Philippines
    Posts
    2

    how to display name and integer

    Hello everyone. I was trying to display name and integer using printf() but the 'string' part doesnot appear. Please help me.

    here is the code:
    Code:
    #include<stdio.h>
    
    
    int main() {
        
        char name[20];
        char level;
        
        printf("Enter employee name: \n");
        scanf("%s",&name);
        printf("Enter bonus level (0, 1 or 2): \n");
        scanf("%i",&level);
        
        printf("%s and %i",name,level);
        
        return 0;
    }
    and the OUTPUT:
    Enter employee name:
    jane
    Enter bonus level (0, 1 or 2):
    0
    and 0



    Please help.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Location
    Quezon City, Philippines, Philippines
    Posts
    2

    answer

    I understand my

    char level:

    so the output should be %c

    or change the variable to

    int level


  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The name of the string IS the address of the base element of the string, so just scanf("%s",name) (with no & before it), is correct.

    When you press the enter key for entering the name, you have also put a newline char, into the input stream - so add a space before the %c for the level, if you keep it a char: scanf(" %c", &level). An int is preferred however.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 09-21-2008, 06:11 PM
  2. How to display integer to binary format?
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2005, 11:32 PM
  3. How to read in an integer and display it again
    By axr0284 in forum C++ Programming
    Replies: 7
    Last Post: 12-07-2004, 01:37 PM
  4. Help me: Display 100's 10's 1's from an integer!
    By Ramza in forum C Programming
    Replies: 5
    Last Post: 03-12-2002, 10:31 PM
  5. How to display a 64-bit integer?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2002, 11:13 PM