//person class
class Person
{
//private:
public:

    char name[30];
    char type[20];
    char date[10];
    char id[10];
    Person(char pname[],char ptype[],char pdate[],char pid[]);
    void addPerson();
    void personDelete();
    char personFind(char pid[]); 
};

//this function find with id and return the name of the person

char Person::personFind(char pid[])
{
    fstream readFile;
    readFile.open("employee.txt",ios::in);

    if(readFile.fail())
    {
        cout<<"Cannot open file"<<endl;
        exit(1);
    }

    readFile>>id>>name>>type>>date;

    while(!readFile.eof())
    {
        if(id==pid)
        {
            return name;
            //break;
        }
    readFile>>id>>name>>type>>date;
    }
    readFile.close();
}

//main 
int _tmain(int argc, _TCHAR* argv[])
{
    Person *p=new Person("j","j","j","j");

    p->addPerson();
    cout<<endl;
    p->personFind("22");

    char ch;
    cin>>ch;

    return 0;
}

i got this error

Error   5   error C2440: 'return' : cannot convert from 'char [30]' to 'char'   c:\users\stephen\desktop\abc company\abc company\supermarket.cpp    91  

what is the sollution of this error please help
thank you

Edited 3 Years Ago by mike_2000_17: Fixed formatting

In line 91 you say: return name; . Name is declared as char name[30] , so it's an array of 30 chars. But you've defined your function like this: char Person::personFind(char pid[]) so it can only return 1 char.

To solve the problem, I strongly suggest that you use std::string instead of char[].

example:

std::string something()
{
    return "this was returned by something";
}
int main()
{
    std::cout << something();
    return 0;
}

In line 91 you say: return name; . Name is declared as char name[30] , so it's an array of 30 chars. But you've defined your function like this: char Person::personFind(char pid[]) so it can only return 1 char.

To solve the problem, I strongly suggest that you use std::string instead of char[].

example:

std::string something()
{
    return "this was returned by something";
}
int main()
{
    std::cout << something();
    return 0;
}

can we use string data type in c++ if i use that there is lot of errors

string name;

i think we cant use string data type in c++ pls gv an idea

tnx byee

#include "SuperMarket.h"
#include <iostream>
#include<conio.h>
#include<fstream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<iomanip>

ummm you could try changing the function to have return type pointer to char...

i.e.

//in the Person class...
[B]char*[/B] personFind(char pid[]);

//...

[B]char*[/B] Person::personFind(char pid[])
{
     //code goes here...
}

I am also worried that if you end up returning the name, you do not close the file...that is, your code never reaches the readFile.close() line (unless I'm missing something)...

This article has been dead for over six months. Start a new discussion instead.