Thread: Help with understanding a given warning

  1. #16
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    if you want to print an int, you need %d instead of %s. %s is only for strings.
    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?

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Fix all the warnings first. You have a problem in the code snippet that you posted that your compiler should be warning you about.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Jul 2014
    Posts
    34
    OK. no warnings now but if you enter incorrect ID or password and have to re-enter one or both, once you get in the program doesn't recognize any of your choice selections as valid.
    Code:
    /* Leslie Bauert
       CSE1311 HW 8
       Aug. 11, 2014
     */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    struct Members
    {
        char fName[20];
        char lName[20];
        char Email[30];
        char id[20];
        char pass[20];
        char SSN[15];
        char gender;
        double GPA;
        int age;
    
    
    };
    
    
    void Up_Date_Info(struct Members mem[20],int i);
    void View_Info (struct Members mem[20], int i);
    void display();
    
    
    int main(void)
    {
        int i;
        struct Members mem[20];
        for(i = 0; i < 20; i++)
            {
                strcpy(mem[i].id, " ");
                strcpy(mem[i].pass, " ");
                strcpy(mem[i].Email, "             ");
                strcpy(mem[i].fName, " ");
                strcpy(mem[i].lName, " ");
                strcpy(mem[i].SSN, " ");
                //char gender = '\0';
            }
        char choice;
        char temp[20];
        char temppass[20] = "";
        i = 0;
    
    
         printf("Start Program\n");
         printf("1. Log in\n");
         printf("2. Sign up\n");
         printf("3. Exit\n");
    
    
        do
        {
    
    
            printf("What do you want to do: ");
            scanf("%c", &choice);
            printf("\n");
    
    
            switch (choice)
            {
                case '1':
                    printf("Please enter your id: ");
                    strcpy(temp, "");
                    scanf("%s", temp);
    
    
                    printf("Please enter your password: ");
                    strcpy(temppass, "");
                    scanf("%s", temppass);
                    getchar();
    
    
                    for (i = 0; i < 20; i++)
                    {
                        if (strcmp(temp, mem[i].id) != 0 || strcmp(temppass, mem[i].pass) != 0)
                        {
                            while ((strcmp(temp, mem[i].id) != 0 || strcmp(temppass, mem[i].pass) != 0))
                            {
                                if (strcmp(temp, mem[i].id) != 0)
                            {
                                printf("Please re-enter your id: ");
                                strcpy(temp, "");
                                scanf("%s", temp);
                            }
                            if (strcmp(temppass, mem[i].pass) != 0)
                            {
                                printf("Please re-enter your password: ");
                                strcpy(temppass, "");
                                scanf("%s", temppass);
                                getchar();
                            }
                            }
    
    
                            printf("\n");
                        }
    
    
                        if (strcmp(temp, mem[i].id) == 0 && strcmp(temppass, mem[i].pass) == 0)
                        {
                            printf("Hello %s!!!\n\n", mem[i].id);
                            do
                            {
                                printf("1. View User Account\n");
                                printf("2. Update User Account\n");
                                printf("3. Log out\n\n");
                                printf("What do you want to do?");
                                choice = '\0';
                                scanf("%c", &choice);
                                getchar();
                            switch (choice)
                            {
                            case '1':
                                printf("User id is: %s\n", mem[i].id);
                                printf("User password is: %s\n", mem[i].pass);
                                printf("Your first name is %s\n", mem[i].fName);
                                printf("Your last name is %s\n", mem[i].lName);
                                printf("Your Email is %s\n", mem[i].Email);
                                printf("Your GPA is %.1f\n", mem[i].GPA);
                                printf("Your gender is %c\n", mem[i].gender);
                                printf("Your age is %d\n", mem[i].age);
                                printf("Your SSN is %s\n", mem[i].SSN);
                                printf("\n");
                                break;
    
    
                            case '2':
                                Up_Date_Info(mem, i);
                                getchar();
                                break;
    
    
                            case '3':
                                break;
    
    
                            default:
                                printf("That is not a valid option at this time\n");
                                break;
                            }
                          }while (choice != '3');
                        }
    
    
                        display();
                        break;
                    }
    
    
                    break;
                case '2':
                    printf("Please input user id: ");
                    scanf("%s", temp);
                    for(i = 0; i < 20; i++){
                        if(strcmp(" ", mem[i].id) == 0)
                            {
                            strcpy(mem[i].id, temp);
    
    
                            printf("Please input user password: ");
                            strcpy(temp, "");
                            scanf("%s", temp);
                            strcpy(mem[i].pass, temp);
    
    
                            printf("Please input Email: ");
                            strcpy(temp, "");
                            scanf("%s", temp);
                            strcpy(mem[i].Email, temp);
    
    
                            printf("Please input GPA: ");
                            strcpy(temp, "");
                            scanf("%s", temp);
                            mem[i].GPA = atof(temp);
    
    
                            printf("Please input first name: ");
                            strcpy(temp, "");
                            scanf("%s", temp);
                            strcpy(mem[i].fName, temp);
    
    
                            printf("Please input last name: ");
                            strcpy(temp, " ");
                            scanf("%s", temp);
                            strcpy(mem[i].lName, temp);
    
    
                            printf("Please input gender: ");
                            strcpy(temp, " ");
                            scanf("%s", temp);
                            mem[i].gender = temp[0];
    
    
                            printf("Please input age: ");
                            strcpy(temp," ");
                            scanf("%s", temp);
                            mem[i].age = atoi(temp);
    
    
                            printf("Please input SSN: ");
                            strcpy(temp, "");
                            scanf("%s", temp);
                            strcpy(mem[i].SSN, temp);
    
    
                            printf("Thank you!!!\n\n");
                            display();
                            break;
                        }
                    }
                    break;
                case '3':
                    break;
                default:
                    printf("%c is not a viable option at this time\n", choice);
                    break;
            }
        }while(choice != '3');
        printf("Goodbye %s!!!\n", mem[i].id );
    
    
        return 0;
    }
    
    
    void display()
    {
        printf("1. Log in\n");
        printf("2. Sign up\n");
        printf("3. Exit\n\n");
    }
    
    
    void Up_Date_Info(struct Members mem[20], int i)
    {
    
    
        char choice = '\0';
        char temp[20];
    
    
        printf("1. ID\n");
        printf("2. PASSWORD\n");
        printf("3. FIRST NAME\n");
        printf("4. LAST NAME\n");
        printf("5. EMAIL\n");
        printf("6. GPA\n");
        printf("7. GENDER\n");
        printf("8. AGE\n");
        printf("9. SSN\n");
    
    
        printf("What do you want to update?");
        scanf("%s", &choice);
    
    
        switch(choice)
        {
            case '1':
                printf("Please input your new ID: \n");
                strcpy(temp," ");
                scanf("%s", temp);
                strcpy(mem[i].id, temp);
    
    
                printf("Your new id is %s\n", mem[i].id);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '2':
                printf("Please enter your new password:");
                strcpy(temp," ");
                scanf("%s", temp);
                strcpy(mem[i].pass, temp);
    
    
                printf("Your new password is %s\n", mem[i].pass);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '3':
                printf("Please enter your new first name:");
                strcpy(temp," ");
                scanf("%s", temp);
                strcpy(mem[i].fName, temp);
    
    
                printf("Your new first name is %s\n", mem[i].fName);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '4':
                printf("Please enter your new last name:");
                strcpy(temp," ");
                scanf("%s", temp);
                strcpy(mem[i].lName, temp);
    
    
                printf("Your new last name is %s\n", mem[i].lName);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '5':
                printf("Please enter your new Email:");
                strcpy(temp," ");
                getchar();
                scanf("%s", temp);
                strcpy(mem[i].Email, temp);
    
    
                printf("Your new Email is %s\n", mem[i].Email);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '6':
                printf("Please enter your new GPA:");
                strcpy(temp," ");
                scanf("%s", temp);
                mem[i].GPA = atof(temp);
    
    
                printf("Your new GPA is %f\n", mem[i].GPA);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '7':
                printf("Please enter your new gender:");
                strcpy(temp," ");
                scanf("%c", temp);
                mem[i].gender = temp[0];
    
    
                printf("Your new gender is %c\n", mem[i].gender);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '8':
                printf("Please enter your new age:");
                strcpy(temp," ");
                scanf("%s", temp);
                mem[i].age = atoi(temp);
    
    
                printf("Your new age is %d\n", mem[i].age);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '9':
                printf("Please enter your new SSN:");
                strcpy(temp," ");
                scanf("%s", temp);
                strcpy(mem[i].SSN, temp);
    
    
                printf("Your new SSN is %s\n", mem[i].SSN);
                printf("Update is done!!!\n\n");
                break;
    
    
            default:
                printf("%c is not a valid option at this time.", choice);
                break;
    
    
        }
    }

  4. #19
    Registered User
    Join Date
    Jul 2014
    Posts
    34
    Hello laserlight;
    If you don't mind, where do you live? I live if Ft. Worth Texas.

  5. #20
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    scanf("%s", &choice);

    What type is choice?

    Edit:
    Hint: You likely want %c with a space in front of it.
    The space means skip whitespace like newlines.

    Code:
    scanf(" %c", &choice);
    Tim S.
    Last edited by stahta01; 08-11-2014 at 01:50 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #21
    Registered User
    Join Date
    Jul 2014
    Posts
    34
    Hello All. I wanted to thank everyone that helped me with problems this weekend. I finely got the code to work. I will add it here for those interested.
    Code:
    
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    struct Members
    {
        char fName[20];
        char lName[20];
        char Email[30];
        char id[20];
        char pass[20];
        char SSN[15];
        char gender;
        double Height;
        int age;
    
    
    };
    
    
    void Up_Date_Info(struct Members mem[20],int i);
    void View_Info (struct Members mem[20], int i);
    void display();
    
    
    int main(void)
    {
        int i;
        struct Members mem[20];
        for(i = 0; i < 20; i++)
            {
                strcpy(mem[i].id, " ");
                strcpy(mem[i].pass, " ");
                strcpy(mem[i].Email, "             ");
                strcpy(mem[i].fName, " ");
                strcpy(mem[i].lName, " ");
                strcpy(mem[i].SSN, " ");
            }
        char choice;
        char temp[20];
        char temppass[20] = "";
        i = 0;
    
    
         printf("Start Program\n");
         printf("1. Log in\n");
         printf("2. Sign up\n");
         printf("3. Exit\n");
    
    
        do
        {
    
    
            printf("What do you want to do: ");
            scanf("%c", &choice);
            printf("\n");
    
    
            switch (choice)
            {
                case '1':
                    printf("Please enter your id: ");
                    strcpy(temp, "");
                    scanf("%s", temp);
    
    
                    printf("Please enter your password: ");
                    strcpy(temppass, "");
                    scanf("%s", temppass);
                    getchar();
    
    
                    for (i = 0; i < 20; i++)
                    {
                        if (strcmp(temp, mem[i].id) != 0 || strcmp(temppass, mem[i].pass) != 0)
                        {
                            while ((strcmp(temp, mem[i].id) != 0 || strcmp(temppass, mem[i].pass) != 0))
                            {
                                if (strcmp(temp, mem[i].id) != 0)
                            {
                                printf("Please re-enter your id: ");
                                strcpy(temp, "");
                                scanf("%s", temp);
                                getchar();
                            }
                            if (strcmp(temppass, mem[i].pass) != 0)
                            {
                                printf("Please re-enter your password: ");
                                strcpy(temppass, "");
                                scanf("%s", temppass);
                                getchar();
                            }
                            }
    
    
                            printf("\n");
                        }
    
    
                        if (strcmp(temp, mem[i].id) == 0 && strcmp(temppass, mem[i].pass) == 0)
                        {
                            printf("Hello %s!!!\n\n", mem[i].id);
                            do
                            {
                                printf("1. View User Account\n");
                                printf("2. Update User Account\n");
                                printf("3. Log out\n\n");
                                printf("What do you want to do?");
                                choice = '\0';
                                scanf("%c", &choice);
                                getchar();
                            switch (choice)
                            {
                            case '1':
                                printf("User id is: %s\n", mem[i].id);
                                printf("User password is: %s\n", mem[i].pass);
                                printf("Your first name is %s\n", mem[i].fName);
                                printf("Your last name is %s\n", mem[i].lName);
                                printf("Your Email is %s\n", mem[i].Email);
                                printf("Your Height is %.1f feet.\n", mem[i].Height);
                                printf("Your gender is %c\n", mem[i].gender);
                                printf("Your age is %d\n", mem[i].age);
                                printf("Your SSN is %s\n", mem[i].SSN);
                                printf("\n");
                                break;
    
    
                            case '2':
                                Up_Date_Info(mem, i);
                                getchar();
                                break;
    
    
                            case '3':
                                break;
    
    
                            default:
                                printf("That is not a valid option at this time\n");
                                break;
                            }
                          }while (choice != '3');
                        }
    
    
                        display();
                        break;
                    }
    
    
                    break;
                case '2':
                    printf("Please input user id: ");
                    scanf("%s", temp);
                    for(i = 0; i < 20; i++){
                        if(strcmp(" ", mem[i].id) == 0)
                            {
                            strcpy(mem[i].id, temp);
    
    
                            printf("Please input user password: ");
                            strcpy(temp, "");
                            scanf("%s", temp);
                            strcpy(mem[i].pass, temp);
    
    
                            printf("Please input Email: ");
                            strcpy(temp, "");
                            scanf("%s", temp);
                            strcpy(mem[i].Email, temp);
    
    
                            printf("Please input Height: ");
                            strcpy(temp, "");
                            scanf("%s", temp);
                            mem[i].Height = atof(temp);
    
    
                            printf("Please input first name: ");
                            strcpy(temp, "");
                            scanf("%s", temp);
                            strcpy(mem[i].fName, temp);
    
    
                            printf("Please input last name: ");
                            strcpy(temp, " ");
                            scanf("%s", temp);
                            strcpy(mem[i].lName, temp);
    
    
                            printf("Please input gender: ");
                            strcpy(temp, " ");
                            scanf("%s", temp);
                            mem[i].gender = temp[0];
    
    
                            printf("Please input age: ");
                            strcpy(temp," ");
                            scanf("%s", temp);
                            mem[i].age = atoi(temp);
    
    
                            printf("Please input SSN: ");
                            strcpy(temp, "");
                            scanf("%s", temp);
                            strcpy(mem[i].SSN, temp);
    
    
                            printf("Thank you!!!\n\n");
                            display();
                            break;
                        }
                    }
                    break;
                case '3':
                    break;
                default:
                    printf("%c is not a viable option at this time\n", choice);
                    break;
            }
        }while(choice != '3');
        printf("Goodbye %s!!!\n", mem[i].id );
    
    
        return 0;
    }
    
    
    void display()
    {
        printf("1. Log in\n");
        printf("2. Sign up\n");
        printf("3. Exit\n\n");
    }
    
    
    void Up_Date_Info(struct Members mem[20], int i)
    {
    
    
        char choice = '\0';
        char temp[20];
    
    
        printf("1. ID\n");
        printf("2. PASSWORD\n");
        printf("3. FIRST NAME\n");
        printf("4. LAST NAME\n");
        printf("5. EMAIL\n");
        printf("6. Height\n");
        printf("7. GENDER\n");
        printf("8. AGE\n");
        printf("9. SSN\n");
    
    
        printf("What do you want to update?");
        scanf("%s", &choice);
    
    
        switch(choice)
        {
            case '1':
                printf("Please input your new ID: \n");
                strcpy(temp," ");
                scanf("%s", temp);
                strcpy(mem[i].id, temp);
    
    
                printf("Your new id is %s\n", mem[i].id);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '2':
                printf("Please enter your new password:");
                strcpy(temp," ");
                scanf("%s", temp);
                strcpy(mem[i].pass, temp);
    
    
                printf("Your new password is %s\n", mem[i].pass);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '3':
                printf("Please enter your new first name:");
                strcpy(temp," ");
                scanf("%s", temp);
                strcpy(mem[i].fName, temp);
    
    
                printf("Your new first name is %s\n", mem[i].fName);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '4':
                printf("Please enter your new last name:");
                strcpy(temp," ");
                scanf("%s", temp);
                strcpy(mem[i].lName, temp);
    
    
                printf("Your new last name is %s\n", mem[i].lName);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '5':
                printf("Please enter your new Email:");
                strcpy(temp," ");
                getchar();
                scanf("%s", temp);
                strcpy(mem[i].Email, temp);
    
    
                printf("Your new Email is %s\n", mem[i].Email);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '6':
                printf("Please enter your new Height:");
                strcpy(temp," ");
                scanf("%s", temp);
                mem[i].Height = atof(temp);
    
    
                printf("Your new Height is %.1f feet.\n", mem[i].Height);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '7':
                printf("Please enter your new gender:");
                strcpy(temp," ");
                scanf("%c", temp);
                mem[i].gender = temp[0];
    
    
                printf("Your new gender is %c.\n", mem[i].gender);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '8':
                printf("Please enter your new age:");
                strcpy(temp," ");
                scanf("%s", temp);
                mem[i].age = atoi(temp);
    
    
                printf("Your new age is %d\n", mem[i].age);
                printf("Update is done!!!\n\n");
                break;
    
    
            case '9':
                printf("Please enter your new SSN:");
                strcpy(temp," ");
                scanf("%s", temp);
                strcpy(mem[i].SSN, temp);
    
    
                printf("Your new SSN is %s\n", mem[i].SSN);
                printf("Update is done!!!\n\n");
                break;
    
    
            default:
                printf("%c is not a valid option at this time.", choice);
                break;
    
    
        }
    }
    Thanks again.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good to see that you have made good progress. Now, consider this snippet of your code:
    Code:
    strcpy(temp," ");
    scanf("%s", temp);
    strcpy(mem[i].id, temp);
    You copy " " to temp, then you overwrite temp with input read from standard input, then you copy temp to mem[i].id.

    Sometimes you want to read into a buffer so that you can do validation or format the input before writing it to the destination. In this case, you only blindly copy what was read. Therefore, you can simplify:
    Code:
    scanf("%19s", mem[i].id);
    Notice the %19s instead of just %s in order to avoid buffer overflow. Consider checking the return value of scanf.

    I also suggest that you move the code within the various parts of the big switch statement into separate functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-23-2013, 01:44 PM
  2. Replies: 4
    Last Post: 03-12-2011, 06:59 PM
  3. Replies: 9
    Last Post: 05-28-2010, 10:11 AM
  4. Help on understanding the warning message
    By ssharish2005 in forum C Programming
    Replies: 3
    Last Post: 05-24-2007, 02:03 AM
  5. warning i've never seen
    By linuxdude in forum C Programming
    Replies: 5
    Last Post: 08-30-2006, 01:30 PM