Rich
Rich

Reputation: 71

C++ Issue with Stream Operators

I'm very new to Cpp and am having some issues declaring input streams. I keep getting an error, "error: no match for 'operator>>' in 'std::cin >> new_contact.Person::Name', in addition to a whole slew of other things that all relate back to misuse of the >> operator it looks like. Here is the code, as well as the structure for each of the structs being used.

#include <iostream>
#include <map>
#include <string>
#include <vector>
using std::string;
using std::vector;
using std::endl;
using std::map;
using std::cout;
using std::cin;

typedef struct {
    string Name;
    map<string,int> Number;
} Person;

void addContact(vector<Person> Contacts) {
    Person new_contact;
    char ans;
    string name;
    cout << "What is the name of your new contact (first name)? " << endl;
    cin >> new_contact.Name;
    cout << "Do you want to enter a number for your contact? " << endl;
    cin >> ans;
    while (ans == 'y') {
        cout << "What type of number are you entering? " << endl; //ex CELL, FAX, HOME, WORK, etc.
        cin >> name;
        cout << "What is the number of your contact? " << endl;
        cin >> new_contact.Number[name];
        cout << "do you want to enter another number? " << endl;
        cin >> ans;
    }
    Contacts.push_back(new_contact);
}

Please let me know where I am going wrong as I do not see any glaring issues when comparing what I have with previous errors people have run into (most people experiencing this issue seem to put the endl command at the end of the cin stream, however, I have not done this). Thanks in advance!

EDIT: Now, I am stuck with an infinite loop while trying to recheck the 'ans' variable. The function simply infinitely loops through alternating "Do you want to enter another nummber?" and "What type of number are you entering?" with no input from me.

Upvotes: 0

Views: 256

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477228

cin >> new_contact.Name requires an overload of operator>> for vector<char>, which doesn't exist.

The simplest fix is to change the type of the Name member:

string Name;

Upvotes: 2

Related Questions