1. 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. I suppose you didn't understand my answer to the question then. I basically gave it to you.

3. Thanks man!

4. 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. 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. 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. Originally Posted by whiteflags
Are you guys sure that enums will work?
Probably yes. But I like your approach more

8. Well?

9. 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. 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. Originally Posted by Sotiris Kaniras
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. 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. Is there a reason why you can't just use

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

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

15. Originally Posted by gemera
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...