#include <iostream>
#include <conio.h>
using namespace std;
int main(){
    char command[1024];
    char newchar;
    cout << "Command Line Interface Test with Intellisense" << endl;
    cout << endl;
    newchar = _getch();
    command = command + newchar;
}

My Code. It doesn't work.
command = command + newchar has a problem.
Please help.

Recommended Answers

All 2 Replies

Unfortunately, that approach won't work; there is no append operator for plain character arrays, and strcat(), the append function for C-style strings (which aren't quite the same thing) doesn't work on single characters.

To add a character to a char buffer like you describe, you would need an index that tracks the current position in the array, and when you reach the end of the buffer (for example, when a newline has been entered), in order to use it as a C-string, you would have to delimit the string with a NUL character ('\00'). It's something of a pain to do manually:

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

int main(){
    char command[1024];
    char newchar;
    int index;

    cout << "Command Line Interface Test with Intellisense" << endl;
    cout << endl;

    while (true) {
        // display a prompt
        cout << "> ";

        index = 0;
        while (true) {
            newchar = _getch();
            if (newchar == '\n') {
                command[index] = '\00';
                break;
            }
            else {
                command[index] = newchar;
            }
            index++;
        }
        // perform command here
    }
    return 0;
}

Fortunatly, there is an alternative: the C++ string class, which does indeed have an append operator, and handles a lot of the details for you.

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

int main(){
    string command;
    char newchar;

    cout << "Command Line Interface Test with Intellisense" << endl;
    cout << endl;

    while (true) {
        // display a prompt
        cout << "> ";

        while (true) {
            newchar = _getch();
            if (newchar == '\n') {
                break;
            }
            else {
                command += newchar;

        }
        // perform command here
    }
    return 0;
}

Two things to note: first, the <conio.h> isn't standard C/C++, and most compilers don't have a version of it (or if it does, it may have different functions in it). For unbuffered input, it is better to use the Windows functions, or a portable library such as ncurses. Second, as I have show, you should always have main() return a value, as that indicates the state of the program to the operating system - zero means the program ended normally, anything else indicates an error.

Within the context given, the new character could be placed in at least a 2 element char array, with a null terminator, making it a string. Then concatenate that. That would also require that the target array be made a valid string at declaration.

#include <iostream>
#include <conio.h>
using namespace std;
int main(){
    char command[1024] = "";
    char addition[2] = "";
    char newchar;
    cout << "Command Line Interface Test with Intellisense" << endl;
    cout << endl;
    newchar = _getch();
    addition[0] = newchar;
    strcat( command, addition );
}
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.