| | |
Some wierd kind of bug...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
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)
#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(); }
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
3. The program does not handle backspace and arrow keys. So if you make a typing mistake there is no way to correct it.
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)
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.
Well I coded for the backspace. Here is the code if anybody is interested...
I put it in a Do-While loop
C++ Syntax (Toggle Plain Text)
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...
I put it in a Do-While loopC++ Syntax (Toggle Plain Text)
if (str[num] == '\b') { str[num] = '\0'; printf(" \b"); str[num-1] = '\0'; num = num - 1; }
C++ Syntax (Toggle Plain Text)
if (str[num] == '\b') { printf(" \b"); str[--num] = '\0'; // sub 1 from num and set to \0 }
Also, what happens if there are no characters and num is 0?
C++ Syntax (Toggle Plain Text)
str[num] = str[num] - 'A' + 'a'; // converts it to lowercase
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- wierd email problem (Windows NT / 2000 / XP)
- MSVC++ liking issue (C++)
- How is your forum doing? (Growing an Online Community)
Other Threads in the C++ Forum
- Previous Thread: The Visual Studio® 2005 Standard Edition
- Next Thread: c++ white space question?
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







