Thread: Error in loop, simple program error, replace int with int

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    122

    Error in loop, simple program error, replace int with int

    Code:
    #include <stdio.h>
    #define LIMIT_2 28
    
    /* j, k, l, m, n, o */
    
    int main (void)
    {
        int p = 0;
    
        char alphabet[LIMIT_2] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'w', 'y', 'z', '\0' };
    
        char array[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', '\0' };
        int k_2 = 61;
        int x = 97;
        int u = 0;
    
        while (p <= 26 && k_2 <= 79) // k_2 <= 79    
        {
            while (x <= 123 && p <= LIMIT_2) // while <= 123
            {
                printf("\n%c\t%d", alphabet[p], k_2);
                p++;
                k_2++;
    
                if (k_2 == 69)
                {
                    while (u <= 5)
                    {
                        printf("\n7%c", array[u]);
                        u++;
                    } 
                }
    
            printf("\t%d\t", x);
            x++;
            }
    
            printf("\n");
        }
    
        return 0;
    }
    Please, what would be the best way to output "6A, 6B, 6C, 6D, 6E, 6F" inside the second column after 69? Any other bugs I will fix by myself.

    https://www.rapidtables.com/code/text/ascii-table.html
    Last edited by Salem; 07-14-2021 at 10:49 PM. Reason: Remove colour tags from code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,714
    The best way is to use the %x format, rather than trying to mung something yourself with a separate loop.

    Code:
    #include <stdio.h>
    
    int main ( ) {
      for ( int c = 'g' ; c <= 's' ; c++ ) {
        printf("%c %d %X %x\n", c, c, c, c);
      }
    }
    
    $ gcc bar.c
    $ ./a.out 
    g 103 67 67
    h 104 68 68
    i 105 69 69
    j 106 6A 6a
    k 107 6B 6b
    l 108 6C 6c
    m 109 6D 6d
    n 110 6E 6e
    o 111 6F 6f
    p 112 70 70
    q 113 71 71
    r 114 72 72
    s 115 73 73
    > char array[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', '\0' };
    You get the same thing with
    char array[] = "ABCDEF";
    Consider that for your whole alphabet string.
    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. Loop Error with program HELP!!!!!
    By TechyTaf in forum C Programming
    Replies: 10
    Last Post: 07-14-2017, 11:58 AM
  2. Loop Error with program HELP!!!!!
    By TechyTaf in forum C Programming
    Replies: 1
    Last Post: 06-26-2017, 12:18 AM
  3. Replies: 5
    Last Post: 02-09-2010, 06:08 PM
  4. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  5. Find th efor loop error (simple!... I assume...)
    By Schmag in forum C++ Programming
    Replies: 2
    Last Post: 06-22-2007, 04:24 PM

Tags for this Thread