Thread: Printing an index question

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    Exclamation Printing an index question

    I have project in my class where I have to print a "coded message". The user is to input numbers that represent an alphabet array, then I need to convert those numbers into the letters of the array. Any help would be greatful as I have been beating my head on my keyboard for a couple of days now. Here is my code:

    Code:
    #include <stdio.h>
    
    
    int main(void){
        
        char alphabet[] = "0ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        char entry[15];
        size_t i, j;
        
        printf("%s%25s\n", "Alpha Number", "Alpha Letter\n");
        
        for(i = 1; i < 53; ++i){
            printf("%12u%22c\n", i, alphabet[i]);
        }
        
        printf("Type a coded message.  Separate whole numbers with a comma:\n");
        scanf("%14s", entry);
        
        printf("%s%25s\n", "Alpha Number", "Alpha Letter\n");
        
        for(j = 0; j < 15; ++j){
    
    
        
        for(i = 1; i < 53; ++i){
            
                if(entry[j] == ','){
                    ++entry[j];
                }
                if(alphabet[j] == entry[i]){
                    printf("%12c%22u\n", entry[j], alphabet[entry[j]]);
    
    
                }//End IF
            }//End inner loop
            if(entry[j] == '\0'){
                j = 999;
            }//End if
        }//End outer loop
        printf("Your coded message is:\n%c", alphabet[entry[i]]);
    }//End program

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Save us some time, and post up a coded message, and what the final output should be, and what your program is currently giving for that input.

    That will help us zoom in on the problem easier.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Are you sure you understand the assignment? Because I have to say, this is one of the most useless assignments I've seen. User is forced to enter digits and commas as some kind of encoded message - except the encoding is only A-z as 1-52?

    Anyway, assuming that's what you want to do.
    Code:
    if(alphabet[j] == entry[i])
    You are comparing successive Alphabetical characters to your user-entered Digit characters. When will that ever be equal?
    Is it your intention that if a user entered 1, that would map to 'A'? If so, you need to convert your string of integers and commas to a series of actual integers. Then you'd use the value as index to your alpha array.

    Are you required to read the user input as a string? If not, I'd strongly recommend reading as integers into an array.

    Also, don't printf string literals using conversion specifiers, just
    Code:
    printf("Alpha Number          Alpha Letter\n";

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    This assignment is based on one we did previously where the user is supposed to enter a character and the output would print two columns, the index number and the letter the user typed. My thought process was that I could just reverse the process, but I haven't figured that part out. The commas aren't required. User input as a string isn't required, the only requirement is that the user types a message using 1-52 and the output will be two columns one with the numbers entered, one with the associating letters, then the message the user typed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-04-2012, 09:03 PM
  2. Replies: 1
    Last Post: 08-12-2011, 03:07 AM
  3. Array Index: A simple question?
    By logicwonder in forum C Programming
    Replies: 18
    Last Post: 01-06-2006, 03:26 AM
  4. ? about this> index::index () : type_intex()
    By bobk544 in forum C++ Programming
    Replies: 5
    Last Post: 09-30-2005, 02:59 PM
  5. PHP simple question : undefined index
    By FloatingPoint in forum Tech Board
    Replies: 5
    Last Post: 07-16-2003, 07:01 AM

Tags for this Thread