-
July 1st, 2009, 02:28 PM
#1
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?
-
July 1st, 2009, 06:34 PM
#2
Re: Preset text for cin
Not possible using standard libraries only. If you go for platform-specific libraries, there is probably a way.
-
July 1st, 2009, 08:14 PM
#3
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.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|