i made a class test program with telephone directory.
it writes name and telephone number but not surname
i have used return name,surname which does not carry both string.
is it possible to do them without writing another function script.
thanks in advance

#include <cstdlib>
#include <iostream>

using namespace std;

class telephone_book
{
      public:
             telephone_book (string name,string surname,int number)
             {
                       setrecord(name,surname,number);
                       }
             void setrecord (string name,string surname,int number)
             {
                  name1=name;
                  surname1=surname;
                  number1=number;
                  }
      string takerecord()
      {
             return name1,surname1;
             }
      int takerecord1 ()
      {
          return number1;
          }
void displaymessage ()
{
     cout << "telephone book \n"<<  takerecord()<<"?"<<takerecord1()<<endl;
    
}
 private:
            string name1,surname1;
            int  number1;
};


int main(int argc, char *argv[])
{
    telephone_book record1("burcin","erek",111111);
    telephone_book record2("deniz","xxxx",222222);
    cout << "record1="<< record1.takerecord()<<" "<<record1.takerecord1()
    << "\nrecord2 ="<< record2.takerecord()<< " "<<record2.takerecord1()<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Recommended Answers

All 3 Replies

You can't return more than one value. Pass in your second variable by reference: string takerecord(string & surname) { //set surname equal to surname1 and return name1 }

thank you.

i did as below

  string takerecord(string &surname)
      {
             surname=surname1;
             return name1;
             }
cout section of the main was modified as
  cout << "record1="<< record1.takerecord(name1)<<" "<<record1.takerecord(surname)<<" "<<record1.takerecord1()
    << "\nrecord2 ="<< record2.takerecord(name1)<< " "<<record2.takerecord1(surname)<<endl;

name duplicates instead of surname
nevertheless i am so wierd.

A simple

string surname;
string firsname = record1.takerecord(surname);
cout<<"record1="<<firstname<<" "<<surname<<etc.

There's no need to keep calling the method. I honestly can't remember if the order of execution of the "<<" is guaranteed to go left to right from implementation to implementation, which is why I didn't put it all on one line.

You could also modify your method to keep things less confusing.

void takerecord(string & name, string & surname)
{
    name = name1;
    surname = surname1;
}

Which avoids having to know which is returned and which is passed by reference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.