D33wakar 36 Posting Whiz in Training

This is a simple hangman game that I wrote using my beginner python skills. Though this game is playable, there must be lots of things I must've been doing wrong or the methods I'm using here may be not so pythonic. So I want some suggestions on how to improve this game.
The 'wordsdict.pkl' file contains a pickled dict with words as keys and meanings as their values.

e.g. {'word': 'its meaning'}
D33wakar 36 Posting Whiz in Training
fflush(stdin); //clear the buffer???!

Don't use 'fflush' with 'stdin' ,see why.also thismight be helpful.
Either use getchar after each call to scanf to discard the '\n' from the input buffer or don't use scanf at all.There are other solutions for interactive inputs.

D33wakar 36 Posting Whiz in Training

If you're using the variable val just to do this:

mov [val], ax
add si,val

then there's no need to do so,just do it directly

add si,ax

It's quite difficult to understand what you're trying to do, the information provided here is too little(not enough) and as far as your sample code is concerned, syntactically it's fine.
Here's an example, I hope this will help .
suppose there's a string defined as

string    db "ABCDEFGH",'$'

and it's characters can be accessed like this

mov bx,offset string
mov al,[bx+si];al = "A"
add si,02
mov al,[bx+si];al="C"

or like this

mov al,[string+si];al="A"
add si,02
mov al,[string+si];al="C"
D33wakar 36 Posting Whiz in Training

I was playing with the code provided by 'hkdani' and the mere idea that 'stdin' uses a char pointer '_ptr' to point to the data in the buffer, I tried to compile the following code(just for fun, cause it's been itching me):

#include <stdio.h>
#include <string.h>
int main(void)
{
    FILE *pFile ;
    int i;
	char name[5];

    pFile = stdin ;
    i = strlen (pFile->_ptr) ;//_ptr is char buffer in struct _iobuf
    printf("%d bytes in stdin\n",i) ;
	printf("enter your name\n");
    scanf("%5[1234]",&name);

   while((i = strlen (pFile->_ptr))>=1){
	   printf("%d bytes in stdin\n",i) ;
	   if(i==1)//atleast 1 character needed for getchar
		   break;
	   printf("removing \'%c\' from the buffer\n",getchar());

	}


    return 0 ;
}

first shows

0 bytes in stdin
enter your name

input:

rakawid

after pressing enter shows:

9 bytes in stdin
removing 'r' from the buffer
8 bytes in stdin
removing 'a' from the buffer
7 bytes in stdin
removing 'k' from the buffer
6 bytes in stdin
removing 'a' from the buffer
5 bytes in stdin
removing 'w' from the buffer
4 bytes in stdin
removing 'i' from the buffer
3 bytes in stdin
removing 'd' from the buffer
2 bytes in stdin
removing '
' from the buffer
1 bytes in stdin

"9 bytes in stdin", what's that extra byte? does anyone know?

vedro-compota commented: +++ +3
hkdani commented: A willing and inquisitive mind that probes the depths to add knowledge. Gems of wisdom will be gleaned from this source. +6
D33wakar 36 Posting Whiz in Training
void print_triangle_info(int area(), int perimeter());
void print_square_info(int area(), int perimeter());

Are 'area' and 'perimeter' functions? If not, why give parentheses after them.the correct form of prototyping will be:

void print_triangle_info(int, int);
void print_square_info(int,int);//takes two integers as arguments and return nothing(void)

and then their definitions

void print_triangle_info(int area, int perimeter) {//no parentheses after area and perimeter
//Now make use of the area and perimeter to print "triangle info"
}

and then call them from 'main'

int main(void) {
//calculate area and perimeter here or create functions to calculate them outside the 'main'

print_triangle_info(area, perimeter);
print_square_info(area, perimeter);
}

edit:
@ WaltP- Wow, that was quick!

minimi commented: Thank you for the heads up! +2
D33wakar 36 Posting Whiz in Training
int ok = 0;
for (x=0;x<=2;x++)
 {
 y=0;
 y++;
  if(c[x][y] ==d[x][y])
  ok = 1;
}

It only compares
c[0][1] with d[0][1]
c[1][1] with d[1][1]
c[2][1] with d[2,1] and so on.
If You want to compare each of the elements then, put it in a nested loop

for(x=0;x<2;x++)
    for(y=0;y<2;y++)
        if(c[x][y]==d[x][y])
        //rest of the code...
D33wakar 36 Posting Whiz in Training
int a,b;
a = b;//bad practice
char c[2][2];
char d[3][3];
c = d;

Well?Why don't you try that in your compiler? the answer is no. you cannot do that with arrays.

// or can you compare two arrays to get a true or false statement?
if ( c == d)

Though the compiler doesn't report any error in this case, you'll not get the intended result from it.Because here, the addresses of arrays c and d are being compared for equality which is never true.

// or if I want to know if two signs in an array are equal:
if ( c[1][2] == d[1][2])

This is the correct form.

Is it possible to use the equal sign = on arrays like you can use it on variables. If yes how do I write it correctly?

Suppose you have an array

int array[5]

then you can assign values to them like this

array[0]=1;//1st address assigned to 1
int i;//will be used as offset address
for(i=1;i<5;i++)
    array[i]=i+1;//2nd address contains 2, 3rd address contains 3 etc.
D33wakar 36 Posting Whiz in Training

Though, the thread has been marked solved, I would like to point few things out.

If you don't want to mess with getting rid of the '\n', then just use gets.

No, never use 'gets()' while taking strings as input(see why).People say don't use 'scanf' because it shows similar behavior to that of 'gets'.'fgets' is much more safer, and you should remove the trailing '\n' character as 'Ancient Dragon' suggested.
And this one is for 'hkdani'.

hkdani commented: Leaves helpful comments giving insights into the intracies of important principles in the programming language. +5
D33wakar 36 Posting Whiz in Training

...that's your homework

I'm sorry that I sounded like your professor up there, but my only intention was to help you find the bug, and it was simple so I thought that you were able to find that on your own.

I'm thinking that your solution was the following for line 9.

printf("%d", matrices[Index][i])

No, it was not.
It was this:

printf("%d",matrices[i]);

Because, the pointer "matrices" you're passing to the function "Display" is equivalent to "matrices[index]" for which you dynamically allocated memory in your 'main'

matrices [Index] = (int *) malloc( size * sizeof(int));

And you only got the first member because 'Index' in your 'Display' function always remains zero.
So you can also do like this:

for (Index = 0; Index <= size; Index++)
{
    printf("%d", matrices[Index]);
}

And also I can't believe that someone who has done several projects is still having trouble understanding the concepts of pointers and then applying them?

D33wakar 36 Posting Whiz in Training

I don't understand - why compiler allows such code =

You can do like this then.

#if defined(_WIN32)||defined(_WIN64)

//Windows specific includes and code goes here..

#elif defined(linux)

//linux specific includes and code goes here..

#endif
D33wakar 36 Posting Whiz in Training

Remove this line

int mainmenu();

from both 'main' and 'winmain'.
and add a prototype of the function mainmenu at the beginning .

...
int mainmenu(void);

int main()
...
vedro-compota commented: ++ +3
D33wakar 36 Posting Whiz in Training

but what about point between words - it's feature of objective programming , but original c is functional language , isn't it?
if "isn't" how can it be implemented without objective style?

Well, WNDCLASS is a structure defined in 'windows.h' and the contents of a structure are accessed with a dot operator, remember!
See here .

vedro-compota commented: +++++ +3
D33wakar 36 Posting Whiz in Training

You might want to add a getchar() after that scanf ,

...
printf("Enter name, price and pages\n");
fgets(b[i].name, 25, stdin);
scanf("%f %d", &b[i].price, &b[i].pages);
getchar();
}
...

After scanf has read your input ,a '\n'(which was typed just after the number) is still left in the input buffer, so we use that getchar to empty the buffer.
scanf() is a bit complex function to handle, especially when taking input interactively from the user. It'll fail to do the job at hand if it doesn't get what it is expecting.Here in your program, user must enter a string followed by a floating point number followed by an integer.If a user fumbled and mistakenly (or intentionally) entered a floating point number instead of an integer, the program will misbehave.
So I'd suggest you to use fgets even also to read the numbers the from user and later convert them using atoi and atof to their respective formats.

D33wakar 36 Posting Whiz in Training

First of all, you're using the if statement in the wrong way(a semicolon before the body)

if (t != NULL);
{
printf("%s\n",line);// here you should use "t" instead of "line"
 
 
 
}

Strtok separates the string in tokens separated by a delimiter string (here " "). Printing the "t" will print the first token at first.

fgets (line, sizeof (line), fp);
char* t=strtok(line," ");
if (t != NULL)
    printf("%s\n",t);

calling strtok for the second time (with NULL as the first argument if you're reading from the same string) will print the second token and so on.If there are no tokens left it will return NULL.

fgets (line, sizeof (line), fp);
char* t=strtok(line," ");/*in the first call the first argument should not be NULL*/
if (t != NULL)
    printf("%s\n",t);
t=strtok(NULL," ");
if (t != NULL)
    printf("%s\n",t);
t=strtok(NULL," ");
if (t != NULL)
    printf("%s\n",t);

So to achieve the output you want you have to do like this:

...
if (fp == NULL)
    perror ("error opening file");
else {
    while(fgets (line, sizeof (line), fp)){
        t=strtok(line," ");
        printf("\n");
        while( t != NULL ) {
            printf( "%s\t",t);
            t = strtok( NULL, " " );
        }
    }
}
...
Ancient Dragon commented: good info +17
D33wakar 36 Posting Whiz in Training

It's because of this line

int a[n];

You cannot declare arrays like that.
Here either 'n' should be initialized prior to declaring array

printf("Enter size:");
scanf("%d",&n);
int a[n];

or n should be replaced by some constant value.
like this:

#define BUFFER 100
...
int a[BUFFER];
challarao commented: Thank you +2
D33wakar 36 Posting Whiz in Training

It would have been better to understand your problem if you had posted your code here.
Sure I'll help.