CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2009
    Posts
    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?

  2. #2
    Lindley is offline Elite Member Power Poster Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+)
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,861

    Re: Preset text for cin

    Not possible using standard libraries only. If you go for platform-specific libraries, there is probably a way.

  3. #3
    Join Date
    Nov 2006
    Posts
    1,611

    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).

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts



HTML5 Development Center

Click Here to Expand Forum to Full Width