I have problems in how can i use the backspace key work in deleting characters in real time. I have a program where you can type a string no more than 60characters but say i don't want the last character and want to delete it, if i hit backspace it moves the cursor back but the character is still there. I know how to erase the character in the array everytime you hit backspace but how do you show it in real time?????? below is my code for my program.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <iostream.h>


void instruct(void);
void classify(char cInput[], float fstats[], int i, int iOdd, int iEven, int iUpper,
             int iLower, int iOaverage, int iEaverage);
void summary(float fstats[], int i, int iOdd, int iEven, int iUpper,
             int iLower, int iOaverage, int iEaverage);
int  count(char cChoice, char cInput[]);
int  search(char cGet, char cInput[]);
void menu(void);


void main(void)
{

    int iOdd=1, iEven=2;
    int i=0, iCheck=0, j;
    int iUpper=3, iLower=4;
    int iOaverage =5, iEaverage=6;
    char cInput [61] = {"0"};
    float fstats[7]={0,0,0,0,0,0,0};
    char cPick , cChoice, cGet;


    menu();
    cPick = getche();

    for (;cPick != '6'; )
    {
        switch(cPick)
        {
            case '1':  system("cls");
                       cInput[i]='\0';
                       instruct();
                       for (j=0; j<=6; j++)
                           fstats[j]=0;
                       classify(cInput,fstats,i,iOdd,iEven,iUpper,iLower,iOaverage,iEaverage);
                       break;

            case '2':  iCheck = strlen(cInput);
                       if (iCheck >=1)
                       {
                            system("cls");
                            for (j=0; j<iCheck;j++)
                                printf("%c", cInput[j]);
                            printf("\n");
                       }
                       else
                       {
                           printf("\nThere is no string in the memory please input a string first\n");
                           //printf("Press enter to continue");
                       }
                       //cin.clear(); 
                       //cin.ignore(80,'\n'); 
                       //cin.get();
                       system("pause");
                       break;

            case '3':  iCheck = strlen(cInput);
                       if (iCheck >=1)
                       {
                           system("cls");
                           printf("Please input the character to search:");
                           cGet = getche();
                           if (search(cGet, cInput) > 0)
                               printf("\nthe character occurs first at the index %d\n",search(cGet,cInput));
                           else
                               printf("\nthere are no occurences of that character\n");
                       }
                       else
                       {
                           printf("\nThere is no string in the memory please input a string first\n");
                           //printf("Press enter to continue");
                       }
                       //cin.get();
                       system("pause");
                       break;

            case '4':  iCheck = strlen(cInput);
                       if (iCheck >=1)
                       {
                           system("cls");
                           printf("please input character:");
                           cChoice = getche();
                           if (count(cChoice,cInput) >0)
                               printf("\nThere are %d occurences of the character %c\n", count(cChoice, cInput), cChoice);
                           else 
                               printf("\nthere are no occurences of that character\n");
                       }
                       else
                       {
                           printf("\nThere is no string in the memory please input a string first\n");
                           //printf("Press enter to continue");
                       }
                       //cin.get();
                       system("pause");
                       break;

            case '5':  iCheck = strlen(cInput);
                       if (iCheck >=1)
                       {
                           system("cls");
                           summary(fstats,i,iOdd,iEven,iUpper,iLower,iOaverage,iEaverage);
                       }
                       else
                       {
                           printf("\nThere is no string in the memory please input a string first\n");
                           //printf("Press enter to continue");
                       }
                       //cin.get();
                       system("pause");
                       break;

        }
    system("cls");
    menu();
    cPick = getche();
    }
printf("\n\n");

}

void instruct(void)
{
    printf("To terminate the program, strike the spacebar or Enter.\n\n\n");
    printf("Next key>");
}

void classify(char cInput[], float fstats[], int i, int iOdd, int iEven, int iUpper,
             int iLower, int iOaverage, int iEaverage)
{
    int j =0, iResult =0;
    char cTemp;

    cTemp = getche();

    for(;(cTemp != 32 && cTemp!= 13); ) //ascii 32 for blank and 13 for carriage return
    {
        if ( j<=59)
        {
            if (isalpha(cTemp) != 0)
            {
                if (isupper(cTemp) !=0)
                    fstats[iUpper]++;
                else
                    fstats[iLower]++;
            }

            if(isdigit(cTemp) !=0)
            {
                if( (cTemp%2) == 0)
                {
                    fstats[iEven]++;
                    iResult = cTemp - '0';
                    fstats[iEaverage] = (fstats[iEaverage] + iResult);
                }

                else 
                {
                    fstats[iOdd]++;
                    iResult = cTemp - '0';
                    fstats[iOaverage] = (fstats[iOaverage] + iResult);
                }
            }

            if (cTemp == 8)
                fstats[i]--;

            cInput[j]= cTemp;
            cInput[j+1] = '\0';
            fstats[i]++;
            j++;
            cTemp = getche();
        }
        else 
        {
            printf("\nYou cannot enter more than 60 characters, the firs 60 characters will remain\n");
            //printf("hit enter to continue");
            //cin.get();
            system("pause");
            break;
        }
    }// for loop
}

void summary(float fstats[], int i, int iOdd, int iEven, int iUpper,
             int iLower, int iOaverage, int iEaverage)
{
    if (fstats[iEven] ==0)
        fstats[iEaverage] =0;
    else 
        fstats[iEaverage]= fstats[iEaverage]/fstats[iEven];

    if (fstats[iOdd] ==0)
        fstats[iOaverage] =0;
    else
        fstats[iOaverage] = fstats[iOaverage]/fstats[iOdd];

    system("cls");

    printf("Thank you! Your summary:\n\n");
    printf("Total number of keystrokes entered:%.f\n", fstats[i]);
    printf("Upper case alphabetic characters:%.f\n", fstats[iUpper]);
    printf("Lower case alphabetic characters:%.f\n", fstats[iLower]);
    printf("Even digits:%.f\n", fstats[iEven]);
    printf("Odd digits:%.f\n", fstats[iOdd]);
    printf("Average value of the even digits entered:%.2f\n", fstats[iEaverage]);
    printf("Average value of the odd digits entered:%.2f\n", fstats[iOaverage]);

}

int count(char cChoice, char cInput[])
{
    int i, iNum, iTotal =0;

    iNum = strlen(cInput);
    for ( i=0; i<iNum; i++)
    {
        if (cChoice == cInput[i])
            iTotal++;
    }

    return iTotal;
}


int search(char cGet, char cInput[])
{
    int i, iIndex=0, iNum=0;

    iNum = strlen(cInput);
    for (i=0; i < iNum; i++)
    {
        if (cGet == cInput[i])
        {
            iIndex = i;
            iIndex++;
            break;
        }
    }

    return iIndex;
}

void menu(void)
{
    printf("1.  Enter a string of characters\n");
    printf("2.  Display the current string\n");
    printf("3.  Search for a character\n");
    printf("4.  Count occurences of a specific character\n");
    printf("5.  Produce a summary report on the current string\n");
    printf("6.  Exit\n\n");

}
Member Avatar for iamthwee

hello, I thinks it because you using getche();

Can u just use scanf() or watever it is?

i am using getche() because i'm monitoring each character inputed, basically i got to quit as soon as space character is inputed.

Member Avatar for iamthwee

Well in order to do it in real time I would do sumthing like so...

if (ch == '\b' && i > 0) 
    {
      printf("\b \b");
      
      i--;
      array[i] = '\0';
    }

'\b' is backspace

and everything it is hit it deletes whatever was stored in the array

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.