Thread: Can't get strings to print when using for loop.

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    2

    Question Can't get strings to print when using for loop.

    Hello! I am making a program that takes the user's family names, their ages, and where they live. At the end I will be eventually averaging out their age and also printing the names of anyone who lives in Kansas.

    I can't seem to get the Kansas part to work properly though.. When I execute the code, everything else works perfectly, but the Kansas part doesn't even print. I'm not sure exactly what I'm doing wrong. Is there something different I need to do when strings are involved rather than integers/floats? Thanks in advance!

    Code:
    #include <stdio.h>
    int main ()
    {
      /* variable definition: */
      char familyMember[60], familyMemberName[60], state[20], stateData[20];
      float userFinished=1, age, ageData[5];
      int count=0, count2=0, i, j;
       
       // Input Data
       while (userFinished > 0)
       {
        printf("Enter family member:\n");
        scanf("%s", &familyMember);
        printf("Enter age of family member:\n");
        scanf("%f", &age);
            ageData[count]=age;
            count=count+1;
        printf("Enter the state where they live:\n");
        scanf("%s", &state);
        if (state=="Texas" || state=="texas")
        {
            familyMemberName[count2];
            count2= count2 + 1;
         }
        printf("Would you like to continue entering names? Enter 1 for yes, and 0 for no\n");
        scanf("%d", &userFinished);
        } 
    
       // Print data
        for (j=0; j<count; j++)
        printf ("The age of your family is %f\n", ageData[j]);
       
       for (i=0; i<count2; i++)
        printf ("Family member %s was born in Texas!\n", familyMemberName[i]);
       
    
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I haven't really look deeply at the code, but ran it and got some warnings.

    Code:
    /*
    main.c||In function 'main':|
    main.c|13|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[60]'|
    main.c|19|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[20]'|
    main.c|20|warning: comparison with string literal results in unspecified behavior|
    main.c|20|warning: comparison with string literal results in unspecified behavior|
    main.c|22|warning: statement with no effect|
    main.c|26|warning: format '%d' expects type 'int *', but argument 2 has type 'float *'|
    main.c|5|warning: unused variable 'stateData'|
    ||=== Build finished: 0 errors, 7 warnings ===|
    */
    Did you get any of these? If not, you need to find out how to maximize your compiler warnings.

    I recommend fixing these things and seeing if you get closer to a working implementation.

    One thing that stuck out with the warnings:

    Code:
    if (state=="Texas" || state=="texas")
    You can't compare strings like this in C. You need to use the "strcmp()" function from "string.h".

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    scanf("%s", &familyMember);
    familyMember is an array of 60 chars, so it can store a string of a maximum length of 59. Furthermore, an array is converted to a pointer to its first element, so you should not be taking its address here. Therefore you should have written:
    Code:
    scanf("%59s", familyMember);
    Also, remember to check the return value of scanf. Note that this mistake happens again when reading into state.

    This is wrong:
    Code:
    if (state=="Texas" || state=="texas")
    The use of operator== here results in pointer comparison, not string comparison. A correct approach is to #include <string.h> and use strcmp, e.g.,
    Code:
    if (strcmp(state, "Texas") == 0 || strcmp(state, "texas") == 0)
    Oh, and please indent your code more consistently. You have made some effort, which is good, but you should be consistent.
    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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    2
    Quote Originally Posted by Salem View Post
    Lol am I not allowed to get help from multiple sources? I don't see how that is wasting anyone's time.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackvoay2
    Lol am I not allowed to get help from multiple sources? I don't see how that is wasting anyone's time.
    It potentially wastes time because it means that people might end up duplicating effort, e.g., by providing you an answer that was already given elsewhere. Pose your question to only one channel, then if you have not received a satisfactory reply after some time, pose it to another channel, with a link to your question in the previous channel so that users responding in this second channel can see for themselves what was already proposed to you and failed to help you. It would also be polite to inform the users of the previous channel that you have moved on elsewhere, e.g., with a link.
    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

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print hexadecimal strings from binary
    By Mestertik in forum C++ Programming
    Replies: 8
    Last Post: 08-21-2013, 04:28 AM
  2. How to print this using for loop?
    By passdot in forum C Programming
    Replies: 3
    Last Post: 07-10-2012, 12:47 AM
  3. loop help print this
    By gnomelook in forum C Programming
    Replies: 4
    Last Post: 11-17-2011, 10:48 PM
  4. How do I print out a two dimensional array of strings?
    By nellosmomishot in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 03:47 PM
  5. Print from 1 to 10 without using a loop
    By Guti14 in forum C++ Programming
    Replies: 11
    Last Post: 12-23-2003, 10:02 PM

Tags for this Thread