I've been running around like crazy trying to find anything on this problem and so far have found nothing.
I have a function in console to update a record in a file and waht I want to do is when it asks for the updated input it displays the existing value

When input is requested you have

Name: James_

instead of

Name: _

and then you can edit the existing name as if you had typed it yourself.

Here is a snippet of what I currently have, just asking for input.

do
{
	cout << "Please enter client details" << endl
		  << "Name:" << endl
		  << "Age:" << endl
		  << "Rating (0.0 to 1.0):" << endl;
	gotoxy( 7, 2 );
	cin >> temp.name;
	gotoxy( 6, 3 );
	cin >> temp.age;
	gotoxy( 22, 4 );
	cin >> temp.rating;

	cout << endl << "Is this correct? (y/n): ";
	choice = getch();
	while ( choice != 'y' && choice != 'n' )
		choice = getch();
} while ( choice != 'y');

Recommended Answers

All 4 Replies

To do that in the console you can manually read characters and handle backspacing:

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

void GetInput(const std::string& prompt, std::string& field)
{
  std::string temp = field;
  int ch;

  std::cout << prompt << ' ' << field;

  while ((ch = getch()) != '\r') {
    switch (ch) {
      default:
        // Store and display the character
        temp += (char)ch;
        std::cout << (char)ch;
        break;
      case '\b':
        // Don't allow backspacing beyond the field
        if (temp.size() == 0)
          continue;

        // Destructive backspace for the
        // stored data and display
        temp.resize(temp.size() - 1);
        std::cout << "\b \b";
        break;
    }
  }

  // Don't forget to display the newline
  std::cout << '\n';

  // Update the field if something was typed
  if (!temp.empty())
    field = temp;
}

int main()
{
  std::string name = "James";

  GetInput("Name:", name);

  std::cout << "The name is \"" + name + "\"\n";
}
[...]
  while ((ch = getch()) != '\r') {
[..]

Why use getch() when there's a standard function: getchar()?
Without getch() you can also lose: #include <conio.h> I know Edward's coding skills and I guess getch() got there by mistake?

> Why use getch() when there's a standard function: getchar()?
Because getchar() doesn't do what Edward needed.

getchar() is completely segregated from the console buffer such that the buffer contents are only sent to the program after a newline is detected; it doesn't recognize the backspace key because the console buffer interprets that scan code long before the buffer contents are finalized, so a function that supports raw input is needed to edit parts of the prompt.

In a small way, Edward is using a non-standard function to simulate the behavior of the console buffer from within Ed's program to take advantage of real-time buffer editing. getch() was a conscious choice based on the needs of the function and lack of a suitable standard alternative. :)

commented: Good post. +3

getchar() is completely segregated from the console buffer such that the buffer contents are only sent to the program after a newline is detected;

Darn, you're right. I haven't been programming 'c' in a while. My apologies for my previous post

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.