Hello, :)

I have chosen to go with getch() for my keyboard input.

However, when I went to experiment with it, I got some unexpected results. The 'Y' in 'Your word was ' is missing in the command prompt when I run this program after I compile using gcc, and when I compile it using Dev-C++ 4, my program doesn't even show the last 2 output lines which are intended to show you what the values are in the variables.

I get no errors or warnings when I compile in either one, and I have quadruple checked my logic, and even went back to my C book from college, and my syntax is identical to his.

Can you try this code on your compiler to see what happens? Any suggestions?

Thanks alot, :lol:
Diode

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <process.h>


void clear(void);
void settext(void);





// BEGINNING OF PROGRAM

int main()                                  // starts the program
{

  char str[20];                             // declares char array 'str'
  int num = 0;                              // declares the integer 'num'
  int length = 0;                           // declares the integer 'length'

  clear();                                  // clears the screen

  printf("Enter a word: ");                 // instructs you to enter a word

  do                                        // starts loop for input
  {

    str[num] = getch();                     // puts a char into 'str'

    printf("%c", str[num]);                 // echoes char to screen

    if (str[num] >= 'A')                    // this whole 'IF' block
      {                                     // checks to see if the
        if (str[num] <= 'Z')                // entered character is
        {                                   // uppercase, and if so,
          str[num] = str[num] - 'A' + 'a';  // converts it to
        }                                   // lowercase
      }                                     //

    num++;                                  // increments 'num' by 1

  } while(str[num-1] != 0x0D);              // loops until ENTER is hit

  length = num - 1;                         // assigns the value of 'num'
                                            // minus 1 (the length of 'str')
                                            // to 'length'

  printf("\n\nYour word was %s \n\n", str); // outputs 'str' to screen

  printf("\nIt is %d characters long", length);
                                            // outputs 'str' and its
                                            // length to the screen

  settext();                                // holds output until
                                            // a key is pressed

  clear();                                  // clears the screen

  return 0;                                 // quits program with
                                            // condition zero
}

// END OF PROGRAM





// BEGINNING OF FUNCTIONS

void clear(void)
{
  system("cls");
}

///////////////////////////////////

void settext(void)
{
  char text;
  text = getch();
}

Recommended Answers

All 5 Replies

1. Your program suffers from the same bug that gets() contains -- it will allow you to enter more characters than can be held in the input buffer. The do-while loop needs to check that buffer overflow does not occur.

2. The input buffer is not null-terminated, which might explain why the final printf() displays incorrect information. You can easily correct this by

char str[20]  = {0};

3. The program does not handle backspace and arrow keys. So if you make a typing mistake there is no way to correct it.

I figured it out, but thanks for looking. I need the line:

str[num-1] = '\0';

Thanks :)
Diode

Whoops, sorry Ancient Dragon, I clicked on Advanced Reply before I realized that you had responded. I plan on coding for backspaces and everything else that is nice later, possibly tonight.

Again, thanks :D
Diode

Well I coded for the backspace. Here is the code if anybody is interested...

if (str[num] == '\b')
      {

        str[num] = '\0';
        printf(" \b");
        str[num-1] = '\0';
        num = num - 1;
      }

I put it in a Do-While loop

Well I coded for the backspace. Here is the code if anybody is interested...

if (str[num] == '\b')
      {

        str[num] = '\0';
        printf(" \b");
        str[num-1] = '\0';
        num = num - 1;
      }

I put it in a Do-While loop

A slightly better way may be:

if (str[num] == '\b')
{
    printf(" \b");
    str[--num] = '\0';    // sub 1 from num and set to \0
}

No need to zero the \b. Just zero the previous character.
Also, what happens if there are no characters and num is 0?

str[num] = str[num] - 'A' + 'a';  // converts it to lowercase

To convert to lower case, look up the tolower() function.

And as Ancient Dragon mentioned, what happens when num is 20 or greater?

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.