0

I wrote this code to obtain name. telephone and address of a person and then i input these into class object variables:

#include<iostream>
#include<cstdlib>
#include<fstream>
#include<string>
using namespace std;

class contact{
public:
    string name;//ALL CLASS VARIABLES ARE PUBLIC
    unsigned int phonenumber;
    string address;

    contact(){//Constructor
        name= "Noname";
        phonenumber= 0;
        address= "Noaddress";
    }

    /*void input_contact_name(string s){//function to take contact name
        name=s;
    }
    void input_contact_number(int x){//function to take contact number
        phonenumber=x;
    }
    void input_contact_address(string add){//function to take contact address
        address=add;
    }*/
};

int main(){
    contact *d;
    d= new contact[200];
    string name,add;
    int choice;//Variable for switch statement
    unsigned int phno;
    static int i=0;//i is declared as a static int variable
    bool flag=false;
    cout<<"\tWelcome to the phone Directory\n";//Welcome Message
    cout<<"Select :\n1.Add New Contact\n2.Update Existing Contact\n3.Delete an Existing Entry\n4.Display All Contacts\n5.Search for a contact\n6.Exit PhoneBook\n\n\n";//Display all options
    cin>>choice;//Input Choice from user
    while(!flag){//While Loop Starts
        switch(choice){//Switch Loop Starts
        case 1:
            cout<<"\nEnter The Name\n";
            cin>>name;

            d[i].name=name;
            cout<<"\nEnter the Phone Number\n";
            cin>>phno;
            d[i].phonenumber=phno;
            cout<<"\nEnter the address\n";
            cin>>add;
            d[i].address=add;
            i++;

            flag=true;
        }
    }
    return 0;
}

However If I enter the name separated with its surname, the code bypasses the next cins and exits. Can some one help me out why this happens? Same happens when I enter 10 digit cell no. Thanks in advance

3
  • 1
    Please put some effort into formatting your code. It would also be a good idea to post shorter samples. Commented Apr 15, 2011 at 10:09
  • Thanks Mat, I would keep that in mind in future :D Commented Apr 15, 2011 at 10:13
  • I suggest you accept the answer given by ildjarn. It's been some time and it is the correct answer. Commented Jun 8, 2011 at 7:42

1 Answer 1

6

Use std::getline() instead of operator>> to extract a std::string containing whitespace.

Sign up to request clarification or add additional context in comments.

2 Comments

I wondered what the problem is with operator>> and strings with spaces?
operator>> reads one element. Typically, an element is considered to be terminated by the next whitespace, as is the case for std::string and all primitives. std::getline is your friend, as the answer says.

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.