Thread: Calculator using mouse, need help with a bug while dragging the mouse

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    2

    Calculator using mouse, need help with a bug while dragging the mouse

    Hi, I have an assignment to do a calculator in C, wich will work by clicking the numbers with the mouse, however i have a bug that when i hold the mouse and dragg it, it assumes that I'm clicking multiple times, also, when I click in an empty space the program creates a blank space on the calculator screen.
    I would really like some help on that, here goes the code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include "..\MouseHandler.h" // ajustar o caminho ao seu projecto
    #include "..\calcSkinLib.h"  // ajustar o caminho ao seu projecto
    #define PI = 3.1415;
    
    
    
    
    
    
    
    
    char tecla(struct mouseClick evento, HANDLE wHnd);
    
    
    
    
    int main(int argc, char *argv[]) {
        HANDLE rHnd, wHnd;  //Handles for reading and writing to the console.
        struct mouseClick evento;
    
    
    
    
        if (!initializeControls(&rHnd, &wHnd)) {    //must call before doing anything
            printf("ERROR WHILE INITIALIZING\n"); //if couldn't initialize
            exit(0);
        }
    
    
        printCalcMask(wHnd);
        locate(79, 24, wHnd); printf("\xb2");
        locate(0, 0, wHnd); printf("        clique em no ecr\xc6 (em [24, 79] para encerrar)");
    
    
        do {
    
    
            char num1[20]; char opr;
            double n1 = 0; double n2 = 0; double rslt = 0;
            char mClick;
            int l1 = 0;
    
    
            
            do{
    
    
                evento = readMouse(rHnd);
                locate(0, 0, wHnd);
                mClick = tecla(evento, wHnd);
    
    
                locate(25 + l1, 6, wHnd);
                switch (mClick){
                case '0':num1[l1] = '0'; /*locate(35 + l1, 6, wHnd);*/ printf("0"); break;
                case '1':num1[l1] = '1'; /*locate(35 + l1, 6, wHnd);*/ printf("1"); break;
                case '2':num1[l1] = '2'; /*locate(35 + l1, 6, wHnd);*/ printf("2"); break;
                case '3':num1[l1] = '3'; /*locate(35 + l1, 6, wHnd);*/ printf("3"); break;
                case '.':num1[l1] = '.'; /*locate(35 + l1, 6, wHnd);*/ printf("."); break;
                case '4':num1[l1] = '4'; /*locate(35 + l1, 6, wHnd);*/ printf("4"); break;
                case '5':num1[l1] = '5'; /*locate(35 + l1, 6, wHnd);*/ printf("5"); break;
                case '6':num1[l1] = '6'; /*locate(35 + l1, 6, wHnd);*/ printf("6"); break;
                case '7':num1[l1] = '7'; /*locate(35 + l1, 6, wHnd);*/ printf("7"); break;
                case '8':num1[l1] = '8'; /*locate(35 + l1, 6, wHnd);*/ printf("8"); break;
                case '9':num1[l1] = '9'; /*locate(35 + l1, 6, wHnd);*/ printf("9"); break;
                case 'C':for (int j = 0; j <= 20; j++){ num1[j] = ' '; }l1 = 0; n1 = 0; n2 = 0; locate(25, 6, wHnd); printf("\t\t\t     "); break;
                case 'p':num1[0] = '3'; num1[1] = '.'; num1[2] = '1'; num1[3] = '4'; num1[4] = '1'; num1[5] = '5';
                    locate(25, 6, wHnd); printf("\t\t\t"); locate(25 + l1, 6, wHnd); printf(" pi"); l1 = 0; break;
    
    
    
    
                case '-':{ opr = '-'; locate(25, 6, wHnd); printf("\t\t\t     ");
                    if (n2 == 0) {
                        n2 = n1;
                        for (int j = 0; j <= 20; j++){
                            num1[j] = ' ';
                        }
                    }
    
    
                    l1 = 0; } break;
    
    
    
    
                case '+':{ opr = '+'; locate(25, 6, wHnd); printf("\t\t\t     ");
                    if (n2 == 0) {
                        n2 = n1;
                        for (int j = 0; j <= 20; j++){
                            num1[j] = ' ';
                        }
                    }
                    
    
    
                    l1 = 0; } break;
    
    
    
    
    
    
                case '*':{ opr = '*'; locate(25, 6, wHnd); printf("\t\t\t     ");
                    if (n2 == 0) {
                        n2 = n1;
                        for (int j = 0; j <= 20; j++){
                            num1[j] = ' ';
                        }
                    }
    
    
                    l1 = 0; } break;
    
    
                case '/':{ opr = '/'; locate(25, 6, wHnd); printf("\t\t\t     ");
                    if (n2 == 0) {
                        n2 = n1;
                        for (int j = 0; j <= 20; j++){
                            num1[j] = ' ';
                        }
                    }
    
    
                    l1 = 0; } break;
    
    
    
    
    
    
    
    
                case '=':
    
    
                    switch (opr){
    
    
                    case '+':locate(25, 6, wHnd); printf("\t\t\t"); locate(25, 6, wHnd); n2 += n1; printf("%.4f", n2);
                        for (int j = 0; j <= 20; j++){ num1[j] = ' '; }l1 = 0; break;
    
    
                    case '-':locate(25, 6, wHnd); printf("\t\t\t"); locate(25, 6, wHnd); n2 = n2 - n1; printf("%.4f", n2); l1 = 0;
                        for (int j = 0; j <= 20; j++){ num1[j] = ' '; } break;
    
    
                    case '*':locate(25, 6, wHnd); printf("\t\t\t"); locate(25, 6, wHnd); n2 = n2 * n1; printf("%.4f", n2); l1 = 0;
                        for (int j = 0; j <= 20; j++){ num1[j] = ' '; } break;
    
    
                    case '/':locate(25, 6, wHnd); printf("\t\t\t"); locate(25, 6, wHnd); n2 = n2 / n1; printf("%.4f", n2); l1 = 0;
                        for (int j = 0; j <= 20; j++){ num1[j] = ' '; } break;
                    }
    
    
    
    
                    //if (opr == '+'){ locate(36, 6, wHnd); printf("\t\t"); locate(36, 3, wHnd); n2+=n1; printf("%.4f",n2); }
                    //    else if (opr == '-'){ locate(36, 6, wHnd); printf("\t\t"); locate(36, 3, wHnd); n2 =n2- n1; printf("%.4f", n2); }
    
    
    
    
    
    
                }//switch
    
    
    
    
                
                if
                    (l1 <= 15)
                {
                    l1++;
                }
                n1 = atof(num1);
            } while (!(mClick == 'o'));
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        } while (!(evento.xPos == 79 && evento.yPos == 24));
        locate(0, 1, wHnd);
        printf("Pressione uma tecla para fechar...\x07"); _getch();
        return EXIT_SUCCESS;
    }
    char tecla(struct mouseClick evento, HANDLE wHnd){
        if (evento.xPos >= 30 && evento.xPos <= 32 && evento.yPos == 10) return 'x'; //exit
        if (evento.xPos >= 35 && evento.xPos <= 37 && evento.yPos == 10) return 'f';//fix
        if (evento.xPos >= 39 && evento.xPos <= 41 && evento.yPos == 10) return 'b';//back
        if (evento.xPos >= 44 && evento.xPos <= 47 && evento.yPos == 10) return 'c';//ce
        if (evento.xPos >= 49 && evento.xPos <= 51 && evento.yPos == 10) return 'C';//c
        if (evento.xPos >= 26 && evento.xPos <= 28 && evento.yPos == 12) return 'i';//inv
        if (evento.xPos >= 30 && evento.xPos <= 32 && evento.yPos == 12) return 's';//sin
        if (evento.xPos >= 34 && evento.xPos <= 36 && evento.yPos == 12) return 'o';//cos
        if (evento.xPos >= 38 && evento.xPos <= 40 && evento.yPos == 12) return 't';//tan
        if (evento.xPos >= 42 && evento.xPos <= 43 && evento.yPos == 12) return 'n';//ln
        if (evento.xPos >= 45 && evento.xPos <= 47 && evento.yPos == 12) return 'l';//log
        if (evento.xPos >= 49 && evento.xPos <= 51 && evento.yPos == 12) return 'e';//exp
        if (evento.xPos >= 26 && evento.xPos <= 28 && evento.yPos == 14) return '7';//7
        if (evento.xPos >= 30 && evento.xPos <= 32 && evento.yPos == 14) return '8';//8
        if (evento.xPos >= 34 && evento.xPos <= 36 && evento.yPos == 14) return '9';//9
        if (evento.xPos >= 38 && evento.xPos <= 40 && evento.yPos == 14) return '/';// /
        if (evento.xPos >= 42 && evento.xPos <= 45 && evento.yPos == 14) return 'm';// mc
        if (evento.xPos >= 47 && evento.xPos <= 51 && evento.yPos == 14) return 'q';//raiz
        if (evento.xPos >= 26 && evento.xPos <= 28 && evento.yPos == 16) return '4';//4
        if (evento.xPos >= 30 && evento.xPos <= 32 && evento.yPos == 16) return '5';//5
        if (evento.xPos >= 34 && evento.xPos <= 36 && evento.yPos == 16) return '6';//6
        if (evento.xPos >= 38 && evento.xPos <= 40 && evento.yPos == 16) return '*';//*
        if (evento.xPos >= 42 && evento.xPos <= 44 && evento.yPos == 16) return 'M';//mr
        if (evento.xPos >= 47 && evento.xPos <= 49 && evento.yPos == 16) return 'p';//pi
        if (evento.xPos >= 26 && evento.xPos <= 28 && evento.yPos == 18) return '1';//1
        if (evento.xPos >= 30 && evento.xPos <= 32 && evento.yPos == 18) return '2';//2
        if (evento.xPos >= 34 && evento.xPos <= 36 && evento.yPos == 18) return '3';//3
        if (evento.xPos >= 38 && evento.xPos <= 40 && evento.yPos == 18) return '-';//-
        if (evento.xPos >= 42 && evento.xPos <= 45 && evento.yPos == 18) return '&';//ms
        if (evento.xPos >= 47 && evento.xPos <= 51 && evento.yPos == 18) return 'r';//rad
        if (evento.xPos >= 26 && evento.xPos <= 28 && evento.yPos == 20) return '.';// .
        if (evento.xPos >= 30 && evento.xPos <= 32 && evento.yPos == 20) return '0';//0
        if (evento.xPos >= 34 && evento.xPos <= 36 && evento.yPos == 20) return '=';// =
        if (evento.xPos >= 38 && evento.xPos <= 40 && evento.yPos == 20) return '+';// +
        if (evento.xPos >= 42 && evento.xPos <= 45 && evento.yPos == 20) return '#';//m+
        if (evento.xPos >= 47 && evento.xPos <= 51 && evento.yPos == 20) return 'd';//deg
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,541
    > char tecla(struct mouseClick evento, HANDLE wHnd);
    Look in evento to see what the button states are.
    Make tecla return a value when the button is pressed (or released).

    Perhaps modify the function like so
    char tecla(struct mouseClick evento, HANDLE wHnd, unsigned int *buttonStates);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    2
    Quote Originally Posted by Salem View Post
    > char tecla(struct mouseClick evento, HANDLE wHnd);
    Look in evento to see what the button states are.
    Make tecla return a value when the button is pressed (or released).

    Perhaps modify the function like so
    char tecla(struct mouseClick evento, HANDLE wHnd, unsigned int *buttonStates);

    Hi, thanks for replying, i'm not good programmer, i'm only in the beggining, I tried what you said but it didn't work, an error appeared: "Error 1 error C2660: 'tecla' : function does not take 2 arguments"
    I think if I find a way for it to return a value only when the mouse is released that my problem is solved, but how do i do that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic-tac-toe using mouse
    By aaa111 in forum C Programming
    Replies: 1
    Last Post: 08-30-2009, 09:33 AM
  2. Identifying mouse used with Global mouse hooks
    By Chillance in forum Windows Programming
    Replies: 4
    Last Post: 02-15-2009, 08:42 AM
  3. do it by mouse
    By planet_abhi in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-16-2003, 09:27 AM
  4. mouse
    By stilllearning in forum C Programming
    Replies: 0
    Last Post: 04-15-2003, 03:34 PM
  5. Mouse help
    By Magos in forum C Programming
    Replies: 3
    Last Post: 12-03-2001, 05:45 AM

Tags for this Thread