flowinwind
flowinwind

Reputation: 69

C++, User input of letters, numbers, and spaces into a string

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;

Upvotes: 0

Views: 2371

Answers (4)

Rachel
Rachel

Reputation: 71

You should use getLine like that:

string address;
cout << "\nEnter customer street address: ";
cin.getLine(address,MaxLengthOfAddress,\n);

https://www.geeksforgeeks.org/getline-string-c/

Upvotes: 0

marcos assis
marcos assis

Reputation: 388

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.

Upvotes: 0

David
David

Reputation: 4873

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);

string Reference
getline Reference numeric_limits Reference

Upvotes: 0

Nasir Mahmood
Nasir Mahmood

Reputation: 1505

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";
}

Upvotes: 0

Related Questions