Thread: How can I display a print function x amount of times?

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    1

    How can I display a print function x amount of times?

    Hey, im new to C and i'd like to know how I can display the information stored in an array.

    How can the code work so that if only 3 slots (for example) in the array are stored with information, the info stored in a[4], and a[5] will not print.

    I'm aware of the concepts of for loops, however I cant seem to figure out how to limit how many messages are displayed depending on how many slots in the array contain user stored information (as mentioned above).

    Code:
                         
    
    printf("           %d-%d-%d  %lf\n", a[1].day, a[1].month, a[1].year, a[1].GPA);                    
    printf("           %d-%d-%d  %lf\n", a[2].day, a[2].month, a[2].year, a[2].GPA);
    printf("           %d-%d-%d  %lf\n", a[3].day, a[3].month, a[3].year, a[3].GPA);                    
    printf("           %d-%d-%d  %lf\n", a[4].day, a[4].month, a[4].year, a[4].GPA);                    
    printf("           %d-%d-%d  %lf\n", a[5].day, a[5].month, a[5].year, a[5].GPA);
    Thanks

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    There is no real shortcut for knowing.

    In your program, you probably have a place where this data is input into a. There you'll want to increment a special counter for you to use later for situations like the one you have now.

    It might look like this:
    Code:
    int count = 0;
    int flag = 1;
    for (i = 0; flag && i < ARRAY_SIZE(a); ++i)
    {
       printf("Enter birthday #%d dd-mm-yyyy.\n", i + 1);
       scanf("%d-%d-%d", &a[i].day, &a[i].month, &a[i].year);
       printf("Enter a GPA.\n");
       scanf("%lf", &a[i].GPA);
       getchar();
    
       printf("Continue y/n?\n")'
       flag = getchar();
       if (flag == 'n')
          flag = 0;
       else
          flag = 1;
    }
    count = i;
    Now you have the variable count, which could be 3 or 5 or whatever. You could use it in place of the ARRAY_SIZE() macro.
    Last edited by whiteflags; 04-15-2017 at 10:03 PM.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    Or maybe
    Code:
    int i = 1; // 1 because that’s the first number listed in your example code
    while( a[i] ) {
    	printf("           %d-%d-%d  %lf\n", a[i].day, a[i].month, a[i].year, a[i].GPA);
    	i++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to print specific amount of characters per line
    By Yashiiro in forum C Programming
    Replies: 4
    Last Post: 06-13-2016, 11:52 AM
  2. Print character x times based on user's input
    By Kecupochren in forum C Programming
    Replies: 5
    Last Post: 12-21-2012, 12:03 PM
  3. Replies: 15
    Last Post: 06-13-2012, 12:34 PM
  4. Print name "x" times using command line arguments
    By zekiegypt in forum C Programming
    Replies: 3
    Last Post: 04-20-2011, 11:32 AM
  5. how to print big amount of number?
    By zzatan in forum C Programming
    Replies: 9
    Last Post: 10-02-2009, 07:46 PM

Tags for this Thread