I've been stuck on this homework assignment all week. Just when I get the program to finally run, I realize that by using just cin >> breed, if my input has a space it ruins the code (as my program requires gathering 3 separate variables, first an int, then a string, and last a bool). Because this is the second variable, it messes up my code using phrases that have a white character. When I try changing it to cin.get or cin.getline, this is the error message I get:
c2664 error "cannot convert argument 1 from std::string to _Elem *"
Below is the code in question (the middle line is giving the error). Any help would be greatly appreciated!
#include <iostream>
#include <string>
using namespace std;
int main()
{
int birthyear;
string breed;
bool vaccines;
cout << "Please enter value for dog's birth year: ";
cin >> birthyear;
cout << "What is the breed of the dog: ";
cin.getline(breed, 100);
cin.ignore();
cout << "Has the dog been vaccinated (1 = Yes/ 0 = No): ";
cin >> vaccines;
}
First up, you need to be aware that there are two
getlinethings in C++, one in the I/O area and one in the top-level standard namespace.cin.getline(breed, 100)is the one in the I/O area (specificallyistream::getline()and it knows nothing about strings, preferring to work on character arrays. You should probably avoid that one.The one that does know about strings is
std::getline()and that's generally the preferred one if you don't want to go back to the bad old days of C-legacy "strings".In addition, you need to be careful in C++ when you mix the type-specific input (like
<<) and line-specific input (likegetline) operations. It's important to know where the file pointer is before and after each operation.For example,
cin << someIntwill leave the file pointer immediately after the integer it reads in. That means, if your next operation isgetline(), it's likely to find everything on the line after that integer (at the bare minimum, this will be the newline character that you entered to get the integer processed), not the next line where you're going to be typing in your string.A simple fix for your case is to ignore everything up to and including the newline before you attempt to get the next line. That can be done with
ignore():You could also opt for ensuring all input is line-based (converting those lines to the correct type once they're entered) since that's likely to ease your task of ensuring the pointers are in the right place, and that errors in input (like entering
xyzzyfor an integer) can be better handled.Something like this should be a good start: