Thread: Format String is not a string literal

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    2

    Format String is not a string literal

    Hi everyone,

    This is really my first time posting and I'm a newcomer to programming. I'm wondering if someone can point out what i am doing wrong in the if and for loop with choice 2.
    Code:
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct employeedatabase
    {
        char firstname[ 20 ];
        char lastname[ 20 ];
        char dateofbirth[ 10 ];
        char ssn[ 11 ];
        char contactnumber[ 13 ];
        unsignedint age[ 2 ];
        char gender[ 6 ];
        double hourlysalary[ 10 ];
    }employees[10];
    
    
    //Function prototype
    int choice = 0;
    int quit = 3;
    int counter = 0;
    int i = 0;
    
    
    int main( )
    {
        printf("\n********** MENU **********\n\n"
               "Enter your choice:\n"
               "1: Add a New Employee to the list.\n"
               "2: Print Current Employees.\n"
               "3: Quit.\n\n" );
    
        printf("Your choice is: ");
        scanf( "%d", &choice);
    
        while(choice !=3)
        {
            if( choice == 1)
    
            {
                printf("\n***** New Employee *****\n");
                printf("\nPlease fill in all information below:\n\n");
    
                printf("First Name: ");
                scanf("%s", employees[counter].firstname);         //input for First Name
    
                printf("Last Name: ");
                scanf("%s", employees[counter].lastname);          //input for Last Name
    
                printf("DOB (MM/DD/YYYY): ");
                scanf("%s", employees[counter].dateofbirth);       //Input for DOB
    
                printf("SSN: ");
                scanf("%s", employees[counter].ssn);               //Input for Social
    
                printf("Telephone Number: ");
                scanf("%s", employees[counter].contactnumber);     //Input for Phone Number
    
                printf("Gender: ");
                scanf("%s", employees[counter].gender);            //Input for Gender
    
                printf("Hourly Salary: $");
                scanf("%lf", employees[counter].hourlysalary);     //Input for Salary
    
                printf("\nEnter your choice:\n"
                       "1: Add a New Employee to the list.\n"
                       "2: Print Current Employees.\n"
                       "3: Quit.\n\n" );
    
                printf("Your choice is: ");
                scanf( "%d", &choice);
    
                if( choice == 1) counter++;// Increments employees by one
    
            }//End of Choice 1
    
    
            if(choice == 2)
    
            {
                printf("\n***** List of Employees *****\n");
                {
                    for(i = 0;i <= 10; i++)
                    {
                        printf(employees[counter].firstname);
                        printf(employees[counter].lastname);
                        printf(employees[counter].dateofbirth);
                        printf(employees[counter].ssn);
                        printf(employees[counter].contactnumber);
                        printf(employees[counter].gender);
                        printf(employees[counter].hourlysalary);
                    }//End of for loop
                }
    
            }//End of Choice 2
    
    
    
        }// End of While Loop
    
    
    
    }// End main
    
    Attached Files Attached Files

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    printf("%s",employees[counter].firstname);
    To prevent hacking of your code with names like "%p %d" etc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    if you just want to print a string with no formatting, try puts().
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You should have your global variables, changed to local (automatic) variables, declared inside the functions they're used in. These are not function prototypes, and shouldn't be outside functions:
    Code:
    //Function prototype
    int choice = 0;
    int quit = 3;
    int counter = 0;
    int i = 0;
    Avoiding most if not all global variables, is a big improvement.


    In your for loop for choice == 2, you are using the counter variable as your index, when you should be using the i variable.
    Last edited by Adak; 07-17-2013 at 10:03 AM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    struct employeedatabase
    {
        char firstname[ 20 ];
        char lastname[ 20 ];
        char dateofbirth[ 10 ];
        char ssn[ 11 ];
        char contactnumber[ 13 ];
        unsignedint age[ 2 ];
        char gender[ 6 ];
        double hourlysalary[ 10 ];
    }employees[10];
    
    ...
    
    for(i = 0;i <= 10; i++)
    {
        printf(employees[counter].firstname);
        printf(employees[counter].lastname);
        printf(employees[counter].dateofbirth);
        printf(employees[counter].ssn);
        printf(employees[counter].contactnumber);
        printf(employees[counter].gender);
        printf(employees[counter].hourlysalary);
    }//End of for loop
    Adak already mentioned the "i vs counter" issue but... your loop condition should not ever allow entry into the body when the loop/index variable is more than what is valid for the array in question.


    Also, when you are inputting the data you'd better make sure that counter is 0-9... anything else and you should not allow input into that employees array.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jul 2013
    Posts
    2
    Thanks for the help everyone, I believe i have fixed the issues that you pointed out.

    I just have one more question, for some reason when i run the project and choose option 2 the code runs endlessly until i stop it or it quits unexpectedly.

    Any thoughts?

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You scan in "choice" before you enter the loop. That variable is only updated within the loop when you run choice 1, so when you enter two, it continuously executes choice 2 each iteration.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a C++ std::string literal
    By Subsonics in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2013, 09:57 AM
  2. Modifying a string literal.
    By Eman in forum C++ Programming
    Replies: 45
    Last Post: 12-30-2010, 06:37 PM
  3. question about string literal
    By pangzhang in forum C Programming
    Replies: 6
    Last Post: 07-31-2010, 07:25 AM
  4. Replies: 7
    Last Post: 07-18-2005, 08:43 AM
  5. String literal
    By subdene in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2002, 02:10 PM