943,917 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1337
  • C++ RSS
Sep 18th, 2006
1

Some wierd kind of bug...

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <process.h>
  5.  
  6.  
  7. void clear(void);
  8. void settext(void);
  9.  
  10.  
  11.  
  12.  
  13.  
  14. // BEGINNING OF PROGRAM
  15.  
  16. int main() // starts the program
  17. {
  18.  
  19. char str[20]; // declares char array 'str'
  20. int num = 0; // declares the integer 'num'
  21. int length = 0; // declares the integer 'length'
  22.  
  23. clear(); // clears the screen
  24.  
  25. printf("Enter a word: "); // instructs you to enter a word
  26.  
  27. do // starts loop for input
  28. {
  29.  
  30. str[num] = getch(); // puts a char into 'str'
  31.  
  32. printf("%c", str[num]); // echoes char to screen
  33.  
  34. if (str[num] >= 'A') // this whole 'IF' block
  35. { // checks to see if the
  36. if (str[num] <= 'Z') // entered character is
  37. { // uppercase, and if so,
  38. str[num] = str[num] - 'A' + 'a'; // converts it to
  39. } // lowercase
  40. } //
  41.  
  42. num++; // increments 'num' by 1
  43.  
  44. } while(str[num-1] != 0x0D); // loops until ENTER is hit
  45.  
  46. length = num - 1; // assigns the value of 'num'
  47. // minus 1 (the length of 'str')
  48. // to 'length'
  49.  
  50. printf("\n\nYour word was %s \n\n", str); // outputs 'str' to screen
  51.  
  52. printf("\nIt is %d characters long", length);
  53. // outputs 'str' and its
  54. // length to the screen
  55.  
  56. settext(); // holds output until
  57. // a key is pressed
  58.  
  59. clear(); // clears the screen
  60.  
  61. return 0; // quits program with
  62. // condition zero
  63. }
  64.  
  65. // END OF PROGRAM
  66.  
  67.  
  68.  
  69.  
  70.  
  71. // BEGINNING OF FUNCTIONS
  72.  
  73. void clear(void)
  74. {
  75. system("cls");
  76. }
  77.  
  78. ///////////////////////////////////
  79.  
  80. void settext(void)
  81. {
  82. char text;
  83. text = getch();
  84. }
Similar Threads
Reputation Points: 50
Solved Threads: 0
Junior Poster in Training
Diode is offline Offline
70 posts
since Jan 2005
Sep 18th, 2006
0

Re: Some wierd kind of bug...

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
C++ Syntax (Toggle Plain Text)
  1. 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.
Last edited by Ancient Dragon; Sep 18th, 2006 at 11:07 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Sep 18th, 2006
0

Re: Some wierd kind of bug...

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

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

Thanks
Diode
Reputation Points: 50
Solved Threads: 0
Junior Poster in Training
Diode is offline Offline
70 posts
since Jan 2005
Sep 18th, 2006
1

Re: Some wierd kind of bug...

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
Diode
Reputation Points: 50
Solved Threads: 0
Junior Poster in Training
Diode is offline Offline
70 posts
since Jan 2005
Sep 19th, 2006
0

Re: Some wierd kind of bug...

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

C++ Syntax (Toggle Plain Text)
  1. if (str[num] == '\b')
  2. {
  3.  
  4. str[num] = '\0';
  5. printf(" \b");
  6. str[num-1] = '\0';
  7. num = num - 1;
  8. }

I put it in a Do-While loop
Reputation Points: 50
Solved Threads: 0
Junior Poster in Training
Diode is offline Offline
70 posts
since Jan 2005
Sep 19th, 2006
1

Re: Some wierd kind of bug...

Click to Expand / Collapse  Quote originally posted by Diode ...
Well I coded for the backspace. Here is the code if anybody is interested...

C++ Syntax (Toggle Plain Text)
  1. if (str[num] == '\b')
  2. {
  3.  
  4. str[num] = '\0';
  5. printf(" \b");
  6. str[num-1] = '\0';
  7. num = num - 1;
  8. }
I put it in a Do-While loop
A slightly better way may be:
C++ Syntax (Toggle Plain Text)
  1. if (str[num] == '\b')
  2. {
  3. printf(" \b");
  4. str[--num] = '\0'; // sub 1 from num and set to \0
  5. }
No need to zero the \b. Just zero the previous character.
Also, what happens if there are no characters and num is 0?

C++ Syntax (Toggle Plain Text)
  1. 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?
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,738 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: The Visual Studio® 2005 Standard Edition
Next Thread in C++ Forum Timeline: c++ white space question?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC