0

Hello I am having trouble with the getline() function in C++ I am new to C++ and relatively new to programming period. I am in an intro to C++ course and my research that I have done on this topic myself online has not led me to a solution I do not really understand all the things that they speak about as this is the very first assignment in my class. Any quick/rough help/understanding would be greatly appreciated as we will cover this topic in class more thoroughly. Below is the code that I am writing. It is a simple program that just asks users for name and address and then displays that info in correct mailing format.

#include <iostream>
#include <string>

int main()
{
 // Variables for Mailing Addresses
  std::string firstName;
  std::string lastName;
  int houseNum;
  std::string streetName;
  std::string cityName;
  std::string state;
  int zipCode;

 // Asking for input
  std::cout << "What is your First name?: ";
  std::cin >> firstName;"\n";

  std::cout << "What is your Last name?: ";
  std::cin >> lastName;"\n";

  std::cout << "What is your House Number?: ";
  std::cin >> houseNum;"\n";

  std::cout << "What is your Street Name?: ";
  std::getline(std::cin,houseNum);
 return 0;
}

The error code thats thrown is "No matching function for call to 'getline'".

  • 2
    `std::getline(std::cin,houseNum);` shoud be: `std::getline(std::cin, streetName);` – Hawky Jan 27 '20 at 16:14
  • 1
    Why do you use `std::cin` for the first three and `std::getline` for the last? What made you want to do something different there? What does your input actually look like? – scohe001 Jan 27 '20 at 16:15

2 Answers2

1

Well, I see a couple major issues.

You have this line:

std::cin >> firstName;"\n";

The first part is correct, but the second part is not. You want this:

std::cin >> firstName;
std::cout << "\n" << std::endl;

Also, you call getline on the string objects you created earlier. std::cin is a stream-type object, not a string. I suggest you look at this page that talks about getline.

robotsfoundme
  • 418
  • 4
  • 18
1

The problem with getline() is that you are trying to assign a string stream to an int variable houseNum, hence the error.

no matching function for call to 'getline(std::istream&, int&)'

Also, the line of code:

std::cin >> firstName;"\n";

Should give you:

warning: statement has no effect [-Wunused-value]

For the "\n"

Enable compiler warnings, they save you a lot of time.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • @JohnDellaVecchio, glad to help, see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – anastaciu Jan 27 '20 at 17:56