So I have a small problem and I can't find an elegant solution to it.
I'm asking the user to input their address. How would I put that into a string? It would contain letters, numbers, and spaces. I tried the getline function but no success with that.
cout << "\nEnter customer street address" << endl;
cin >> address;
To do something like that you'll want to use a string
and getline
.
string address;
cout << "\nEnter customer street address: ";
cin.ignore(numeric_limits<streamsize>::max()); // May or may not be needed.
getline(cin, address);
you can use std::getline
int main()
{
// greet the user
std::string name;
std::cout << "What is your name? ";
std::getline(std::cin, name);
std::cout << "Hello " << name << ", nice to meet you.\n";
}
Like said:
string address;
cout << "\nEnter customer street address: ";
getline(cin, address);
With this your input can be typed until user hits enter (newline);
If you want multiple line input you'll still need some stop criteria.
You should use getLine like that:
string address;
cout << "\nEnter customer street address: ";
cin.getLine(address,MaxLengthOfAddress,\n);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.