Like Tree4Likes

character converter

This is a discussion on character converter within the C Programming forums, part of the General Programming Boards category; I suppose you didn't understand what I want to do.. I want to get a letter from the keyboard and ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    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
    2,217
    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,675
    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
    2,217
    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
    2,217
    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
    237
    Is there a reason why you can't just use

    Code:
    x = input_char - 'A' + 1;
    iMalc and stahta01 like this.

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,400
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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...

Page 2 of 3 FirstFirst 123 LastLast
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, 02:34 PM
  3. Key Converter
    By Insenic in forum C Programming
    Replies: 5
    Last Post: 02-02-2006, 11:17 PM
  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, 04:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21