Solved

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

Posted on 2010-11-30
6
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 34237019
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
ID: 34237080
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 31

Expert Comment

by:Zoppo
ID: 34237383
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bfunieru1
ID: 34237441
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
ID: 34237446
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 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 34238796
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to programmatically differentiate between C and Java 10 221
I could not build boost code, 10 116
Why isn't object file created? 6 107
COM server issue 2 26
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

738 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