Thread: character converter

  1. #16
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    I suppose you didn't understand what I want to do..
    I want to get a letter from the keyboard and convert it into its equal number.. A to 1, B to 2 ... Z to 26..
    (Without using getchar() )

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I suppose you didn't understand my answer to the question then. I basically gave it to you.

  3. #18
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Thanks man!

  4. #19
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Hi again.. I have this code:

    Code:
    for (k = 0; k < 4; k++)
          if ((comp4 = strcmp(&command[k], "play")) == 0) {
              comp5 = 2;
         }
         if (comp5 == 2) {
             for (k = 0; k < 26; k++)
                   if (tolower(command[5]) == "abcdefghijklmnopqrstuvwxyz"[k]) {
                       x = k;
                       printf("\n\nx = %d\n\n", x);
                   }
        }
    and whatever character i give, it shows x = 12.. why?

  5. #20
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It would be easier to help you if you put that code into a simple "main()" function for us, that still demonstrates the problem you're seeing.

  6. #21
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include "hfiles.h"
    
    
    int main(int argc, char *argv[])
    {
        int k, j, x = -1, size, player;
        char dif, **move, who, *command, y = -1;
        char comp, comp2, comp3, comp4, comp5;
    
    
        comp = 1;
        comp2 = 1;
        comp3 = 1;
        comp4 = 1;
        comp5 = 1;
        size = 11;
        dif = 1;
        who = 0;
        player = 1;
        for (k = 1; k < argc; k++) {
            comp = strcmp(argv[k], "-n");
            comp2 = strcmp(argv[k], "-d");
            comp3 = strcmp(argv[k], "-b");
            comp4 = strcmp(argv[k], "-s");
            if (!comp) {
                size = atoi(argv[k+1]);
            }
            if (!comp2) {
                dif = atoi(argv[k+1]);
            }
            if (!comp3) {
                who = 1;
            }
        }
    
    
        if ((move = malloc(size * sizeof(char *))) == NULL) {
             printf("\n\nCould not allocate memory!");
             return 0;
        }
        for (k = 0; k < size; k++) {
            if ((*(move+k) = malloc(size * sizeof(char))) == NULL) {
                 printf("\n\nCould not allocate memory!");
                 return 0;
            }
        }
    
    
        if ((command = malloc(size * sizeof(char))) == NULL) {
             printf("\n\nCould not allocate memory!");
             return 0;
        }
    
    
        if (who) {
            printf("> ");
            scanf("%s", command);
            if (((comp5 = strcmp(command, "quit"))) == 0) {
                for (k = 0; k < size; k++)
                     free(*(move+k));
                free(move);
                free(command);
                return 0;
            }
    
    
            if (((comp5 = strcmp(command, "newgame"))) == 0) {
                while (player <= size*size) {
                     showstate(size, move);
                     if (player % 2 != 0) {
                         printf("\n\nWhite player (computer) plays now\n> ");
                         scanf("%s", command);
                         if (((comp5 = strcmp(command, "quit"))) == 0) {
                             for (k = 0; k < size; k++)
                                  free(*(move+k));
                             free(move);
                             free(command);
                             return 0;
                          }
                          player++;
                     }
                     else {
                        printf("\n\nBlack player (human) plays now\n> ");
                        scanf("%s", command);
                        if (((comp5 = strcmp(command, "quit"))) == 0) {
                             for (k = 0; k < size; k++)
                                  free(*(move+k));
                             free(move);
                             free(command);
                             return 0;
                        }
                        for (k = 0; k < 4; k++)
                            if ((comp4 = strcmp(&command[k], "play")) == 0) {
                                comp5 = 2;
                                printf("\n\n%d", comp5);
                            }
                        if (comp5 == 2) {
                            for (k = 0; k < 26; k++)
                                 if (tolower(command[5]) == "abcdefghijklmnopqrstuvwxyz"[k]) {
                                     x = k;
                                     y = atoi(&command[6]);
                                     printf("\n\nx = %d, y = %d\n\n", x, y);
                                    // break;
                                 }
                        }
                        player++;
                     }
               }
            }
        }
        else {
            printf("> ");
            scanf("%s", command);
            if (((comp5 = strcmp(command, "quit"))) == 0) {
                for (k = 0; k < size; k++)
                     free(*(move+k));
                free(move);
                free(command);
                return 0;
            }
            while (player <= size*size) {
                   showstate(size, move);
                   if (player % 2 != 0) {
                       printf("\n\nWhite player (human) plays now\n> ");
                       scanf("%s", command);
                       if (((comp5 = strcmp(command, "quit"))) == 0) {
                            for (k = 0; k < size; k++)
                                 free(*(move+k));
                            free(move);
                            free(command);
                            return 0;
                       }
    
    
                       player++;
                       if ((comp4 = strcmp(&command[k], "play")) == 0) {
                           for (k = 0; k < 26; k++)
                                if (tolower(command[k]) == "abcdefghijklmnopqrstuvwxyz"[k]) {
                                    x = k;
                                    y = command[6];
                                    printf("\n\nx = %d, y = %d\n\n", x, y);
                                    break;
                                }
                       }
                   }
                   else {
                       printf("\n\nBlack player (computer) plays now\n> ");
                       scanf("%s", command);
                       if (((comp5 = strcmp(command, "quit"))) == 0) {
                            for (k = 0; k < size; k++)
                                 free(*(move+k));
                            free(move);
                            free(command);
                            return 0;
                       }
                       player++;
                   }
            }
        }
        printf("\n\n");
    
    
        for (k = 0; k < size; k++)
            free(*(move+k));
        free(move);
        free(command);
    
    
        return 0;
    }

  7. #22
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by whiteflags View Post
    Are you guys sure that enums will work?
    Probably yes. But I like your approach more
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #23
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Well?

  9. #24
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Are you going to tell us what command line arguments and inputs you give to the program that will illustrate the problem you're seeing?

    Or are we supposed to dig through the code to figure out which commands the program expects, and guess as to which ones will lead us to the problem?

  10. #25
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Or you could just ask me nicely, instead of being ironic..
    anyway.. ignore the command line arguments.. the input must be for example "play E4"... So, the program has to save the 4 to variable y and convert E to digit.. (5) and then save it to variable x..

  11. #26
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Quote Originally Posted by Sotiris Kaniras View Post
    Or you could just ask me nicely, instead of being ironic..
    anyway.. ignore the command line arguments.. the input must be for example "play E4"... So, the program has to save the 4 to variable y and convert E to digit.. (5) and then save it to variable x..
    I realised that the problem is caused because of the "space" character.. Why?

  12. #27
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I'm having a very difficult time understanding how this code is supposed to work, so I can't really give any concrete advice. Also, we don't have "hfiles.h" so that limits what other people are able to test.

    In answer to your last question, if you want to enter "play E4", then you can't use "scanf()" (at least not without some trickery) because it will stop reading at the first whitespace (the space, in this case). If you want to get and parse a string, "fgets()" would probably be better for your purposes.

    --------

    Some constructive criticm of your code:

    - Take time to plan out the program before you start writing. It looks like you just kept adding things as ideas came to you.

    - There is also a lot of redundant code that could be placed in separate functions to make your program shorter, clearer, and easier to read.

    - You should use more descriptive variable names - "comp", "comp2", etc tell us little about what those variables are supposed to be used for.

    --------

    Also, what appears to be your string parsing routine seems terribly broken. I'm really not sure what you're trying to do, so I'll just give some comments on what I can see at a glance.

    Code:
    if ((comp4 = strcmp(&command[k], "play")) == 0) {
        for (k = 0; k < 26; k++)
            if (tolower(command[k]) == "abcdefghijklmnopqrstuvwxyz"[k]) {
                x = k;
                y = command[6];
                printf("\n\nx = %d, y = %d\n\n", x, y);
                break;
            }
    }
    Line 1: "command" is a string, so this line probably isn't doing what you think it is (note that your string comparison against "quit" is done correctly).

    Lines 2 and 3: "command" is only given enough space for "size" characters [malloc(size * sizeof(char)]. Size is set to 11. You're trying to reference 26 characters, however.

    --------

    When someone is new to programming, it's normal to stuff everything in "main()". However, when programs begin to grow in size and complexity, it is necessary to break down functionality into separate functions, and strive for code clarity. It seems you're at this point now, so I recommend you get a grasp of good coding techniques (modularity, etc) before going any further.

  13. #28
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Is there a reason why you can't just use

    Code:
    x = input_char - 'A' + 1;

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gemera
    Is there a reason why you can't just use
    Code:
    x = input_char - 'A' + 1;
    Perhaps because the letters of the English alphabet are not guaranteed to be contiguous in value.
    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

  15. #30
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Quote Originally Posted by gemera View Post
    Is there a reason why you can't just use

    Code:
    x = input_char - 'A' + 1;
    Because the user, among other commands, he has to write play E4 not just E...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with hex to dec converter ??
    By XWR-AiA in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2010, 06:23 PM
  2. CD to MP3 Converter
    By Dark_Phoenix in forum Tech Board
    Replies: 5
    Last Post: 12-14-2006, 03:34 PM
  3. Key Converter
    By Insenic in forum C Programming
    Replies: 5
    Last Post: 02-03-2006, 12:17 AM
  4. Help with Error - Binary to Character Converter
    By dvldrmmr in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 01:21 PM
  5. c to c++ converter
    By kashifk in forum C Programming
    Replies: 1
    Last Post: 04-01-2003, 05:38 PM