HI,
Once we accept an input from a keyboard, How can we add that character into a string in VC++?
Can anyone help me with this?
HI,
Once we accept an input from a keyboard, How can we add that character into a string in VC++?
Can anyone help me with this?
You can use std::string
from STL, and the + or += operator.
To do this, #include <string>
and use the class std::string
.
After that, there are various ways to store the input from the user.
First, you may store the character input directly into the string:
std::string myStr;
std::cin >> myStr;
Second, you can append the input to an existing string:
std::string myStr;
myOtherStr += myStr;
As others have pointed, your question indicates that you are a beginner. If that is the case, you should read on the book/tutorial of your choice. You will get more complete and understandable answers, especially from a good book. If you don't know any, you can ask for a good one in StackOverflow - tag you question with recommendation.
#include <iostream>
#include <string>
int main(int argc, char**argv)
{
std::string s;
std::cin >> s;
s += " ok";
std::cout << s;
return 0;
}
Try the following:
std::string inputStr;
std::cin >> inputStr;
This code will accept a string typed on the keyboard and store it into inputStr
.
My guess is that you're in the process of learning C++. If so, my suggestion is to continue reading your C++ book. Keyboard input will surely be addressed in some upcoming chapter or the other.
There are several ways to go about, depending of what you exactly need. Check the C++ I/O tutorial at this site: www.cplusplus.com