Does anyone know why getch() would fail upon starting a second iteration in a while loop?

By fail I mean that I have to press the ENTER key after a keyboard character is pressed to continue.

So in effect it is functioning like getchar() where you have to press enter after a character is pressed.

Recommended Answers

All 6 Replies

post code. Works ok for me

#include <conio.h>
#include <iostream>
using namespace std;

int main()
{
    int c;
    while( (c = getch()) != EOF)
        cout << (char)c << " ";
}

post code. Works ok for me

#include <iostream> 
#include <fstream>
#include <conio.h>

using namespace std;

void encrypt(); void decrypt();

int main(int argc, char *argv[]){
    
    bool loop = true;
    
    while(loop){
        
        cout << "Press \"e\" to encrypt and \"d\" to decrypt a .txt file";
        char mode = getch();
      
        switch (mode){
    
            case 'e': encrypt(); break;    
            case 'd': decrypt(); break;   
        }
  
        cout << "Enter \"c\" to continue and \"e\" to exit";
        char repeat = getch();
        
        if (repeat == 'c'){
            loop = true;
            cout << '\n' << '\n';
            cin.ignore();
            
        }
    
        else
            loop = false;  
    }
     
    return EXIT_SUCCESS;
}

void encrypt(){
    
    cout << '\n' << '\n' << "<<<ENCRYPTION MODE>>>" << '\n' << '\n';

    cout << "Input Path: ";
    string inputPath = "";
    getline(cin, inputPath);
    ifstream plainText(inputPath.c_str());
    cout << '\n';
    
    cout << "Output Path: ";    
    string outputPath= "";
    getline(cin, outputPath);
    ofstream cipherText(outputPath.c_str());
    cout << '\n';
    
    cout << "Shift Value = ";
    int shiftValue = 0;
    cin >> shiftValue;
    cout << '\n';
    
    cipherText << shiftValue << '\n' << '\n';
     
    while (!plainText.eof()){
    
        string plainLine = "";
        getline(plainText, plainLine);
        int lineLength = plainLine.length(); 
        
        for (int i=0; i<=lineLength; i++){
            
            int decimalValue[lineLength];
            char cipherLine[lineLength];
        
            decimalValue[i] = (int)plainLine[i] + shiftValue; 
            cipherLine[i] = (char)decimalValue[i];
            
            cipherLine[lineLength] = '\n';   
            cipherText << cipherLine[i];    
        }    
         
    }
        
    plainText.close();
    cipherText.close();
    remove(inputPath.c_str()); 
}

void decrypt(){

    cout << '\n' << '\n' << "<<<DECRYPTION MODE>>>" << '\n' << '\n';
    
    cout << "Input Path: ";
    string inputPath = "";
    getline(cin, inputPath);
    ifstream cipherText(inputPath.c_str());
    cout << '\n';
    
    cout << "Output Path: ";    
    string outputPath= "";
    getline(cin, outputPath);
    ofstream plainText(outputPath.c_str());
    cout << '\n';
    
    string shiftString = "";
    getline(cipherText, shiftString);
    int shiftValue = atoi(shiftString.c_str());
    
    while (!cipherText.eof()){
    
        string cipherLine = "";
        getline(cipherText, cipherLine);
        int lineLength = cipherLine.length(); 
        
        for (int i=0; i<=lineLength; i++){
            
            int decimalValue[lineLength];
            char plainLine[lineLength];
        
            decimalValue[i] = (int)cipherLine[i] - shiftValue; 
            plainLine[i] = (char)decimalValue[i];
            
            plainLine[lineLength] = '\n';
            plainText << plainLine[i];  
        }   
        
    }
        
    plainText.close();
    cipherText.close();
    remove(inputPath.c_str());
}

I posted the code in which this issue is occurring. It seems that when I iterate through the while loop for the third time, getch() starts functioning like getchar() and I have to press enter after entering a character.

I uses this:

#include <iostream>
#include <conio.h>

using namespace std;

char a;

int main(){
    /* Clean keyboard buffer by a stupid way.                                 */
    /* In fact, there is some other way.                                      */
    while(kbhit()!=0){
    /* kbhit() returns 0 when there is nothing in the keyboard buffer.        */
        getch();
        /* Get one character from the keyboard buffer.                        */
    }
    cout<<"Please press a key.\n";
    a=getch();
    /* Assign a                                                               */
    cout<<"You entered: "<<a;
    return 0;
}

And I am able to get one.

So I need to flush the keyboard buffer with while(kbhit()!=0)?

So I need to flush the keyboard buffer with while(kbhit()!=0)?

Not really, but if you need to.

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.