0

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;

4 Answers 4

0

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";
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

when i try getline(cin, address); it just skips over it and doesnt get user input, why?
@WhatThePhil, very odd, but see my edit. That should take care of it.
@David actually when I use cin.ignore it doesn't work for me (using onlinegdb C++17)
0

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.

Comments

0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.