Solved

How to use a default value for a string input from console in ANSI c++

Posted on 2010-11-30
6
436 Views
Last Modified: 2012-05-10
Does anybody knows a standard function in ANSI C++ which accepts a default value of the input value for reading a string from console?

For example I need that the user inputs something like a name but already gets into the console an default name ("John Doe") which he can change. Is there such a function or code block available?




0
Comment
Question by:bfunieru1
  • 3
  • 2
6 Comments
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
Are you referring to passing the "name" as a command line parameter or maybe reading from stdin when starting the process? You question is a little unclear.

FWIW, there are no "standard" ANSI C++ functions that will do what you ask but once your requirement is understood it shouldn't be too hard to cobble a solution together for you.
0
 

Author Comment

by:bfunieru1
Comment Utility
The later. Not a command line parameter after program name.

It should be something as what one get from a function as cin or gets.

0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
Hi,

if I understand correctly you want the user to input a name, but if i.e. the user just hits ENTER a default name should be used, right?

If so see the attached code ...

Hope that helps,

ZOPPO
std::string getName( const std::string strDefault )

{

	std::string strName;

	std::cout << "Enter your name [default='" << strDefault << "']: ";

	getline( std::cin, strName );



	if ( false != strName.empty() )

	{

		strName = strDefault;

	}



	return strName;

}



int main(int argc, char* argv[])

{

	std::string strName = getName( "John Doe" );



	std::cout << "Entered name: '" << strName << "'" << std::endl;



	return 0;

\

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:bfunieru1
Comment Utility
This solution can also work for sure.
I will prefer if it will be possible to  change something in the default string if for example instead of the short "John Doe" is something much longer which will make new writing very anoying.

0
 

Author Comment

by:bfunieru1
Comment Utility
To make it more clear it should act similar to a windows textbox which has a default text which one can change or if it is OK just press ENTER and send it.
0
 
LVL 30

Accepted Solution

by:
Zoppo earned 500 total points
Comment Utility
Sorry, I don't know if such a functionality exists or if it is even possible to do such thing in standard C++.

The problem IMO is that C++'s 'cin' simply retrieves the entered data from the used console (i.e. in Windows 'cmd.exe') - but, any editing functionality is handled by the console, there IMO is no chance to anyhow affect it using and STL functionality.

It may be that you can do something similar as you need using a CURSES library (but i cannot guarantee this since I never used such a library) - see more about this here: http://en.wikipedia.org/wiki/Curses_%28programming_library%29

ZOPPO
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now