Preset text for cin
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: Preset text for cin

  1. #1
    Join Date
    Jul 2009

    Preset text for cin

    How can you preset suggested text for a cin?


    cout<<"Old value is: "<<oldVal<<" New value: ";

    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
    Join Date
    Oct 2007
    Seattle, WA

    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

    Re: Preset text for cin

    No, but you could wrap the concept into a function template

    This is untested pseudo code, just to illustrate

    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)