Reputation:
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?
Upvotes: 1
Views: 11691
Reputation: 23941
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;
Upvotes: 4
Reputation: 278
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
Upvotes: -1
Reputation: 36838
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.
Upvotes: 0
Reputation: 347256
#include <iostream>
#include <string>
int main(int argc, char**argv)
{
std::string s;
std::cin >> s;
s += " ok";
std::cout << s;
return 0;
}
Upvotes: 0