-3

I am trying to create a function that displays details of user after they have signed up. Keep getting error about strcmp function.

Note i'm getting from compiler: candidate function not viable: no known conversion from 'std::__cxx11::string' (aka >'basic_string') to 'const char *' for 1st argument

Code:

void display_uDetails(char n[])
{
std::cout<<"\nUSER DETAILS\n";
int flag=0;
fp.open("book.dat",ios::in);
while(fp.read((char*)&u,sizeof(createUser)))
{
    if(strcmp(u.retuname(),n)==0)
    {
        u.show_user();
        flag=1;
    }
}
fp.close();
if(flag==0)
    std::cout<<"\n\nUser does not exist!";
getch();
}

Update:

The error is coming from my retuname() function here:

char* retuname()
{
    return uname;
} 

Display function:

void display_uDetails(char n[])
{
    std::cout<<"\nUSER DETAILS\n";
    int flag=0;
    fp.open("book.dat",ios::in);
    while(fp.read((char*)&cu,sizeof(createUser)))
    {
      if(cu.retuname() == n)
      {
        cu.show_user();
        flag=1;
      }
}
fp.close();
if(flag==0)
    std::cout<<"\n\nUser does not exist!";
getch();
}

It says *error: no viable conversion from returned value of type > 'std::__cxx11::string' (aka 'basic_string') to function return type 'char ' return uname; ^~~~~

'cu' Class definition

class createUser
{
   string uname;
   string pword;
   string name;
};

Defining createUser class as 'cu' & declaring stream

// global declaration for stream object, object 

 fstream fp, fp1;
 createUser cu;
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Dame
  • 1
  • 3
  • very similar question asked not so long ago: https://stackoverflow.com/questions/59036335/how-can-i-alphabetize-strings-from-an-array-in-c – 463035818_is_not_an_ai Nov 25 '19 at 17:39
  • 1
    `fp.read((char*)&u,sizeof(createUser))` what is `createUser` hopefully its a POD type. [https://stackoverflow.com/questions/146452/what-are-pod-types-in-c](https://stackoverflow.com/questions/146452/what-are-pod-types-in-c) – drescherjm Nov 25 '19 at 17:43
  • @drescherjm yes it is a POD type – Dame Nov 25 '19 at 17:45
  • 1
    Your errors and the shown code doesn't make sense together. – Some programmer dude Nov 25 '19 at 18:25
  • I'm also worried that `uname` might be a pointer which make the whole file reading wrong. – Some programmer dude Nov 25 '19 at 18:25
  • Take another look. I added the updated code from the display function @Someprogrammerdude – Dame Nov 25 '19 at 18:34
  • @drescherjm I am clueless then. When i run the program it is the only error i am getting – Dame Nov 25 '19 at 18:39
  • `if(cu.retuname() == n)` you can't use `==` with char arrays / c-strings. This will compare pointers not the c-string. – drescherjm Nov 25 '19 at 18:40
  • ***When i run the program it is the only error i am getting*** Maybe you did not save your changes and its compiling an old version of your code. The error still does not make sense. – drescherjm Nov 25 '19 at 18:43
  • @drescherjm But it can't be or the first error originally asked about wouldn't make sense. – Some programmer dude Nov 25 '19 at 18:43
  • 2
    @Dame It's still not a [mcve]. What is `u`? What is the class or structure definition used? – Some programmer dude Nov 25 '19 at 18:44
  • Didn't think to add that in. **u** is the class definition, it is now defined as **cu** – Dame Nov 25 '19 at 18:47
  • 1
    @Dame `u` in the first example, `cu` in the second... Which is it *really*? Again, we can't help you if you don't show us a [mcve]. – Some programmer dude Nov 25 '19 at 18:48
  • the second example clearly says **UPDATED** meaning things were adjusted @Someprogrammerdude it is **'cu'** – Dame Nov 25 '19 at 18:50
  • `createUser` is not a `POD` type. So the read will not work. You can not serialize a structure containing a std::string this way. That will not cause the error however. – drescherjm Nov 25 '19 at 18:54
  • 1
    Now the second error makes sense: `uname` is of type `std::string` while your return type for the function is `char *`. That's an invalid combination. ***But*** that return type of the function doesn't make sense with the *first* error. There's just to many inconsistencies in the question, the code and the errors. Please refresh [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And try to to create a [mcve] that we can replicate the errors ourselves. – Some programmer dude Nov 25 '19 at 18:56
  • Is it that having private elements that makes it not a POD type? Damn learn more on here than from professors lol @drescherjm – Dame Nov 25 '19 at 18:56
  • ***Is it that having private elements that makes it not a POD type?*** That is not the problem. `std::string` contains internal pointers. – drescherjm Nov 25 '19 at 18:57
  • I think this should help with the POD determination: [https://ideone.com/gd9Cpv](https://ideone.com/gd9Cpv) taken from here: [http://www.martinbroadhurst.com/pod-types-in-c.html](http://www.martinbroadhurst.com/pod-types-in-c.html) – drescherjm Nov 25 '19 at 19:02
  • @drescherjm got it to work. Just swapped strings with char and used strcmp as originally planned – Dame Nov 25 '19 at 19:30
  • 1
    Perhaps you need some more time with [a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to learn C++ better? Don't treat C++ as "C with classes", use the functionality that C++ provides, like `std::string`, containers, algorithms and streams. And if you're required to work with raw binary files then remember to actually open them in binary mode, and use *serialization* to read and write the files from C++ classes and objects. Or just use plain-text files. – Some programmer dude Nov 25 '19 at 19:50
  • I just started 2 months ago. I am getting better with strings, but everything takes practice. I did not learn C yet so I don't believe I'll be treating it like that. Thanks for the books though. – Dame Nov 25 '19 at 20:03

1 Answers1

0

Reading the error message it seems that u.retuname() is returning a std::string, which means you can't use strcmp to compare it.

But std::string have an overloaded == operator so you can compare it directly as

if (u.retuname() == n) { ... }
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I did that and I am now looking at this error: no viable conversion from returned value of type 'std::__cxx11::string' (aka 'basic_string') to function return type 'char *' return uname; – Dame Nov 25 '19 at 18:02
  • Updated the problem above – Dame Nov 25 '19 at 18:16