OK, I've been banging my head against a wall for a few hours. And I need some sleep. Can someone please show my how to accept input and keep count of it so I can send WriteBytes2() the address of the buffer and the number of bytes entered?

Here is a snipet of what I have...

quit = false;
while (buffer!=CR)
	WriteBytes2(&buffer, count); //write to other terminal
	if (buffer=='?') 
	{					//check for quit command
		return 0;

but the count isn't right. My output is garbage. The sad thing is about 4 hours ago I had this thing working but I don't know what I changed that messed the count up (besides deleting it in the first place).

Background: I am writing a program that will accept user input and send it over the serial port.

Thank you :eek:

int main()
  char buffer[200];
  buffer[0] = 199;   //Here we can control how many character we can enter, thus a maximum of 199 characters
  char *p;

  p =  cgets(buffer);  //We get characters till the user presses Enter or CR

  printf("\nYou Entered: %s\n", p);      //We prit the characters that the user entered
  //your codes goes here
  char *result = strchr(buffer, '?');   //Here we test to see if the charaters include "?"

    if (result !=NULL)
      cout << "WE got it";
      return 0;


I've got this through Borland C++Builder6, I don't think you have cgets or conio.h!!!

If your input is a C-style string, you can use strlen:

#include <cstring>

std::size_t len = std::strlen ( buffer );

If your input is an std::string, it's even easier. Just use the size or length member functions. Alternatively, since it appears you're just reading a string, you can use scanf and the %n modifier to tell you how many characters were read:

char buffer[1024];
int n = 0;

scanf ( "%1023s%n", buffer, &n );

Or you can fill the buffer character by character and keep a count. There are tons of options.

This article has been dead for over six months. Start a new discussion instead.