Some wierd kind of bug...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Some wierd kind of bug...

 
1
  #1
Sep 18th, 2006
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,647
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Some wierd kind of bug...

 
0
  #2
Sep 18th, 2006
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
  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Re: Some wierd kind of bug...

 
0
  #3
Sep 18th, 2006
I figured it out, but thanks for looking. I need the line:

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

Thanks
Diode
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Re: Some wierd kind of bug...

 
1
  #4
Sep 18th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Re: Some wierd kind of bug...

 
0
  #5
Sep 19th, 2006
Well I coded for the backspace. Here is the code if anybody is interested...

  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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,124
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Some wierd kind of bug...

 
1
  #6
Sep 19th, 2006
Originally Posted by Diode View Post
Well I coded for the backspace. Here is the code if anybody is interested...

  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:
  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?

  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?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 1155 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC