-
Preset text for cin
How can you preset suggested text for a cin?
E.g.
cout<<"Old value is: "<<oldVal<<" New value: ";
cin.SetPrefixText(oldVal);
cin>>newVal;
So the user sees:
Old value is: Sam New value: Sam
So, in this made-up scenario the user can just hit the Enter key to keep the existing value.
Is this even possible?
-
Re: Preset text for cin
Not possible using standard libraries only. If you go for platform-specific libraries, there is probably a way.
-
Re: Preset text for cin
No, but you could wrap the concept into a function template
This is untested pseudo code, just to illustrate
Code:
template<T> GetValueWithDefault( T &o, T &n )
{
T newVal;
cout<<"Old value is: "<< o <<" New value: ";
cin >> newVal;
if ( IsNull( newVal ) )
{
newVal = o;
cout << o;
}
}
This code assumes you have an overload for a function IsNull which can evaluate any type T for "no entry" from cin.
The idea is simply to provide a function that takes the input, checks to see if nothing was entered, and output the default provided. Many variations on the theme are possible.